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

Error Message when no records are found

Status
Not open for further replies.

mgbeye

Programmer
May 30, 2001
47
US
I have a form that loads information from an internal query. Meaning I didn't create a query seperatly...it doesn't show up on the query list. It prompts for the begining and ending date then loads all records in that range. What I want to do is have and error message displayed when no records are found. I am not quite sure what event I should put it in...or exactly how to code it.

THANKS!!
 
I'm not sure what an internal query is so I'm going to suggest that you might have made this form with the wizard and you utilized a query as one of you source tables for the form's recordsource. What I would do is open the form in design view and look at the SQL statement that is the recordsource. Save it so that it is a saved object (a query) in the database. On the form's On Open event write this code:

Private Sub Form_Open(Cancel as Integer)
DIm db as dao.database
dim rs as dao.recordset

set db = currentdb()

set rs = db.openrecordset(MySavedQuery, dbSnapshot)

if rs.bof or rs.eof then
Msgbox "Sorry, there are no records matching that date range", vbExclamation
set rs = nothing
set db = nothing
Cancel = -1
exit sub

end if

End sub

This will cancel the form if there are no records.

Hope that helps

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top