Larry.
Interesting, I have never used the Like operator in the Validation
Rule property before but, as you say, it works. You might also like to
consider adding Or Is Null to the end of the code in case the user
wants to delete all the text in the Text box (perhaps, for example,
where the FirstName information is not available).
Having said that I still think my original idea is preferable. Doing
it your way means that you allow the user to enter ANY character and
when they leave the field you then show them an error message to tell
them that what they have just entered is wrong (and presumably your
error message tells them what characters are allowed). They then have
to cancel that message box, delete what they have just entered and
then enter some new data (which is not very user friendly IMO). If, on
the other hand, you trap the errors in the KeyPress event they can
never enter an invalid character in the first place.
I get the impression from your other reply (and apologies if I'm
wrong) that you are not too comfortable with entering VBA code but it
is very easy to do, try this :-
With the form open in Design mode select the Text box.
Click on the small button with three dots that shows at the end of the
line on the On Key Press event property.
If the Choose Builder form appears, click on Code Builder and then
click OK (ignore this step if it doesn't).
The VBA form will appear with the cursor placed in the KeyPress event,
something like this (where Text1 is the name of your Text box).
Private Sub Text1_By_KeyPress(KeyAscii As Integer)
End Sub
Copy and paste the code I sent before into the form so that it appears
between the Private Sub... and End Sub lines.
Click the Save button on the main tool bar and you're done.
And don't forget to delete the Validation Rule and Validation Text
properties.
Peter Hibbs.
Peter,
I modified your Like statement and put it directly in the Validation Rule
property. It works. Not Like "*[!A-Z'-]*"
Thank you for your help.
Larry
:
What I normally do in this case is to only allow the user to enter the
required characters. To do this just paste the code below into the
KeyPress event of the FirstName and LastName text boxes.
If Chr(KeyAscii) Like "[!A-Z'-]" And _
KeyAscii <> vbKeyBack Then KeyAscii = 0
HTH
Peter Hibbs.
On Wed, 12 Mar 2008 18:32:01 -0700, tuesamlarry
Need syntax for a validation rule to accept only letters, hyphen, and single
quote. These are the only characters I anticipate in fields to collect first
and last names.
Using Access 2007.