Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Valiation of Form Data 1

Status
Not open for further replies.

Neak1

Computer
Sep 10, 2001
12
0
0
US
I have a form the contains about 30 fields. Some of these fields are required at all times, and the others are required under some kind of condition. What I do know is that you can control this in some way by using the validation rule which is listed on the properties window of every field. What I need to know is how to force the user to make relational entries. EX. There is a field called Address, if Address is coded then Zip must also be coded. As of now the user is only required to enter in Address when one is given so they can skip this field in some cases, but in those cases were the Address is given and must be coded the user is still allowed to skip over the Zip field. How can I prevent this from happening. Please Help! I have tried many types of coding an nothing has helped.
 
Replies continue below

Recommended for you

The following code should solve your problem:
To use this example, copy this sample code to the Declarations portion of a form "UserForm1". Make sure that the form contains the following controls:

**two forms called UserForm1 and UserForm2-
you don't necessarily need UserForm2 but you should omit any reference to it in that case.
** A command button on UserForm1 called CommandButton1-essentially your "OK" button.
** A textbox on UserForm1 called TextBox1-A box for a street address
** A textbox on UserForm1 called TextBox2-A box to enter a zip code.
** A textbox on UserForm1 called TextBox3-Really, it can be any control that is next on the TAB order after TextBox2
** A label on UserForm1 called Label1- Caption should be "Zip Code"
** TAB order should be TextBox1->TextBox2->TextBox3->Commandbutton1


Private Sub CommandButton1_Click()
If (TextBox1.Value <> &quot;&quot; And TextBox2.Value = &quot;&quot;) Then
HiliteZip
TextBox2.SetFocus
Else
NormalZip
'Get info from UserForm1 here and go to a new userform
UserForm2.Show
End If
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If (TextBox1.Value <> &quot;&quot; And TextBox2.Value = &quot;&quot;) Then
Cancel = True
HiliteZip
End If
End Sub

Private Sub TextBox3_Enter()
NormalZip
End Sub

Private Sub HiliteZip()
Label1.Caption = &quot;*Zip Code required&quot;
Label1.ForeColor = RGB(255, 0, 0)
TextBox2.BackColor = RGB(255, 255, 0)
End Sub

Private Sub NormalZip()
Label1.Caption = &quot;Zip Code&quot;
Label1.ForeColor = RGB(0, 0, 0)
TextBox2.BackColor = RGB(255, 255, 255)
End Sub


This code should notify the user that he or she needs to enter a zip code regardless if the user uses the TAB or mouse button or both to move from one field to another.
 
Status
Not open for further replies.
Back
Top