Restrict data entry in a control

BrettM

just a pert, "ex" to come
Local time
Tomorrow, 04:15
Joined
Apr 30, 2008
Messages
134
I have a text box in which I wish to restrict the characters to be typed (no spaces) and set the length to a 10.

I have placed a simple statement in the "On Key Press" to restrict the spaces "If KeyAscii = 32 Then KeyAscii = 0" and this works great.

The issue I am facing is that the underlaying control is not actually being updated at this point so it is not possible to actually check the length as it is always NUL until the Update event.

I have tried forcing the issue a little by placing the following code in the "On Key Press" event...

Field= Field & Chr(KeyAscii)
Field.SelStart = Len(Field)
'otherwise the entire field is highlighted
If Len(Field) = 10 Then
KeyAscii = 0 'otherwise 2 items appear

End If

...however while this also works well, it completely screws up any in-field corrections that use the Delete or Backspace keys.

Can anyone offer a simple solution here?

Regards Brett
 
Well, if the textbox is bound to a field in the form's underlying table, the simplest way is to set the Field Length in the underlying table to 10!

If it's an unbound textbox, you can use this code:
Code:
Private Sub YourTextBox_Change()
Dim MaxChars as Integer
MaxChars = 10
 If Len(Me.YourTextBox.Text) = MaxChars + 1 Then
   Me.YourTextBox.Text = Left(Me.YourTextBox.Text, MaxChars)
   Me.YourTextBox.SelStart = Len(Me.YourTextBox)
 End If
End Sub

where MaxChars is the maximum number of characters to be allowed.

You could use this code for a bound textbox and modify it to, say move to another textbox, after the limit is reached by replacing

Me.YourTextBox.SelStart = Len(Me.YourTextBox)

with

Me.AnotherTextbox.SetFocus


Notice that I used the Change event, which fires as each character is entered in the textbox. I also use the Text property of the textbox. This property holds the value of the textbox before it is saved.
 
Thanks Linq.

The key point I got from your reply was "the Text property holds the value of the textbox before it is saved". That totally relieved my stress.

I used this point and came up with a fairly elegant solution that traps on key entry so I was even able to detect for non number entry - before any checks are done on the underlying text box. Saves a lot of validity checking later on.

in the "On Key Press" event...

If KeyAscii > 32 And KeyAscii < 48 Then KeyAscii = 0
If KeyAscii > 57 Then KeyAscii = 0
If Len(Yourtextbox.Text) = 10 Then
If KeyAscii > 48 Then KeyAscii = 0
End If


Works fabulously. Thanks again for the nudge Linq.
 
whoops

...and of course the first line should read...

If KeyAscii > 31 And KeyAscii < 48 Then KeyAscii = 0

to actually trap the space bar entry.
 
The Text property can come in handy for a very limited number of tasks, such as what you're doing here. The one caveat is that the textbox must have focus in oder for it to work.
 
thanks for that code linq.

btw: freaky, my name is BrettM also from Sydney Australia : /
 

Users who are viewing this thread

Back
Top Bottom