Link to home
Start Free TrialLog in
Avatar of military donut
military donutFlag for United States of America

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
ASKER CERTIFIED SOLUTION
Avatar of Gozreh
Gozreh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how are you getting your input? From an input box? text box on a form?

Here's an basic example when getting input from an inputbox
        tempString = InputBox("Enter Two Digit Value: ")
        Do Until Len(Trim(tempString)) = 2
                tempString = InputBox("Enter Two Digit Value: ", "New Value")
        Loop

Open in new window

Avatar of military donut

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...

?
On Before Update you have to use .Text
Len(xx.Text)
I would place your code in the After_Update event instead of the before_update event.
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?
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")
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!