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

Does query produce data?

Status
Not open for further replies.

FranS37

Programmer
Jun 6, 2002
59
US
I have a form that on the button click runs a query that outputs to an Excel spreadsheet. Is there an easy was to tell if the query produces no data so that so that I can display a msgbox instead?

Thanks.
 
OK,

Do the following in the code behind the button.

.....
dim fld as string
dim src as string
dim criteria as string

fld = &quot;[[<Primary Key>]]&quot;
src = &quot;[[<Source Query>]]&quot;
criteria = &quot;&quot;

If DCount(fld,src,criteria)>0 then
'The query is returning results so you can proceed with the Excel export
else
'No results returned so inform your users
end if
.....


You will have to exchange
<Primary Key> with the name of your Primary Key
<Source Query> with your query name.

This method assumes you are using a Saved Query.


Alec Doughty
Doughty Consulting P/L

&quot;Life's a competition. Play hard, but play fair&quot;
 

Alternatively, open a recordsetclose on the query first:

set rst=myquery.recordsetclone

and then check the clone

if rst.recordcount<1 then
'dont bother running code
exit sub
end if

'run your export code
blahblah


Just another option.


 
What do I dim the query as? How do I get .recordsetclone to be a property of the query?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top