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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional Statements 1

Status
Not open for further replies.

jbento

Technical User
Jul 20, 2001
573
US
I hope someone can help with this.

I have a table (tbl_test). Within that table I have fields. The two fields, which are combo boxes, I want to deal with in this example is: "field1" and "field2". tbl_test is associated with a form named "form1". I want to be able to make sure the user types or chooses a value in at least one of those fields with out leaving the form. The user doesn't have to have text in both, but at least one. Example: If the user has text in "field1" and not "field2", he can leave the form, or vice versa, if he has text in "field2", but not "field1", he can leave the form, but if he does not have text in either field, I want a msgbox to let him no he needs to fill in at least one field.

Can someone help me with code for this. I tried some variations, and I can't get my code to work.

Thanks
 
Code your form's Before Update event.

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me.field1) And IsNull(Me.field2) Then
MsgBox "You must enter a value in field 1 or 2", vbExclamation
Cancel = True
End If

End Sub


HTH
 
I will try this right now. Thanks.

Jerome
 
sko,
I like the code, but there is one thing. The "Before Update", does not work with this, because it allows the user to go to another record if they click the record selector. I got it to work on the On Close function when I click the "x" to close the record. I want it to work no matter how they try to exit the current record they are on, if the conditions aren't met. I even created a command button with a macro to close the form and go to another form, making the form have the code with the "On Close" function, but it still didn't work. It went straight to the other form without running the code. Do you have any ideas on this?

Jerome
 
sko,
Never mind. I got it too work. It was my fault. Your CODE WORKS LIKE A CHARM. THANKS VERY MUCH.

Jerome
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top