473,395 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Counting recordsets

The problem I'm having is the rsa.RecordCount always returns a '1' when
I know there should be about 1600 records returned. I read somewhere
that one should use '.MoveLast' before recordcount but that returns ar
error 'Invalid Operation'. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I've got the following code (this is just a simplified example - I want
to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop

Nov 13 '05 #1
13 3138
The MoveLast should work, unless there are no records at all.

It is not very useful to open a forward-only recordset and jump to the end,
so try:
Set rsa = db.OpenRecordset(strsqla, dbOpenDynaset)
If rsa.RecordCount > 0 Then
rsa.MoveLast
MsgBox rsa.RecordCount
End If

I also notice you have a variable named Counter that is not Dim'd at the top
of the procedure, and your code seems to be setting this undeclared variant
to the letter O (which is probably treated as another undeclared empty
variant). You can avoid these problems by adding this to the top of every
module:
Option Explicit
To get Access to do this for you for future modules, choose Options from the
Tools menu (in the code window). On the Editor tab, click the box for:
Require Variable Declaration

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Regnab" <p.*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The problem I'm having is the rsa.RecordCount always returns a '1' when
I know there should be about 1600 records returned. I read somewhere
that one should use '.MoveLast' before recordcount but that returns ar
error 'Invalid Operation'. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I've got the following code (this is just a simplified example - I want
to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop

Nov 13 '05 #2
Change this part of your code:
MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop

To:
If rsa.RecordCount = 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount
--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com

If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!


"Regnab" <p.*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The problem I'm having is the rsa.RecordCount always returns a '1' when
I know there should be about 1600 records returned. I read somewhere
that one should use '.MoveLast' before recordcount but that returns ar
error 'Invalid Operation'. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I've got the following code (this is just a simplified example - I want
to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop

Nov 13 '05 #3
Allen
Why don'y you use the DCOUNT() funcion to check for records?
Bob

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:43***********************@per-qv1-newsreader-01.iinet.net.au...
The MoveLast should work, unless there are no records at all.

It is not very useful to open a forward-only recordset and jump to the end, so try:
Set rsa = db.OpenRecordset(strsqla, dbOpenDynaset)
If rsa.RecordCount > 0 Then
rsa.MoveLast
MsgBox rsa.RecordCount
End If

I also notice you have a variable named Counter that is not Dim'd at the top of the procedure, and your code seems to be setting this undeclared variant to the letter O (which is probably treated as another undeclared empty
variant). You can avoid these problems by adding this to the top of every
module:
Option Explicit
To get Access to do this for you for future modules, choose Options from the Tools menu (in the code window). On the Editor tab, click the box for:
Require Variable Declaration

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Regnab" <p.*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The problem I'm having is the rsa.RecordCount always returns a '1' when
I know there should be about 1600 records returned. I read somewhere
that one should use '.MoveLast' before recordcount but that returns ar
error 'Invalid Operation'. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I've got the following code (this is just a simplified example - I want
to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop


Nov 13 '05 #4

If you want an accurate recordcount then it is easier and frequently quicker
to do the following

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String
Dim Counter As Long

strsql = "SELECT Count(*) " & _
"FROM 3036023_A;"

Set db = DBEngine(0)(0)

Set rsa = db.OpenRecordset(strsql, dbOpenSnapshot)

MsgBox rsa.Fields(0) & ""
--
Terry Kreft

"Regnab" <p.*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The problem I'm having is the rsa.RecordCount always returns a '1' when
I know there should be about 1600 records returned. I read somewhere
that one should use '.MoveLast' before recordcount but that returns ar
error 'Invalid Operation'. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I've got the following code (this is just a simplified example - I want
to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop

Nov 13 '05 #5
"PC Datasheet" <no****@nospam.spam> wrote in message
news:Dr*****************@newsread1.news.atl.earthl ink.net...

To:
If rsa.RecordCount = 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount


Erm, I don't think so.
Nov 13 '05 #6
Yes, DCount() would be perfectly fine if you want to check for records, or
even DLookup() if you just want to know if there are any matches.

I assumed the aim of OP here was to learn how to walk through/handle
recordsets.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"scooper" <ro*******@optusnet.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
Allen
Why don'y you use the DCOUNT() funcion to check for records?
Bob

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:43***********************@per-qv1-newsreader-01.iinet.net.au...
The MoveLast should work, unless there are no records at all.

It is not very useful to open a forward-only recordset and jump to the

end,
so try:
Set rsa = db.OpenRecordset(strsqla, dbOpenDynaset)
If rsa.RecordCount > 0 Then
rsa.MoveLast
MsgBox rsa.RecordCount
End If

