View Full Version : Checking a field for characters
Howlsta 09-03-2001, 07:08 AM 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
If Me.Text0 Like "*[a-z]*" Then
MsgBox "True"
End If
HTH
Drew
R. Hicks 09-03-2001, 07:53 AM 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
Howlsta 09-03-2001, 08:00 AM 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
R. Hicks 09-03-2001, 08:39 AM 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..?
Mike Gurman 09-04-2001, 01:45 AM Here's a function that strips any disallowed characters from a given string:
Public Function AllowOnly(yourstring As String, Allowed As String)
If Allowed = "" Then Allowed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN OPQRSTUVWXYZ"
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 http://www.access-programmers.co.uk/ubb/smile.gif
Drew
R. Hicks 09-07-2001, 12:56 PM 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).]
the problem was that IsNumeric will allow 1.2e3 as it fits scientific notation, but having reviewed my post above, why not just use an input mask...
Drew
http://www.access-programmers.co.uk/ubb/Forum7/HTML/001756.html
[This message has been edited by Drew (edited 09-07-2001).]
Howlsta 09-09-2001, 01:26 PM 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).]
BukHix 09-09-2001, 01:52 PM Is this what you are looking for?
Conquer Access RunCommand Constants (http://home.clara.net/tkwickenden/)
Howlsta 09-09-2001, 01:53 PM Yeah that's spot on. Cheers,
Rich
|
|