Checking Field Length

D

DS

I'm typing into an unbound textbox.
I want to limit the number of characters that can be entered.
As your typing the letters in, if you go over a certain number, lets say 5 I
want a msgbox (that Imade to pop up)
I tried this on the after update event but it doesn't seem to fire until
after one leaves the field. I need it to check in live time. Any help is
appreciated.
Thanks
DS

Dim SZ As Long
SZ = Nz(Len(Me.Text0), 0)
If SZ > 5 Then
DoCmd.OpenForm "frmMsgInfo"
Forms!frmMsgInfo!TxtMsg = "5 Characters Only"
Else
End If
 
F

fredg

I'm typing into an unbound textbox.
I want to limit the number of characters that can be entered.
As your typing the letters in, if you go over a certain number, lets say 5 I
want a msgbox (that Imade to pop up)
I tried this on the after update event but it doesn't seem to fire until
after one leaves the field. I need it to check in live time. Any help is
appreciated.
Thanks
DS

Dim SZ As Long
SZ = Nz(Len(Me.Text0), 0)
If SZ > 5 Then
DoCmd.OpenForm "frmMsgInfo"
Forms!frmMsgInfo!TxtMsg = "5 Characters Only"
Else
End If

Wrong event and syntax.

1) The Control's Change event fires each time an entry is made.
2) A control does not have a value until you exit the control.
Until than moment, you have to refer to it's Text property.

Code the Control's Change event:

If Len(Me![ControlName].Text) > 5 Then
MsgBox "you've hit the max."
Me.[ControlName].Text = Left([ControlName].Text, 5)
End If
 
D

DS

Thanks Allen.

I tried this based on your code and it doesn't seem to work.
I'm sure I've misread something.
Thanks
DS

Private Sub Text0_Change()
If Len(Me.Text0) > 5 Then
DoCmd.OpenForm "frmMsgInfo"
Forms!frmMsgInfo!TxtMsg = "5 Characters Only"
Me.Text0 = Left(Me.Text0, 5)
Me.Text0.SelStart = 5
End If
End Sub

Private Sub Text0_KeyPress(KeyAscii As Integer)
If Len(Me.Text0) >= 5 Then
If KeyAscii <> vbKeyBack Then
KeyAscii = 0
Beep
End If
End If
End Sub
 
D

DS

Pretty Slick Fred.
It works! Never knew anything about that text property. Do you have any
more info on that?
Thanks
DS
 
D

DS

This works great. Sort of a combo of Fred's and Allens suggestions.
It Beeps, then Pops up a Message.
Stops from futher typing and takes the focus off the field.
Any downside to this let me know.
Thanks
DS
 
D

DS

Yes Allen is right you do need the on Change event as well to prevent cut
and paste. So in any case this solution seems to work. Thank you Allen and
Fred.
DS
 
L

Linq Adams via AccessMonster.com

As to your question about the *Text* property, about the only other thing you
need to know is that it's only valid when the control has focus. IF it
doesn't have focus using it will pop an error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top