• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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
0
Ernest Grogg
Asked:
Ernest Grogg
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
GozrehCommented:
If Len(xx) <>2 Then
0
 
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 GroggAuthor 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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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 GroggAuthor 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 GroggAuthor 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now