Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RUN UNION QUERY USING VBA??? 1

Status
Not open for further replies.

jane30

Programmer
Nov 14, 2000
92
US
Hi,can I use VBA to run a union query in Access97 like this:

docmd.runsql("Select tbl.* from tbl union select tbl2.* from tbl2)

I tried and didn't work. Please help. I need to extract data from more than one table using union query, not table join. Thanks a lot.
 
In a union query the first select dictates the columns and data type that will be returned by the following select statements. Unless table 2 has the same number of columns and data types the select * will not work.
 
[bugeyed] As far as I know, the 'RunSQL' action is reserved for action queries or data-definition queries.

If you're just opening a recordset, you should use the method that does just that: OpenRecordset()

Dim strSQL As String
Dim db as Database
Dim rst As Recordset

strSQL = "SELECT tbl.* FROM tbl UNION SELECT tbl2.* FROM tbl2;"

Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL)

While Not rst.EOF
' do stuff
Wend VBSlammer
redinvader3walking.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top