military donut
asked on
Validation VBA Rule for 2 Digits
This should be easy so...
I want to use VBA to create a msgbox that if they violate the rule it will bring up the msgbox
I need to know what the code would be to restrict everything except 2 Digits and nothing less or more
EX: 01
EX: 59
Not: 5
but: 50
or Not: 7
but: 77
I know it should be:
If Not ## then
msgbox "try again"
End if
I want to use VBA to create a msgbox that if they violate the rule it will bring up the msgbox
I need to know what the code would be to restrict everything except 2 Digits and nothing less or more
EX: 01
EX: 59
Not: 5
but: 50
or Not: 7
but: 77
I know it should be:
If Not ## then
msgbox "try again"
End if
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
On Before Update Event on the form of the control I got
If Len(xx)<>2 then
msgbox "Try Again"
Cancel=True
Exit Sub
End If
Just keeps giving me the same msgbox over and over, no matter what I type.
Number Field on Table with Long Integer...
No Input mask on control on form.
Just the BeforeUpdate Event...
?
If Len(xx)<>2 then
msgbox "Try Again"
Cancel=True
Exit Sub
End If
Just keeps giving me the same msgbox over and over, no matter what I type.
Number Field on Table with Long Integer...
No Input mask on control on form.
Just the BeforeUpdate Event...
?
On Before Update you have to use .Text
Len(xx.Text)
Len(xx.Text)
I would place your code in the After_Update event instead of the before_update event.
ASKER
It still gives the msgbox no matter what?
If it passes the validation, shouldn't the msgbox not appear? Why does it still show up?
If it passes the validation, shouldn't the msgbox not appear? Why does it still show up?
Try this
Function TwoDigits(Num)
dim Digits
Digits = "0123456789"
If len(Num)=2 then
if (instr(1,Digits,left(Num,1 ),1) > 0) and (instr(1,Digits,right(Num, 1),1) > 0) then
TwoDigits = "Good"
Else
TwoDigits = "Fail"
end if
Else
TwoDigits = "Fail"
End if
End Function
msgbox TwoDigits("10")
msgbox TwoDigits("100")
Function TwoDigits(Num)
dim Digits
Digits = "0123456789"
If len(Num)=2 then
if (instr(1,Digits,left(Num,1
TwoDigits = "Good"
Else
TwoDigits = "Fail"
end if
Else
TwoDigits = "Fail"
End if
End Function
msgbox TwoDigits("10")
msgbox TwoDigits("100")
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok...so I pulled a dummy move...
I didn't put
me.mytextbox in:
Len(xx)
I should have had:
Len(me.mytextbox.Text) <>2 then
Got it now....duh....
thanks!
I didn't put
me.mytextbox in:
Len(xx)
I should have had:
Len(me.mytextbox.Text) <>2 then
Got it now....duh....
thanks!
Here's an basic example when getting input from an inputbox
Open in new window