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!

Message Box

Status
Not open for further replies.

Minuet

Technical User
Dec 18, 2002
48
CA
How can I program Access to pop up an error message box upon LostFocus if an inappropriate value has been entered into certain field? For example, if [ApptType](my field) of [tbleAppointments](my form) = "IPD" then I want an error message to pop up with wording of my choice, but if the value entered is appropriate, then no message should pop up. Can anyone help me?
 

Create an On LostFocus event for the field ApptType

Then enter this code into the event procedure:

If Me.ApptType = "IPD" Then
MsgBox "The value, " & Me.ApptType & " is invalid!"

End If

And that shoud do it.
 
I strongly recommend that you not use the LostFocus event for this purpose. By the time the LostFocus event occurs, the data has already been saved in the record. If the user enters the bad data and then closes the form (without tabbing out of the field first), your MsgBox will pop up, but the form will close anyway, leaving the bad data in the database.

The BeforeUpdate event is provided for performing validations on field data. By using BeforeUpdate, you can stop the record update from happening (using its Cancel parameter) when the data is invalid. This will also stop the form from closing, if the user tries to close it without correcting the field.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Thanks so much for your help, Rick! I will follow your advice!
 
I tried putting the code from BGrego into the BeforeUpdate event, and it seems that the value is still saved in the field. What I probably want to do is create a validate macro . . . So, what is the proper way to write this condition: if ApptType from tblAppointments is "IPD" then a msg box pops up. I think I know how to do the msg box part, just not the condition.
 

If you just want a msgbox to pop up when someone just entered the invalid data of "IPD", then just fire the afterInsert event, calling the msgbox method. This will notify them of the invalid data, but the data will still enter the data to the table. If you want to disallow the data from being entered, use an unbound form. Use the same text boxes, but make them all unbound. Then insert a button to click that takes all of the values, verifies them, and if valid, it call the DoCmd.RunSQL method to insert the record into the tables, otherwise, it displays the message box then sets focus to the invalid field.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top