Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I validate

Status
Not open for further replies.

2ks

Technical User
Jan 13, 2007
54
GB
Hi all

I need a way of validating a string of between 9 and 11 characters. Unfortunately the string can be totally random with numbers or text appearing in any of the digits. eg 87878E78H or H2454565456.

How can i ensure that this is possible in VB user form text box?

Many many thanks

Nikk
 
You can check the length of the value in the text box to make sure its at least 9 characters and you can limit it to no more then 11

Len(txtYourTextbox.Text)

The location of your code is going to depend on when and where you want to do your checks.

On the YourTextBox_Lostfocus you could use an If Then

If Len(txtYourTextbox.Text) <=9 then
MsgBox(WarningHere)
txtYourTextbox.setfocus
Exit Sub

And the keypress event could use another

If Len(txtYourTextbox.Text) <=11 then

KeyAscii = 0 or KeyCode = 0 depending on what that function gets passed.

Exit Sub

That should get you going.
 



Hi,
Code:
select case Len(youruserform.yourtextbox.text)
   Case 9 to 11
      'what to do if string meets criteria

   case else
      'what to do if string fails criteria

End select


Skip,

[glasses] [red][/red]
[tongue]
 
Seems to me that:
I need a way of validating a string of between 9 and 11 characters.
needs some explaining.

That is, unless the length (9 - 11 characters) is the only validation required. If the length is the only issue that needs validation then the post responses will do that.

Could you please confirm if your question has been answered?

Don't know...I suspect not, but hey I have been wrong many times.

Gerry
My paintings and sculpture
 
Looks like a prime candidate for regular expression matching. In Tools->References check the box for Microsoft VBScript Regular Expressions 5.5, then
Code:
Function MyValidate(s As String) As Boolean

    Dim r As RegExp
    Set r = New RegExp
    r.Pattern = "^[A-Z0-9]{9,11}$"
    MyValidate = r.test(s)
    
End Function
This example allows any combination of numbers and upper case letters between 9 and 11 characters.

HTH

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top