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
Ernest GroggSecurity Management InfoSecAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GozrehCommented:
If Len(xx) <>2 Then
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony BerenguelCommented:
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

0
Ernest GroggSecurity Management InfoSecAuthor Commented:
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...

?
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

GozrehCommented:
On Before Update you have to use .Text
Len(xx.Text)
0
Anthony BerenguelCommented:
I would place your code in the After_Update event instead of the before_update event.
0
Ernest GroggSecurity Management InfoSecAuthor Commented:
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?
0
Glenn MadineSystems AnalystCommented:
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")
0
PatHartmanCommented:
Just keeps giving me the same msgbox over and over

Open in new window

Did you change "xx" to be the actual control name?
Also, given your description of the values, this field should be defined as text.  Whenever you store a single digit number, Access will drop the leading zero because leading zeros are not used for numeric values.  They only appear in strings.

If you can't change the data type, you'll need to put the leading zero back before testing.

If Len(Format(xx, "00"))<>2 then
      msgbox "Try Again"
      Cancel=True
      Exit Sub
 End If

Open in new window

0
Ernest GroggSecurity Management InfoSecAuthor Commented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.