I also notice you have a variable named Counter that is not Dim'd at the

top
of the procedure, and your code seems to be setting this undeclared

variant
to the letter O (which is probably treated as another undeclared empty
variant). You can avoid these problems by adding this to the top of every
module:
Option Explicit
To get Access to do this for you for future modules, choose Options from

the
Tools menu (in the code window). On the Editor tab, click the box for:
Require Variable Declaration
"Regnab" <p.*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
> The problem I'm having is the rsa.RecordCount always returns a '1' when
> I know there should be about 1600 records returned. I read somewhere
> that one should use '.MoveLast' before recordcount but that returns ar
> error 'Invalid Operation'. And when I do the bottom loop, the counter
> happily clicks over the right number of times. Me no understand...
>
>
> I've got the following code (this is just a simplified example - I want
> to use the recordcount in an If Statement):
>
> Dim db As DAO.Database
> Dim rsa As DAO.Recordset
> Dim strsql As String
>
> strsql = "SELECT * " & _
> "FROM tblInductionLots;"
>
> Set db = DBEngine(0)(0)
> Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)
>
> MsgBox rsa.RecordCount
>
> Counter = O
> Do Until rsa.EOF
> Counter = Counter + 1
> rsa.MoveNext
> MsgBox Counter
> Loop

Nov 13 '05 #7
DCOUNT() is very slow compared to movelast on an open recordset.

"scooper" <ro*******@optusnet.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
Allen
Why don'y you use the DCOUNT() funcion to check for records?
Bob

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:43***********************@per-qv1-newsreader-01.iinet.net.au...
The MoveLast should work, unless there are no records at all.

It is not very useful to open a forward-only recordset and jump to the

end,
so try:
Set rsa = db.OpenRecordset(strsqla, dbOpenDynaset)
If rsa.RecordCount > 0 Then
rsa.MoveLast
MsgBox rsa.RecordCount
End If

I also notice you have a variable named Counter that is not Dim'd at the

top
of the procedure, and your code seems to be setting this undeclared

variant
to the letter O (which is probably treated as another undeclared empty
variant). You can avoid these problems by adding this to the top of every module:
Option Explicit
To get Access to do this for you for future modules, choose Options from

the
Tools menu (in the code window). On the Editor tab, click the box for:
Require Variable Declaration

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Regnab" <p.*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The problem I'm having is the rsa.RecordCount always returns a '1' when I know there should be about 1600 records returned. I read somewhere
that one should use '.MoveLast' before recordcount but that returns ar
error 'Invalid Operation'. And when I do the bottom loop, the counter
happily clicks over the right number of times. Me no understand...
I've got the following code (this is just a simplified example - I want to use the recordcount in an If Statement):

Dim db As DAO.Database
Dim rsa As DAO.Recordset
Dim strsql As String

strsql = "SELECT * " & _
"FROM tblInductionLots;"

Set db = DBEngine(0)(0)
Set rsa = db.OpenRecordset(strsqla, dbOpenForwardOnly)

MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop



Nov 13 '05 #8

"PC Datasheet" <no****@nospam.spam> schreef in bericht news:Dr*****************@newsread1.news.atl.earthl ink.net...
Change this part of your code:
MsgBox rsa.RecordCount

Counter = O
Do Until rsa.EOF
Counter = Counter + 1
rsa.MoveNext
MsgBox Counter
Loop

To:
If rsa.RecordCount = 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount
Incorrect answer (as Keith already mentioned) and still continuously advertising as a so-called resource ???
If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
These 1000 (if at all a real figure..) is only the result of
-- 4 years abusing the newsgroups.
-- 4 years blatantly advertising and job hunting.

You only care about making money, and you act as if the groups are your private hunting ground.
So why would ANYBODY ever trust a person like you and hire you?
************************************************** ******
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!


Need a psychiatrist ... Contact me!

Arno R

Nov 13 '05 #9
"paii, Ron" <pa**@packairinc.com> wrote in
news:tr******************************@athenet.net:
DCOUNT() is very slow compared to movelast on an open recordset.


SELECT COUNT(*)

will be faster than either of them.

If you want eo know exactly how many records are in a recordset that
you're going to use for some other purpose, then using .MoveLast to
check the number of records is valid.

On the other hand, if you want to see if your recordset returns some
records, you don't need an exact count, in any event -- you need
only check if .RecordCount > 0.

If you need to know exactly how may records you're going to act
upon, then there's no reason to .MoveLast first, just do your
operation and when finished, you should be at the last record, and
then .RecordCount will be accurate.

