error Message for empty query (1 Viewer)

habbabub

Registered User.
Local time
Yesterday, 21:22
Joined
Jan 24, 2008
Messages
73
i have a form which has as button that opens a query
how can i make it so that if there arent any results from the query then a error message will pop up saying "no results found" and the table dosent show?
 

wazz

Super Moderator
Local time
Today, 12:22
Joined
Jun 29, 2004
Messages
1,711
one way is to use the DCount function. create a query for your results and count the results *from the query*. if DCount = 0 then msgbox and exit sub.
 

habbabub

Registered User.
Local time
Yesterday, 21:22
Joined
Jan 24, 2008
Messages
73
how can i get into VB for a query???
 

neileg

AWF VIP
Local time
Today, 05:22
Joined
Dec 4, 2002
Messages
5,975
You need to test the DCount in the code that runs from your button. Look at Access help on DCount. You usually build the parameters for DCount so that it returns the same dataset as your query.
 

MStef

Registered User.
Local time
Today, 05:22
Joined
Oct 28, 2004
Messages
2,251
Another way;
Make a Report on the query, and use "On No Data" properties (in report).
Look at "DemoRptNoDataA2000.mdb" (attachment).
 

Attachments

  • DemoRptNoDataA2000.zip
    16.2 KB · Views: 234

habbabub

Registered User.
Local time
Yesterday, 21:22
Joined
Jan 24, 2008
Messages
73
Another way;
Make a Report on the query, and use "On No Data" properties (in report).
Look at "DemoRptNoDataA2000.mdb" (attachment).

im looking for an error message for the qurey itself, not a report
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 05:22
Joined
Sep 12, 2006
Messages
15,641
what they mean is, when you click your button, your code will be

docmd.openquery ("qryname")


so instead, have

if dcount("*","qrynaME")=0 then
MSGBOX("NO ITEMS")
else
docmd.openquery("qryname")
end if
 

habbabub

Registered User.
Local time
Yesterday, 21:22
Joined
Jan 24, 2008
Messages
73
my query has a critera so it is based on that, so if the criteria dosent match anything i want the error message box to come up

the code above dosent allow this
 

wazz

Super Moderator
Local time
Today, 12:22
Joined
Jun 29, 2004
Messages
1,711
"if the criteria dosent match anything i want the error message box to come up"

it will. if the query works the way you want then...it works all the time. ask the query how many records there are any time you want. if there are records, do something. if there are no records supply the msg.

if you are saying you want to *change* the criteria every time that's different (and you haven't said this).
 

habbabub

Registered User.
Local time
Yesterday, 21:22
Joined
Jan 24, 2008
Messages
73
yes, i have a criteria that changes, according to what the user wants to type in .. so how can i do this?
 

neileg

AWF VIP
Local time
Today, 05:22
Joined
Dec 4, 2002
Messages
5,975
I usually prefer a combo box for user input so they can't enter an invalid criterion.
 

wazz

Super Moderator
Local time
Today, 12:22
Joined
Jun 29, 2004
Messages
1,711
if you're not sure what neil's talking about you should search this site. not trying to be difficult - there are several threads discussing this and it'll probably be faster to search.
 

Users who are viewing this thread

Top Bottom