October 13, 2005 at 12:02 pm
Hello
I have one unbound form .
On this form i have a textbox that i want to limit input to 20 characters.
How can i achieve this.
thanks
Luis Santos
October 14, 2005 at 2:03 am
You could either use an input mask, (See Input Mask Property in Help files for details of options) or for a more user friendly way, check the length on the After Update event. You could then program a better message and take appropriate action.
October 14, 2005 at 4:51 am
you can also physically limit them from typing in more than a specific number of characters on the fly
you need to stick a line of code on each control you want to limit the length of on its OnChange Event
e.g.
Private Sub ControlName_Change()
limittextlength Len(ControlName & ""), 32
End Sub
where len(ControlName) is the current length of input in that control, and 32 is the maximum length. (the & "" is jsut there so null values dont bum out the Len() operator)
and then make a new module (so its globally available to all your forms if you want to use it everywhere)
Option Compare Database
Option Explicit
' For limiting length of unbound fields
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Const WM_SETTEXT = &HC&
Const adhcEM_SETLIMITTEXT = &HC5&
Public Sub limittextlength(currentlen As Long, lngLimit As Long)
' currentlen = length of text eg len(controlname & "")
Dim hwnd As Long
Dim lngNewMax As Long
' Get the window handle for the current window.
hwnd = GetFocus()
lngNewMax = currentlen
If lngNewMax < lngLimit Then
lngNewMax = lngLimit
End If
SendMessage hwnd, adhcEM_SETLIMITTEXT, lngNewMax, 0
End Sub
October 14, 2005 at 7:22 am
Start with this
Private Sub txtData_KeyPress(KeyAscii As Integer)
If Len(Me.txtData.Text) < 20 Or KeyAscii = 8 Then
'do nothing
Else
KeyAscii = 0
End If
End Sub
October 14, 2005 at 8:45 am
Store the maximum length in the controls .Tag property and insert this code into each forms class module:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'we eliminate key strokes causing controls to exceed allowed length
If KeyCode = 32 Or _
(KeyCode >= 48 And KeyCode <= 111) Or _
(KeyCode >= 124 And KeyCode <= 143) Or _
(KeyCode >= 146 And KeyCode <= 255) Then
Dim iLen As Integer, strTag As String
strTag = Nz(Nz(Me.ActiveControl.Tag, ""), "Length"), "")
If ContainsAlpha(strTag) Or Trim(strTag) = "" Then Exit Sub
iLen = CInt(strTag)
If iLen > 0 Then
If Len(Me.ActiveControl.Text) >= iLen Then
KeyCode = 0
MsgBox ("Field cannot exceed " + CStr(iLen) + " characters.")
End If
End If
End If
End Sub
October 26, 2005 at 4:57 pm
Hi Martin
I would like to thank you for your excellent code that is work very well.
Many thanks
Luis Santos
October 26, 2005 at 4:59 pm
Hi Martin
I would like to thank you for your excellent code that you have send me. They work very well
Thanks
Luis Santos
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply