Solved

VBA Access - Count records of query, in another database

Posted on 2011-02-24
4
602 Views
Last Modified: 2012-05-11
Dear Experts,

Can you please have a short look on the attached Code part, I would like to count the number of records of a query, which is in a different database.

So in the example the question would be the number of records of Query1, in database Base.mdb.

Could you advise how to put the rs to this line TotalRecQuery1 = DCount("*", "rs")?

If the Query1 would be in the current database, in that case the TotalRecQuery1 = DCount("*", "Query1") sure works.

thanks,
Sub Test()
Dim xlObj As Object, xltPath As String, xlWs As Object

Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset

Set dbs = DAO.OpenDatabase("D:\1Measurements\Base.mdb")
Set rs = dbs.OpenRecordset("Query1")
Set rs2 = dbs.OpenRecordset("Query2")

Dim TotalRecQuery1 As Long
Dim TotalRecQuery2 As Long
TotalRecQuery1 = DCount("*", "rs")
TotalRecQuery2 = DCount("*", "rs2")

Open in new window

0
Comment
Question by:csehz
  • 2
4 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34969910

Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim TotalRecQuery1 As Long
Dim TotalRecQuery2 As Long

Set dbs = DAO.OpenDatabase("D:\1Measurements\Base.mdb")
Set rs = dbs.OpenRecordset("Query1")
Set rs2 = dbs.OpenRecordset("Query2")

rs.movelast
TotalRecQuery1=rs.recordcount
rs2.movelast
TotalRecQuery2=rs2.recordcount
0
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 200 total points
ID: 34969920
To get the recordcount from a dao recordset you do...

rst.Movelast
rst.movefirst  ' just to reset to the start
x= rst.recordcount
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 300 total points
ID: 34969922
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim TotalRecQuery1 As Long
Dim TotalRecQuery2 As Long

Set dbs = DAO.OpenDatabase("D:\1Measurements\Base.mdb")
Set rs = dbs.OpenRecordset("Query1")
Set rs2 = dbs.OpenRecordset("Query2")

if not rs.eof  then
rs.movelast
TotalRecQuery1=rs.recordcount
end if
if not rs2.eof then
rs2.movelast
TotalRecQuery2=rs2.recordcount
end if


0
 
LVL 1

Author Closing Comment

by:csehz
ID: 34969972
Thanks it works perfect
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now