Solved

VBA Access - Count records of query, in another database

Posted on 2011-02-24
4
596 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks it works perfect
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

8 Experts available now in Live!

Get 1:1 Help Now