Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text field must only contain numbers/dashes 3

Status
Not open for further replies.

dbram

Technical User
Nov 20, 2002
12
0
0
US

I have a text field, size = 20, required. The acceptable data must be numbers and dashes, for example: like you would find in a telephone number (123-456-7890), a social security number (123-45-6789), or such ##-## structure. No other symbols, spaces, or alpha characters can be allowed.

I have researched using a field validation rule at the table level; no luck. I have tried form code on the textbox's LostFocus, BeforeUpdate, AfterUpdate, etc. events; no luck.

Can anyone point me in the right direction to evaluate each "character" in the textbox to make sure it is either a 'number' or a dash? Thanks in advance for any help.

DB


 
Try with the event before update of the text box
Use this code sample (in the example the text box name is "c").

Private Sub c_BeforeUpdate(Cancel As Integer)
Dim i As Integer, flag As String
Cancel = 0

For i = 1 To Len(Me.c)
flag = Mid(Me.c, i, 1)
If flag <> &quot;-&quot; And flag <> &quot;1&quot; And flag <> &quot;2&quot; And flag <> &quot;3&quot; And flag <> &quot;4&quot; And flag <> &quot;5&quot; And flag <> &quot;6&quot; And flag <> &quot;7&quot; And flag <> &quot;8&quot; And flag <> &quot;9&quot; And flag <> &quot;0&quot; Then
Cancel = -1
MsgBox &quot;Invalid character&quot;
Exit For
End If

Next

End Sub
 

Thank you, arebelli. Absolutely perfect.

DB

~*~*~*~*~*~
All you need is a can of WD-40 and a roll of duct tape.
~*~*~*~*~*~
 
If you are in Access, just use an Input Mask. Input masks allow you to pre-determine what is a valid text box entry without using any run-time code. Refer to the MS Help document. It has all you need for creating input mask and even how to change the mask during run-time if needed.

RUN FunnySignatureMessage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top