PC Review


Reply
Thread Tools Rate Thread

Code to count records

 
 
=?Utf-8?B?U2hlbA==?=
Guest
Posts: n/a
 
      3rd Mar 2005
Does anybody out there have any VBA code that will allow me to count the
number of records returned by a query?

--
Thanks in advance!
Shel
 
Reply With Quote
 
 
 
 
RD
Guest
Posts: n/a
 
      3rd Mar 2005
On Thu, 3 Mar 2005 12:29:05 -0800, "Shel"
<(E-Mail Removed)> wrote:

>Does anybody out there have any VBA code that will allow me to count the
>number of records returned by a query?


DAO:

Dim db As DAO.Database, rs As DAO.Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset("Query Name")
Debug.Print rs.RecordCount


There is a very involved example in the Help file. I'll post that if
you like.

HTH,
RD

 
Reply With Quote
 
=?Utf-8?B?U2hlbA==?=
Guest
Posts: n/a
 
      3rd Mar 2005
in the VBA help file? Yes, please post it. I would be very thankful.

Thanks
Shel

"RD" wrote:

> On Thu, 3 Mar 2005 12:29:05 -0800, "Shel"
> <(E-Mail Removed)> wrote:
>
> >Does anybody out there have any VBA code that will allow me to count the
> >number of records returned by a query?

>
> DAO:
>
> Dim db As DAO.Database, rs As DAO.Recordset
>
> Set db = CurrentDb()
> Set rs = db.OpenRecordset("Query Name")
> Debug.Print rs.RecordCount
>
>
> There is a very involved example in the Help file. I'll post that if
> you like.
>
> HTH,
> RD
>
>

 
Reply With Quote
 
John Vinson
Guest
Posts: n/a
 
      3rd Mar 2005
On Thu, 3 Mar 2005 12:29:05 -0800, "Shel"
<(E-Mail Removed)> wrote:

>Does anybody out there have any VBA code that will allow me to count the
>number of records returned by a query?


nRecords = DCount("*", "queryname")

John W. Vinson[MVP]
 
Reply With Quote
 
RD
Guest
Posts: n/a
 
      3rd Mar 2005
On Thu, 3 Mar 2005 13:17:04 -0800, "Shel"
<(E-Mail Removed)> wrote:

>in the VBA help file? Yes, please post it. I would be very thankful.
>
>Thanks
>Shel
>
>"RD" wrote:
>
>> On Thu, 3 Mar 2005 12:29:05 -0800, "Shel"
>> <(E-Mail Removed)> wrote:
>>
>> >Does anybody out there have any VBA code that will allow me to count the
>> >number of records returned by a query?

>>
>> DAO:
>>
>> Dim db As DAO.Database, rs As DAO.Recordset
>>
>> Set db = CurrentDb()
>> Set rs = db.OpenRecordset("Query Name")
>> Debug.Print rs.RecordCount
>>
>>
>> There is a very involved example in the Help file. I'll post that if
>> you like.
>>
>> HTH,
>> RD
>>
>>


Here 'tis:

RecordCount Property Example

This example demonstrates the RecordCount property with different
types of Recordsets before and after they're populated.

Sub RecordCountX()

Dim dbsNorthwind As Database
Dim rstEmployees As Recordset

Set dbsNorthwind = OpenDatabase("Northwind.mdb")

With dbsNorthwind
' Open table-type Recordset and show RecordCount
' property.
Set rstEmployees = .OpenRecordset("Employees")
Debug.Print _
"Table-type recordset from Employees table"
Debug.Print " RecordCount = " & _
rstEmployees.RecordCount
rstEmployees.Close

' Open dynaset-type Recordset and show RecordCount
' property before populating the Recordset.
Set rstEmployees = .OpenRecordset("Employees", _
dbOpenDynaset)
Debug.Print "Dynaset-type recordset " & _
"from Employees table before MoveLast"
Debug.Print " RecordCount = " & _
rstEmployees.RecordCount

' Show the RecordCount property after populating the
' Recordset.
rstEmployees.MoveLast
Debug.Print "Dynaset-type recordset " & _
"from Employees table after MoveLast"
Debug.Print " RecordCount = " & _
rstEmployees.RecordCount
rstEmployees.Close

' Open snapshot-type Recordset and show RecordCount
' property before populating the Recordset.
Set rstEmployees = .OpenRecordset("Employees", _
dbOpenSnapshot)
Debug.Print "Snapshot-type recordset " & _
"from Employees table before MoveLast"
Debug.Print " RecordCount = " & _
rstEmployees.RecordCount

' Show the RecordCount property after populating the
' Recordset.
rstEmployees.MoveLast
Debug.Print "Snapshot-type recordset " & _
"from Employees table after MoveLast"
Debug.Print " RecordCount = " & _
rstEmployees.RecordCount
rstEmployees.Close

' Open forward-only-type Recordset and show
' RecordCount property before populating the
' Recordset.
Set rstEmployees = .OpenRecordset("Employees", _
dbOpenForwardOnly)
Debug.Print "Forward-only-type recordset " & _
"from Employees table before MoveLast"
Debug.Print " RecordCount = " & _
rstEmployees.RecordCount

' Show the RecordCount property after calling the
' MoveNext method.
rstEmployees.MoveNext
Debug.Print "Forward-only-type recordset " & _
"from Employees table after MoveNext"
Debug.Print " RecordCount = " & _
rstEmployees.RecordCount
rstEmployees.Close

.Close
End With

End Sub

 
Reply With Quote
 
David C. Holley
Guest
Posts: n/a
 
      4th Mar 2005
Its a bit late, so I may not have this entirely correct but something to
the effect of...

Set qdf = CurrentDatabase.QueryDefs(queryName)
Set rs = qdf.openrecordset (dbForwardOnly)
if not rs.EOF then
rs.movelast
Debug.print rs.recordCount
Else
Debug.print "No records retured"
end if
set rs = nothing
set qdf = nothing

MVP's how did I do off the top of my head?
(Assuming there are no query parameters)

David H

Shel wrote:
> Does anybody out there have any VBA code that will allow me to count the
> number of records returned by a query?
>

 
Reply With Quote
 
=?Utf-8?B?U2hlbA==?=
Guest
Posts: n/a
 
      4th Mar 2005
This worked exactly as I wanted! THANK YOU VERY MUCH!

"John Vinson" wrote:

> On Thu, 3 Mar 2005 12:29:05 -0800, "Shel"
> <(E-Mail Removed)> wrote:
>
> >Does anybody out there have any VBA code that will allow me to count the
> >number of records returned by a query?

>
> nRecords = DCount("*", "queryname")
>
> John W. Vinson[MVP]
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count records using count(*) in a chart report FL Microsoft Access Reports 3 3rd Dec 2009 03:51 PM
RE: How to count records using count(*) in a chart report FL Microsoft Access Reports 3 1st Dec 2009 11:31 PM
enter a count function to count records? Kimberly Microsoft Access Queries 11 14th May 2008 07:39 AM
How to use count of records in code? =?Utf-8?B?U25vd3NyaWRl?= Microsoft Excel Programming 1 5th Sep 2006 11:50 AM
Access : VBA code : compacting DB + count records of a table Ulrik Beersaerts Microsoft Access Getting Started 1 13th Sep 2004 10:06 AM


Features
 

Advertising
 

Newsgroups
 


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