Exporting the access resultset to excel programmatically

Dear all,

I would know how can I export the access resultset to excel. Can I get the query from Access which I have done and saved in access directly, so that I do not need to rewrite the SQL statement in the VB, if yet, then how? I think I need to use something like

Set rs = db.OpenRecordset("Query name") or such

but I don't know how. Can anyone of you can help me in this issue? Thanks in advance.

Regards,

Alex

[1094 byte] By [AlexKurama] at [2008-1-7]
# 1

You can use the CopyFromRecordset method for that:

Code Snippet

sQuery = "Select * From Products"

RS.ActiveConnection = Cn
RS.Source = sQuery
RS.Open

If RS.EOF = True And RS.BOF = True Then
'Cells(i1, iRevCol).Value = "N/A"
GoTo TakeNextRecord
End If

RS.MoveFirst
Activeworkbook.Sheets(1).Range("A2").CopyFromRecordset RS

Cheers

Shasur

Shasur at 2007-10-2 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...