Solved

to validate text box

Posted on 2000-05-15
4
198 Views
Last Modified: 2008-02-20
well i have a text box for which i need to validate the user to enter first 4 characters and then 5 numbers
the characters should be 4 and numbers should be 5
so please help
thanx
0
Comment
Question by:sonalchouhan
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 2810505
Use a masked edit box control, you can set the mask to be ">????99999" which will only allow 4 letters then 5 numbers, with the letters converted to upper case.
0
 
LVL 2

Expert Comment

by:BobbyOwens
ID: 2810508
Try setting the maxlength to 9. Then in the Validate event, do the following:

Private Sub txtTest_Validate(Cancel As Boolean)

  On Error GoTo txtTest_Validate_Error
 
  Dim intI          As Integer
  Dim strChar       As String
 
'Check length of string
  If Len(txtTest) <> 9 Then
    MsgBox "Invalid Length"
    Cancel = True
    Exit Sub
  End If
'Check 1st 4 characters are A-Z
  For intI = 1 To 4
    strChar = UCase(Mid$(txtTest, intI, 1))
'Check character is between 'A' and 'Z'
    If strChar < "A" Or strChar > "Z" Then
      MsgBox "Character " & intI & " is invalid"
      Cancel = True
      Exit Sub
    End If
  Next intI
'Check numerics
  For intI = 5 To 9
    strChar = UCase(Mid$(txtTest, intI, 1))
    If Not IsNumeric(strChar) Then
      MsgBox "Number " & intI & " is invalid"
      Cancel = True
      Exit Sub
    End If
  Next intI
 
txtTest_Validate_Exit:
  Exit Sub
 
txtTest_Validate_Error:
  MsgBox Err.Description, , "txtTest_Validate"
  Resume txtTest_Validate_Exit
 
End Sub



0
 
LVL 2

Accepted Solution

by:
richtsteig earned 10 total points
ID: 2810526
You can either

use the maskedit-Control with the mask-property set to "&&&&#####".
In this case the user isn't allowed to enter other than the predefined chracters

or

use the LoastFocus-Event of the text box to check the value when the user wants to set the focus to another control
(eg  

Sub Text1_LostFocus()

If Len(text1) <> 9 Then
   MsgBox("Invalid number of characters")
Else

  If not text1.text like "[a-z][a-z][a-z][a-z][a-z]####"
    MsgBox ("Invalid character format")
  End If

End Sub



or use the Change-Event to check every single character when entered

eg
Sub Text1_Change()
Dim i As Integer
Dim iChar as string
For i=1 To Len(Text1)
   iChar = UCase(Mid(Text1,i,1))
   if (i >= 1 and i <=4 and Not iChar >= "A" and iChar <= "Z") or
     (i >=5 and i<= 9 and Not IsNumeric(ichar))  then
         MsgBox("Invalid character at position " & i)
   End If
End Sub

or use the KeyPress-Event to validate a character before accepting it

0
 
LVL 2

Expert Comment

by:richtsteig
ID: 2810538
Tim, Bobby

Sorry bout that, but your typing rate is at a very high level......

you posted your comments while I was typing an answer, that is just a combination of your two comments

Sorry again

Peter
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 51
VBA color chart bars 12 79
Updates not working for MS Windows 7 12 154
Best way to parse out a json string in VB6? 10 116
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now