By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,808 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,808 IT Pros & Developers. It's quick & easy.

A2K: problem with limitfieldsize code that restricts number of input characters

P: n/a
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.ActiveControl

' 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

Feb 8 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Deano" <de***@mailinator.comwrote in message
news:61*************@mid.individual.net...
>I use this code from http://support.microsoft.com/kb/210047 to stop more
than 50 characters being input into a textbox. [snip]
The problem is that once the limit is reached the count stays at 50 even
after the user deletes a few characters ...
Haven't fooled with that particular solution, but here's an alternative:
Unbound text box: limiting entry length
at:
http://allenbrowne.com/ser-34.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

Feb 8 '08 #2

P: n/a

"Allen Browne" <Al*********@SeeSig.Invalidwrote in message
news:47**********************@per-qv1-newsreader-01.iinet.net.au...
"Deano" <de***@mailinator.comwrote in message
news:61*************@mid.individual.net...
I use this code from http://support.microsoft.com/kb/210047 to stop more
than 50 characters being input into a textbox. [snip]
The problem is that once the limit is reached the count stays at 50 even
after the user deletes a few characters ...

Haven't fooled with that particular solution, but here's an alternative:
Unbound text box: limiting entry length
at:
http://allenbrowne.com/ser-34.html
I might use that where I can, but I'm mostly using bound textboxes.
Feb 8 '08 #3

P: n/a
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.ActiveControl

' 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
What happens if you remove the
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
http://www.youtube.com/watch?v=TNJJSYC4zWM
Feb 8 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.