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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Strings 1

Status
Not open for further replies.

lizray

Programmer
May 14, 2008
126
AU
I have a text box used to input values. I need a simple way to determine on the beforeupdate event whether a non-numeric char has been input. Any help would be appreciated
 
Thanks Remou, Sorry, I did not give enough info. the textbox can allow input of spaces and and *(for the like function) but all other chars can only be numeric, as in phone numbers. It probably has to be checked char by char. But my attempts have been very clumsy
 
Something like:

Code:
strCheck = Me.txtBox
For i = 1 To Len(strCheck)
    If Not IsNumeric(Mid(strCheck, i, 1)) And Mid(strCheck, i, 1) <> "*" Then
        blnErr = True
    End If
Next

If blnErr Then
    MsgBox "Numbers & * only, please"
End If
 
you need to check if the ascii code is 20 (space) or between "1" and "9". If the format remains the same every time then you could use a validation to check the input data. I like the isnumeric metod suggested by Remou.

Ian Mayor (UK)
Program Error
Your lack of planning is not my emergency!
 
Or
Code:
blnErr = NOT Isnumeric(Replace(Replace(strCheck,"*","")," ","") & "e0")
 
On second thought ... that doesn't quite work. strings like

+47*, -8 5 or $27

would still pass the test.

You would need
Code:
blnErr = NOT (Isnumeric(Replace(Replace(strCheck,"*","")," ","") & "e0") And _
              Instr(1,"$+-",left(strCheck,1))=0)
 
lizray,
Use the KeyPress event to limit what characters the user can input?
Code:
Private Sub [i]YourTextBox[/i]_KeyPress(KeyAscii As Integer)
  Const strcKeepers As String = "()*-0123456789"
  If InStr(1, strcKeepers, Chr(KeyAscii)) = 0 Then
    Beep
    KeyAscii = 0
  End If
End Sub

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
CautionMP . . .

[blue]Excellent![/blue] [thumbsup2]

Nice, short, simple, sweet . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Thanks everyone. It is possible to go back to the textbox and modify chars, so I cannot check as the chars are input, but I am using your suggestions Remou and Golom and it is now pretty much working
 
if you're using the * as a LIKE qualifier, you'll have to check that it's at the end of the string as a * in the middle would cause an error.

Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top