473,761 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check on empty record set in VBA

Dear reader

A record set can be empty because the condition in the query delivers no
records.

Is there a VBA code to check the status of a record set, record set empty
(no records) or with records?

Thanks for any help.

Simon van Beek
Nov 13 '05
30 35888

"David W. Fenton" <dX********@bwa y.net.invalid> wrote in message
news:Xn******** *************** **********@216. 196.97.142...
jb********@aol. com wrote in
news:11******** **************@ g43g2000cwa.goo glegroups.com:
S. van Beek wrote:
A record set can be empty because the condition in the query
delivers no records.

Is there a VBA code to check the status of a record set, record
set empty (no records) or with records?


If your recordset is rs (say) then

rs.movelast
if rs.recordcount = 0 then
'no rows
else
'rows returned
end if


The original poster does not specify DAO or ADO.

In DAO, you don't need to .MoveLast to know if the recordset is
empty. You only need to .MoveLast when you want an accurate count of
the records returned. If there are records returned, the recordcount
will always be 1 or more, and when none are returned, it will always
be zero, no matter whether you .MoveLast or not.

ADO is different. It returns different things. I don't use ADO, so I
can't tell you what it returns, but the help file for the ADO
recordset object ought to give you the information you need.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc


With ADO the RecordCount property is only available with a KeySet cursor.
There is no need to MoveLast to get the count.

Regardless of cursor type, if there are no records in the recordset, EOF
will be true immediately after opening the recordset.

rs.Open ("Select.... ")
If rs.EOF = True Then
' There were no records returned
End If
Nov 13 '05 #11
ADO 2.8 Documentation:

RecordCount Property
Indicates the number of records in a Recordset object.

Return Value
Returns a Long value that indicates the number of records in the
Recordset.

Remarks
Use the RecordCount property to find out how many records are in a
Recordset object. The property returns -1 when ADO cannot determine the
number of records or if the provider or cursor type does not support
RecordCount. Reading the RecordCount property on a closed Recordset
causes an error.

If the Recordset object supports approximate positioning or
bookmarks-that is, Supports (adApproxPositi on) or Supports
(adBookmark), respectively, return True-this value will be the exact
number of records in the Recordset, regardless of whether it has been
fully populated. If the Recordset object does not support approximate
positioning, this property may be a significant drain on resources
because all records will have to be retrieved and counted to return an
accurate RecordCount value.

NOTE: In ADO versions 2.8 and earlier, the SQLOLEDB provider fetches
all records when a server-side cursor is used despite the fact that it
returns True for both Supports (adApproxPositi on) and Supports
(adBookmark),

The cursor type of the Recordset object affects whether the number of
records can be determined. The RecordCount property will return -1 for
a forward-only cursor; the actual count for a static or keyset cursor;
and either -1 or the actual count for a dynamic cursor, depending on
the data source.

Nov 13 '05 #12
"lylefair" <ly******@yahoo .ca> wrote in
news:11******** **************@ g14g2000cwa.goo glegroups.com:
ADO 2.8 Documentation:

RecordCount Property
Indicates the number of records in a Recordset object.

Return Value
Returns a Long value that indicates the number of records in the
Recordset.

Remarks
Use the RecordCount property to find out how many records are in a
Recordset object. The property returns -1 when ADO cannot
determine the number of records or if the provider or cursor type
does not support RecordCount. Reading the RecordCount property on
a closed Recordset causes an error.

If the Recordset object supports approximate positioning or
bookmarks-that is, Supports (adApproxPositi on) or Supports
(adBookmark), respectively, return True-this value will be the
exact number of records in the Recordset, regardless of whether it
has been fully populated. If the Recordset object does not support
approximate positioning, this property may be a significant drain
on resources because all records will have to be retrieved and
counted to return an accurate RecordCount value.

NOTE: In ADO versions 2.8 and earlier, the SQLOLEDB provider
fetches all records when a server-side cursor is used despite the
fact that it returns True for both Supports (adApproxPositi on) and
Supports (adBookmark),

The cursor type of the Recordset object affects whether the number
of records can be determined. The RecordCount property will return
-1 for a forward-only cursor; the actual count for a static or
keyset cursor; and either -1 or the actual count for a dynamic
cursor, depending on the data source.