So, I'm basically having some difficulting imagining a scenrio where
you'd need the exact recordcount that couldn't be very efficiently
done ay anything *other* than DCount(). DCount() is only really a
valid choice if all you need is a mere count of the records
returned, and should have no advantages over SELECT COUNT(*).

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #10
"Keith W" <he**@there.com> wrote in
news:43**********@glkas0286.greenlnk.net:
"PC Datasheet" <no****@nospam.spam> wrote in message
news:Dr*****************@newsread1.news.atl.earthl ink.net...

To:
If rsa.RecordCount = 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount


Erm, I don't think so.


Why not? It looks completely valid to me.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #11
On Thu, 10 Nov 2005 15:33:12 -0600, "David W. Fenton" <dX********@bway.net.invalid> wrote:
"Keith W" <he**@there.com> wrote in
news:43**********@glkas0286.greenlnk.net:
"PC Datasheet" <no****@nospam.spam> wrote in message
news:Dr*****************@newsread1.news.atl.earthl ink.net...

To:
If rsa.RecordCount = 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount


Erm, I don't think so.


Why not? It looks completely valid to me.


Because if rsa.RecordCount = 0 you already know there are no records in the recordset so why do a MoveLast?

It should be -

If rsa.RecordCount > 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount
Nov 13 '05 #12
Wayne Gillespie <be*****@NOhotmailSPAM.com.au> wrote in
news:uq********************************@4ax.com:
On Thu, 10 Nov 2005 15:33:12 -0600, "David W. Fenton"
<dX********@bway.net.invalid> wrote:
"Keith W" <he**@there.com> wrote in
news:43**********@glkas0286.greenlnk.net:
"PC Datasheet" <no****@nospam.spam> wrote in message
news:Dr*****************@newsread1.news.atl.earthl ink.net...

To:
If rsa.RecordCount = 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount

Erm, I don't think so.


Why not? It looks completely valid to me.


Because if rsa.RecordCount = 0 you already know there are no
records in the recordset so why do a MoveLast?

It should be -

If rsa.RecordCount > 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount


Well, er, why didn't you just say that in your reply?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #13
On Fri, 11 Nov 2005 20:50:46 -0600, "David W. Fenton" <dX********@bway.net.invalid> wrote:

Erm, I don't think so.

Why not? It looks completely valid to me.


Because if rsa.RecordCount = 0 you already know there are no
records in the recordset so why do a MoveLast?

It should be -

If rsa.RecordCount > 0 Then
rsa.MoveLast
End If
MsgBox rsa.RecordCount


Well, er, why didn't you just say that in your reply?


Well, er, maybe because I didn't post a reply.

Nov 13 '05 #14

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

Similar topics

3
by: Ron Nolan | last post by:
Re: Access 2000 How can I loop through a folder on the user's hard drive, and check to see if it is an .mdb file. If it is an .mdb, do some stuff, else go to the next filename? Can this be...
6
by: Steve Jorgensen | last post by:
I keep having problems in which ADO disconnected recordset work under some circumstances, but lose all their data at other times, having no rows or fields, though the recordset object still exists....
2
by: Pieter Linden | last post by:
The answer to this one is probably "test it yourself and find out!", but I'll ask anyway. Pursuant to my previous question - sending separate recordsets to Word using the CreateTableFromRecordset...
1
by: lakshmi | last post by:
Hi all, I recently rewrote a data intensive C++ program in C#. The C++ program was traversing 3 recordsets that were all open at the same time. I replaced those 3 recordsets with 3 .NET data...
16
by: Randy Harris | last post by:
I was inspired by the recent discussion of returning multiple recordsets to ADO from a stored procedure. (Amazed is probably more accurate). I asked about how to accomplish same with Oracle and...
2
by: allyn44 | last post by:
Hello, I have built a serch form for users to edit records. I only want them to pull up the record they need, and I want to check for nulls. There should not be dupes becasue the underlying...
24
by: Donald Grove | last post by:
I want to populate an array with values from an ado recordset (multiple rows) I use the absolute position of the cursor in the recordset to define the row of my array to be populated. I have a...
4
by: mrmagoo | last post by:
I'm building a vb.net Forms project that is getting data from a SQL Server database. One of the main goals of the project is to be really responsive to events, such as textbox change events. I...
4
by: rdemyan via AccessMonster.com | last post by:
Can someone help me with creating code that will look for DAO recordsets in modules and then check to see if the recordset is also closed in the module. All of my recordsets are of the form rs*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.