Checking a field for characters

Howlsta

Vampire Slayer
Local time
Today, 14:37
Joined
Jul 18, 2001
Messages
180
On my form I want to code the before_update event to check if there are any characters in a txt box, cos I only want the user to enter figures. I couldn't find the answer in help. I reckon it must be pretty simple though, anyone know how?

Rich
 
hi Rich,

you could use something like
Code:
    If Me.Text0 Like "*[a-z]*" Then
        MsgBox "True"
    End If

HTH

Drew
 
Try the following in the Before Update event of your control:

Private Sub YourControlName_BeforeUpdate(Cancel As Integer)
Dim strMsg As String, strTitle As String
strMsg = "This Entry Must Be A Numeric Value"
strTitle = " Data Entry Error"
If Not IsNumeric(Me.ActiveControl) Then
MsgBox strMsg, vbCritical + vbOKOnly, strTitle
Cancel = True
End If
End Sub

HTH
RDH
 
Thanks Drew that worked fine.
I also didn't know about that isNumeric function Hicksy, so will be able to use that in future.

Rich
 
Hmmmm ......Well here is a situation:

The user enters the following: 287:#@67 -(9*%!

Drew's method is only looking for the characters A thru Z.

Just a thought ......
RDH

[This message has been edited by R. Hicks (edited 09-03-2001).]
 
Good point Ricky, never under-estimate the ability of users to do the wrong thing. Shouldn't your* ReallyIsNumericIPromise function be in there too?

Drew

*think it was yours..?
 
Here's a function that strips any disallowed characters from a given string:

Code:
Public Function AllowOnly(yourstring As String, Allowed As String)
If Allowed = "" Then Allowed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim intLoop As Integer
For intLoop = 1 To Len(yourstring)
    If InStr(Allowed, Mid(yourstring, intLoop, 1)) > 0 Then
        AllowOnly = AllowOnly & Mid(yourstring, intLoop, 1)
    End If
Next intLoop

End Function

So
AllowOnly("1234abc","1234567890") returns "1234"

AllowOnly("123abc456","1234567890") returns "123456"

AllowOnly("123&%$@abc","") returns "123abc" (if you don't tell it what is allowed, it defaults to alphanumeric, but you can change the default, of course)

HTH

Mike


[This message has been edited by Mike Gurman (edited 09-04-2001).]

[This message has been edited by Mike Gurman (edited 09-04-2001).]
 
just answered a question on a different forum and thought it may apply here too. Why not force the checking while still in the field? Using the OnKeyPress event do

If Not (KeyAscii < 32 Or (KeyAscii > 48 And KeyAscii < 57)) Then
KeyAscii = 0
End If

disallows all entries except numbers and control chars - just ignores them as far as the user is concerned. They don't have to come back later to be told they did it wrong, they just can't do it wrong. I like it anyhow
smile.gif


Drew
 
I don't understand why everyone is trying to "Re-invent the Wheel" ........

Access has a built in function to examine a string to validate if the entry is a Numeric value. The original post enquired about only allowing "figures". I assumed the to mean only allow Numeric values ......

Then all that should needed is:

If Not IsNumeric(YourString) Then ..... ect

If any character in "YourString" in not a numeric value then the result will not be allowed.

RDH

[This message has been edited by R. Hicks (edited 09-07-2001).]
 
Drew,

I kinda like that code you demonstrated for the on keypress event. It prevents the mistake from happening in the first place, and I can certainly use it in my project.
It's good to see that there are many alternative ways for this type of problem.

I've been looking in help for definitions of numbers used for abbreviation in code for instance, the key range in the example is say 32-48 and in this code I found:
DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70

where can I find these definitions in help.

Rich

[This message has been edited by Howlsta (edited 09-09-2001).]

[This message has been edited by Howlsta (edited 09-09-2001).]
 

Users who are viewing this thread

Back
Top Bottom