[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 727
  • Last Modified:

VBA Access - Count records of query, in another database

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
csehz
Asked:
csehz
  • 2
2 Solutions
 
Rey Obrero (Capricorn1)Commented:

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
 
peter57rCommented:
To get the recordcount from a dao recordset you do...

rst.Movelast
rst.movefirst  ' just to reset to the start
x= rst.recordcount
0
 
Rey Obrero (Capricorn1)Commented:
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
 
csehzAuthor Commented:
Thanks it works perfect
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now