View Full Version : Message Box when query return is empty.


pipeline
02-06-2000, 09:07 AM
I am relatively new to Access (and VBA). I have built an application complete with a series of nested switchboards, to track general Project Information, Notes, Issues, Deliverable Information, etc. For my deliverable information, I use a form that is bound(?) to a query to display the information for a project. The query asks for the project number and then the results are displayed on the form. So far so good. Very often in the inital stages of a project, there are no deliverables in preparation (e.g. data gathering phase, etc.) Right now, when I query a project's deliverables, and none exist, I get a blank form returned. What I would like to be able to do is, if there are no deliverables for a project, I would like to have a message box returned with the Message "No deliverables exist for this project.", rather than have the blank form displayed on the screen. Clicking on (OKAY) on the message box would return to the previous menu. I am hoping this is relatively simple to do. Any help, insight on how to make this happen would be greatly appreciated. Thanks.

Axis
02-06-2000, 04:55 PM
Use the DCount function to count the number of records returned by the query. If the query comes up empty, a message box pops up saying no records found (or whatever you want it to say). Here's some simple code that you can put in before you open the report:
========================================
Dim strWhat as String, inCnt as Integer
DoCmd.Hourglass True
strWhat = "(([SQL criteria]))"
intCnt = DCount("[Key Field]", "[Table Name]", strWhat)
If intCnt = 0 Then
MsgBox "There are no records that match this query.", 64, "Search Results"
DoCmd.Hourglass False
Exit Sub
End If
DoCmd.Hourglass False

========================================

For [SQL criteria] insert the SQL statement from your query, minus the "SELECT"
For [Key Field] insert the name of the key index field from your table
For [Table Name] insert the name of the name that's being queried