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

HOW DO CHECK IF QUERY RETURNS ANY RECORS 4

Status
Not open for further replies.

villica

Programmer
Feb 25, 2000
332
0
0
CA
I have a report which the the control source is a query, but this query is based on other queries. How do I check if the query result is greater than zero before displaying the report. If there is not data I want a mesage no data and no display the report

thanks everyone for any suggetions

 
Not the suggestion that you probably wanted but... you could code the report's "On No Data" event to display a message and close the report right away. Rob Marriott
rob@career-connections.net
 
This function will count the number of records in your query. Is this enough to get you on the right track?

Function CountRecords(pstrname As String) As Long

'this function counts the number of records in a query

Dim myDB As DATABASE
Dim ctr As Integer, num As Integer
Dim myrs As Recordset

On Error GoTo points_Err:

Set myDB = CurrentDb
Set myrs = myDB.OpenRecordset("select * from [" & pstrname & "]")

If myrs.BOF = True And myrs.EOF = True Then
num = 0
Else
myrs.MoveFirst
myrs.MoveLast
num = myrs.RecordCount
End If
myrs.Close
CountRecords = num
Exit Function


 
Hello I posted this question a while a go and then requirement was cancel but now is back againg. I would like to thank Raskew for your reply. If Raskew is not on this forum any longer I am hopping someone else can help me figure out this code. First of all what is pstrname and where do I call this function from.

thanks anyone.


 
Hello,

Pstrname is a parameter passed to the function that represents the name of the table the code requires to build the SQL statement.

I must add that the EOF check is not necessary. The BOF is sufficient. Here is another way I would write the same code...

Set myrs = _
currentdb.OpenRecordset("select * from [" & pstrname & "]")

num = 0

If Not myrs.BOF Then
myrs.MoveLast
num = myrs.RecordCount
myrs.MoveFirst
End If

It does the same thing, but less typing.


Gary
gwinn7
 
Thank you for your reply, where do I call this function from. I Have a button that opens the report, is this the place do it on the on clikc even. thank you
 
I like to use:

MyVariable = Dcount("QueryFieldName","MyQueryName")

Then you can 'If' or 'Select Case' on MyVariable
(it will contain a 0 if there are no records...)
 
There is a very easy way to do this. Use

If IsNull(DLookup("FieldName","QueryName")) then "Close Report" else "Run Report"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top