That doesn't seem to indicate whether it indicates 0 for an empty
recordset.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #13
"David W. Fenton" <dX********@bwa y.net.invalid> wrote in
news:Xn******** *************** ***********@216 .196.97.142:
"lylefair" <ly******@yahoo .ca> wrote in
news:11******** **************@ g14g2000cwa.goo glegroups.com:
ADO 2.8 Documentation:

RecordCount Property
Indicates the number of records in a Recordset object.

Return Value
Returns a Long value that indicates the number of records in
the Recordset.

Remarks
Use the RecordCount property to find out how many records are
in a Recordset object. The property returns -1 when ADO
cannot determine the number of records or if the provider or
cursor type does not support RecordCount. Reading the
RecordCount property on a closed Recordset causes an error.

If the Recordset object supports approximate positioning or
bookmarks-that is, Supports (adApproxPositi on) or Supports
(adBookmark), respectively, return True-this value will be
the exact number of records in the Recordset, regardless of
whether it has been fully populated. If the Recordset object
does not support approximate positioning, this property may
be a significant drain on resources because all records will
have to be retrieved and counted to return an accurate
RecordCount value.

NOTE: In ADO versions 2.8 and earlier, the SQLOLEDB
provider fetches all records when a server-side cursor is
used despite the fact that it returns True for both Supports
(adApproxPositi on) and Supports (adBookmark),

The cursor type of the Recordset object affects whether the
number of records can be determined. The RecordCount property
will return -1 for a forward-only cursor; the actual count
for a static or keyset cursor; and either -1 or the actual
count for a dynamic cursor, depending on the data source.


That doesn't seem to indicate whether it indicates 0 for an
empty recordset.


In my experience, and as claimed by Randy Harris earlier in this
thread, testing for .EOF always returns the correct answer upon
opening a recordset.

--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #14
Quintal <rq******@sympa tico.ca> wrote in
news:Xn******** **************@ 207.35.177.135:
In my experience, and as claimed by Randy Harris earlier in this
thread, testing for .EOF always returns the correct answer upon
opening a recordset.


I'm foggy on this, but I seem to remember that you must check both
EOF and BOF to be sure, and that there are some unique circumstances
where it can be an incorrect answer (though I don't remember if it
was a false positive or false negative).

I Googled a bit on this and couldn't find anything, so perhaps it's
just some cobwebs in my brain, but it was my reason for sticking
with checking .RecordCount instead.

Another reason I check .RecordCount is that I'd rather check a
single property rather than two, not that I think the performance
difference is enough to matter, but because it seems more elegant.

