Solved

VBA Access - Count records of query, in another database

Posted on 2011-02-24
4
655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

696 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