Deano wrote:
I use this code from http://support.microsoft.com/kb/210047 to stop more
than 50 characters being input into a textbox.
>
****code start****
Sub LimitFieldSize (KeyAscii, MAXLENGTH)
Dim C As Control
Dim CLen As Integer
>
Set C = Screen.ActiveCo ntrol
>
' Exit if a non-printable character is typed.
If KeyAscii < 32 Then Exit Sub
>
' Exit if typing replaces a selection.
If C.SelLength 0 Then Exit Sub
>
' Fetch length of current contents + 1 for the character typed.
CLen = Len(C.Text & "") + 1
>
' Are there trailing spaces to contend with?
If C.SelStart + 1 CLen Then CLen = C.SelStart + 1
>
' Is length of string greater than max?
If CLen MAXLENGTH Then
Beep
KeyAscii = 0
End If
End Sub
****code end****
>
>
The problem is that once the limit is reached the count stays at 50 even
after the user deletes a few characters and then tries to enter something
different. This means the caret aka cursor, just stays locked and no more
input is allowed.
>
I have been trying to rewrite the code without success - can anyone see what
needs doing to make this code work in the above situation?
>
thanks
>
than 50 characters being input into a textbox.
>
****code start****
Sub LimitFieldSize (KeyAscii, MAXLENGTH)
Dim C As Control
Dim CLen As Integer
>
Set C = Screen.ActiveCo ntrol
>
' Exit if a non-printable character is typed.
If KeyAscii < 32 Then Exit Sub
>
' Exit if typing replaces a selection.
If C.SelLength 0 Then Exit Sub
>
' Fetch length of current contents + 1 for the character typed.
CLen = Len(C.Text & "") + 1
>
' Are there trailing spaces to contend with?
If C.SelStart + 1 CLen Then CLen = C.SelStart + 1
>
' Is length of string greater than max?
If CLen MAXLENGTH Then
Beep
KeyAscii = 0
End If
End Sub
****code end****
>
>
The problem is that once the limit is reached the count stays at 50 even
after the user deletes a few characters and then tries to enter something
different. This means the caret aka cursor, just stays locked and no more
input is allowed.
>
I have been trying to rewrite the code without success - can anyone see what
needs doing to make this code work in the above situation?
>
thanks
>
KeyAscii = 0
line? It seems you are setting it to null (guess I need a Ascii chart).
What would happen if you set KeyAscii to a space (32).
Lava
Leave a comment: