Hi, I'm Access97 user. I have a code running select count(*) statement from multiple tables as followings: (the names of these tables are the combination of [tblName] in "QryPF list" and [tbl1] in "TBL DATA"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
Function test()
On Error GoTo test_Err
DoCmd.SetWarnings False
Dim db As Database, rst1 As Recordset, rst2 As Recordset
Set db = CurrentDb
Set rst1 = db.OpenRecordset("QryPF list"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
Set rst2 = db.OpenRecordset("TBL DATA"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
rst1.MoveFirst
Do Until rst1.EOF
DoCmd.RunSQL "select ' " & rst1("tblName"
& " " & rst2("tbl1"
& "' as TBL, count(*) " _
& " as count into [tblChk count] from [" & rst1("tblName"
& " " & rst2("tbl1"
& "];"
rst1.MoveNext
Loop
rst1.Close
DoCmd.SetWarnings True
test_Exit:
Exit Function
test_Err:
MsgBox Error$
Resume test_Exit
End Function
This code works perfectly. The result tbl [tblChk count] contains only one record. But what I want to do is to union all counts from those tables in one table. How to run a union query through vba code?
Thanks a bunch in advance. ;-)
Function test()
On Error GoTo test_Err
DoCmd.SetWarnings False
Dim db As Database, rst1 As Recordset, rst2 As Recordset
Set db = CurrentDb
Set rst1 = db.OpenRecordset("QryPF list"
Set rst2 = db.OpenRecordset("TBL DATA"
rst1.MoveFirst
Do Until rst1.EOF
DoCmd.RunSQL "select ' " & rst1("tblName"
& " as count into [tblChk count] from [" & rst1("tblName"
rst1.MoveNext
Loop
rst1.Close
DoCmd.SetWarnings True
test_Exit:
Exit Function
test_Err:
MsgBox Error$
Resume test_Exit
End Function
This code works perfectly. The result tbl [tblChk count] contains only one record. But what I want to do is to union all counts from those tables in one table. How to run a union query through vba code?
Thanks a bunch in advance. ;-)