require # characters in field

  • Thread starter Thread starter Rebecca
  • Start date Start date
R

Rebecca

I have set the character length in a field to 20.
How do I set the field (or in the form where I enter the data) so that every
entry _has_ to have 20 characters?

In other words--if someone hits tab to go to the next field after typing in
19 or 21 characters, I want an error message to pop up and the entry to not
be accepted. (The person will have to go back and fix the entry)

Thanks.
 
hi Rebecca,
I have set the character length in a field to 20.
How do I set the field (or in the form where I enter the data) so that every
entry _has_ to have 20 characters?
Either use the Form Before Update and Form Before Insert event or the
TextBox After Update event with this kind of code:

yourTextBox.Value = Trim(yourTextBox.Value)
If Len(yourTextBox.Value) <> 20 Then
MsgBox "Text to short."
End If

If you are using the form events set also Cancle = True in this case.


mfG
--> stefan <--
 
I have set the character length in a field to 20.
How do I set the field (or in the form where I enter the data) so that every
entry _has_ to have 20 characters?

In other words--if someone hits tab to go to the next field after typing in
19 or 21 characters, I want an error message to pop up and the entry to not
be accepted. (The person will have to go back and fix the entry)

Thanks.

Code the Form Control's BeforeUpdate event:
If Len(Me.[ControlName]) <> 20 then
MsgBox "You must enter exactly 20 characters."
Cancel = True
End If

The message will appear and the focus will go back to that control for
correction.
 
Rebecca

An alternate approach to that offered by Stefan and by FredG would be to
have that control on the form test its own length AFTER EVERY KEYSTROKE.
That way you could have the code jump to the next control when the 20th
character was entered.

You would still need a way to prevent under-filling the field, but you could
accomplish this by setting the focus back to the same control if the length
of the entered characters was less than 20.

Not necessarily any better, just another approach...

(I'm curious, though, what the 20 characters constitute. If you'll provide
a bit more description, and perhaps an example, folks here may be able to
offer alternate approaches to having to require exactly 20 characters.)

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
Back
Top