Solved

VBA Access - Count records of query, in another database

Posted on 2011-02-24
4
615 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

808 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