A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » New Users
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Test for no records returned



 
 
Thread Tools Display Modes
  #1  
Old April 30th, 2004, 12:32 AM
Brian C
external usenet poster
 
Posts: n/a
Default Test for no records returned

If I run a select query that may return no records how do
I test that there are no records?

I need to do this in code, i.e.code a select statement
followed by the test. Thanks in advance.
  #2  
Old April 30th, 2004, 02:29 AM
tina
external usenet poster
 
Posts: n/a
Default Test for no records returned

try

Dim Rst As DAO.Recordset, strSQL As String
strSQL = "SELECT...."
Set Rst = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
If Rst.BOF And Rst.EOF Then
'code that runs when recordset is empty
End If
Rst.Close
Set Rst = Nothing

or, instead

If DCount("AnyField", "QueryName") 1 Then
'code that runs when query has no records
End If

if your query is the SourceObject of a form, and you're wanting to check for
records on opening the form, you may be able to use RecordsetClone instead
of opening a DAO recordset. but i've never done it that way, so can't give
you specifics. if you figure it out, please post so i can learn too.

hth


"Brian C" wrote in message
...
If I run a select query that may return no records how do
I test that there are no records?

I need to do this in code, i.e.code a select statement
followed by the test. Thanks in advance.



  #3  
Old April 30th, 2004, 03:40 AM
Cheryl Fischer
external usenet poster
 
Posts: n/a
Default Test for no records returned

To add to Tina's post, if you want to test a query being used as the Record
Source of a form, you can put the following code in the Open event of the
form:


If Me.RecordsetClone.RecordCount = 0 Then
MsgBox "There are no records to review."
Cancel = True
End If

If there are no records in the query, the form does not open and the message
box is displayed for the user.

--

Cheryl Fischer, MVP Microsoft Access
Law/Sys Associates, Houston, TX


"Brian C" wrote in message
...
If I run a select query that may return no records how do
I test that there are no records?

I need to do this in code, i.e.code a select statement
followed by the test. Thanks in advance.



  #4  
Old April 30th, 2004, 04:58 AM
tina
external usenet poster
 
Posts: n/a
Default Test for no records returned

that's the (easier) one i didn't know - thanks, Cheryl!


"Cheryl Fischer" wrote in message
...
To add to Tina's post, if you want to test a query being used as the

Record
Source of a form, you can put the following code in the Open event of the
form:


If Me.RecordsetClone.RecordCount = 0 Then
MsgBox "There are no records to review."
Cancel = True
End If

If there are no records in the query, the form does not open and the

message
box is displayed for the user.

--

Cheryl Fischer, MVP Microsoft Access
Law/Sys Associates, Houston, TX


"Brian C" wrote in message
...
If I run a select query that may return no records how do
I test that there are no records?

I need to do this in code, i.e.code a select statement
followed by the test. Thanks in advance.





 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 01:24 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.