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!

Correct way to perform multiple validations?

Status
Not open for further replies.

JockVSJock

Technical User
Jun 28, 2001
59
US


New to VB, and I've created a form that has ten entry
fields that I only want to accept numbers. If text is entered, a message box pops up, and shifts the focus to the field where the error occured.

I don't think the way I have it setup is correct.



dbltxt10 = Val(txt10.Text)
dbltxt9 = Val(txt9.Text)
dbltxt8 = Val(txt8.Text)
dbltxt7 = Val(txt7.Text)
dbltxt6 = Val(txt6.Text)
dbltxt5 = Val(txt5.Text)
dbltxt4 = Val(txt4.Text)
dbltxt3 = Val(txt3.Text)
dbltxt2 = Val(txt2.Text)
dbltxt1 = Val(txt1.Text)


'Data check and do the math
'with an error message output if
'data entered is not numeric


If Not IsNumeric(txt1.Text) Then
MsgBox "Etner Only Numeric Data!!!", vbCritical, "Error!!!!"



I'm repeating this for all of the ten fields. Do I need to have dbltxt1, instead of txt1.Text? Or is this even setup right? The book that I have doesn't provide a good example, and I haven't found any so far on the internet.


thanks


 
Things will become easier if you create a control array of text boxes. After that place the following code in your validation routine.
___

Dim N As Integer, dblX(9) As Double
For N = 0 To 9
If IsNumeric(txt(N).Text) Then
dblX(N) = Val(txt(N).Text)
Else
Msgbox "Please enter numeric data."
txt(N).SetFocus
Exit Sub
End If
Next

'now do something with data in dblX() array
 
Make it so:
Private Sub Text1_KeyPress(KewAscii As Integer)

'You allow will apply vbBack
If Chr(KeyAscii)=vbBack Then Exit Sub
If Not Is Numeric(Chr(KeyAscii)) Then
Beep
MsgBox"Enter only numeric data",vbCritical,"Error!!!"
KeyAscii=0
Text1.Text=""
End If
End Sub
 


I'm very new to VB, so right now I want to keep it simple and
just do what I have, not get into arrays yet.

I only want to work with the code that I have.

thanks





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top