Of course, I never use ADO, so I have the advantag of using DAO's
more reliableBob .RecordCount property.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #15
"David W. Fenton" <dX********@bwa y.net.invalid> wrote in
news:Xn******** *************** ***********@216 .196.97.142:
Quintal <rq******@sympa tico.ca> wrote in
news:Xn******** **************@ 207.35.177.135:
In my experience, and as claimed by Randy Harris earlier in
this thread, testing for .EOF always returns the correct
answer upon opening a recordset.
I'm foggy on this, but I seem to remember that you must check
both EOF and BOF to be sure, and that there are some unique
circumstances where it can be an incorrect answer (though I
don't remember if it was a false positive or false negative).

The true test of an empty recordset is RS.BOF AND RS.EOF.
However, Immediately after opening, .BOF is *always* asserted,
so just testing for EOF is sufficient.
I Googled a bit on this and couldn't find anything, so perhaps
it's just some cobwebs in my brain, but it was my reason for
sticking with checking .RecordCount instead.

Another reason I check .RecordCount is that I'd rather check a
single property rather than two, not that I think the
performance difference is enough to matter, but because it
seems more elegant.
Your elegance and mine are not the same: even testing for both
properties is prettier in my mind than

If .RecordCount = 0 Then

IF .BOF and .EOF then

IF .EOF Then
Of course, I never use ADO, so I have the advantag of using
DAO's more reliableBob .RecordCount property.


--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #16

Bob Quintal wrote:
The true test of an empty recordset is RS.BOF AND RS.EOF.
However, Immediately after opening, .BOF is *always* asserted,
so just testing for EOF is sufficient.


No!
Try this using the name of a table that has records.

Sub temp()
Dim r As DAO.Recordset
Set r = DBEngine(0)(0). OpenRecordset(" Table1")
MsgBox r.BOF 'False
Set r = Nothing
End Sub

Nov 13 '05 #17
David W. Fenton wrote:
Quintal <rq******@sympa tico.ca> wrote in
news:Xn******** **************@ 207.35.177.135:

In my experience, and as claimed by Randy Harris earlier in this
thread, testing for .EOF always returns the correct answer upon
opening a recordset.

I'm foggy on this, but I seem to remember that you must check both
EOF and BOF to be sure, and that there are some unique circumstances
where it can be an incorrect answer (though I don't remember if it
was a false positive or false negative).

I Googled a bit on this and couldn't find anything, so perhaps it's
just some cobwebs in my brain, but it was my reason for sticking
with checking .RecordCount instead.


Aye, I've ran into this phenom a couple of times, both .eof and .bof
were false yet there were no records, where on Earth it thought the
cursor was I don't know. .RecordCount is more reliable in DAO and broken
in ADO.

Nov 13 '05 #18
"lylefair" <ly******@yahoo .ca> wrote

Bob Quintal wrote:
The true test of an empty recordset is RS.BOF AND RS.EOF.
However, Immediately after opening, .BOF is *always* asserted,
so just testing for EOF is sufficient.


No!
Try this using the name of a table that has records.

Sub temp()
Dim r As DAO.Recordset
Set r = DBEngine(0)(0). OpenRecordset(" Table1")
MsgBox r.BOF 'False
Set r = Nothing
End Sub

This doesn't show that using .EOF is insufficient, though.

Certainly, (If rs.EOF) will not accurately indicate an empty recordset if
you do something to move the cursor - but the point is, that at *opening* of
a recordset, (If rs.EOF) will accurately indicate if there are no records.
You should only need to test (If rs.BOF And rs.EOF) if you've moved the
cursor.

.... Right?
--
Darryl Kerkeslager
Nov 13 '05 #19
Yes.

Nov 13 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1763
by: perryche | last post by:
If a report yield 0 record, I will get an #error message on a calculation field. How to display a 0 instead of #error on that field? Thanks. Perry
3
4425
by: Andrew Banks | last post by:
I'm outputting data from a SQL server database to a web form (C#). How can I detect if a value is null in the database? Thanks
1
1311
by: Shapper | last post by:
Hello, I need to check if a textbox is returning an empty value. When I use Response.Write(myTextBox.Text) I get the written value. However, when I use the code: If myTextBox.Text Is Nothing Then Response.Write("Empty") End If
1
4330
by: Cesar Zapata | last post by:
Hi, I have a a bound subform and what i'm trying to do is do check if some criteria applies before saving the record and trigger a macro. basically this is what I got. Date Received InStock UnitPrice
8
2650
by: lmurgas | last post by:
FormA = List of organization records bound to table FormB = Tabbed form with details of organization and all other related entities, such as orders, contacts, invoices, (all as subforms bound to FormB on primary key) FormC = Pop-up to add/edit/delete a record from one of the associated entities. For example, there is a pop-up to add a new contact record to an Org. Current Behavior: 1. Open FormA and remains active 2. Double click on row...
6
2357
jinalpatel
by: jinalpatel | last post by:
I am using following code for searching records. 'Purpose: Build up the criteria string form the non-blank search boxes, and apply to the form's Filter. 'Notes: 1. We tack " AND " on the end of each condition so you can easily add more search boxes; _ we remove the trailing " AND " at the end. Dim strWhere As String 'The criteria string. Dim lngLen As...
9
6130
by: Dhiru1009 | last post by:
Hi guys, I am trying to build a user registration form using PHP and MYSQL but encountring a problem. When I click on submit with empty fields it adds records to database also it doesn't matter what information I put it always add records to database when I click on submit. What can I do to make sure user will not be able to add records to database until he enters the right information? I am posting my code for you guys to have a look...
0
1734
by: sumitdipsite2005 | last post by:
I am trying to use VB6 as a middleware between two 3rd party applications. "App. A" ----> VB6 ------> "App B" i am having no trouble sending data from VB to the "App B". But i am having some trouble in reading data from "App A".
1
2353
by: frensan | last post by:
Hello, Not sure if this is the right forum, apologies in advance if it isn't. I am using adodb to connect to oracle database. My VB script returns an empty record for a sql query, but when I issue the same query from SQL Developer or from sql command prompt, the query returns a row. The machine has MDAC 2.8 and Oracle 10 db. I read here http://www.mofeel.net/27-microsoft-public-data-ado/3080.aspx# that there is a known problem with MDAC...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.