Exporting Access data to multiple Excel Sheets

G-Man

New Member
Joined
Sep 16, 2004
Messages
11
Hi,

I am a VBA novice, looking for some help exporting a set of Access 2002 query results to a series of named excel files.

I am not sure of the best way to do this, and I can't figure out if my way will even work because I am stuck iwth and "Invalid Argument" error in my OpenRecordSet statement.

My module looks like this:

Public Sub ExportMyQuery()
Dim strRoot As String
Dim strSQL As String
Dim strFO As String
Dim strFile As String
Dim strTable As String
Dim rs As Recordset
'
strRoot = "C:\temp\"
strTable = "tempFOdata"
'
Set rs = CurrentDb.OpenRecordSet("FOs", dbOpenSnapshot)
If Not rs.EOF Then rs.MoveFirst
Do While Not rs.EOF
strFO = rs!Field1.Value
strFile = strRoot & strFO & ".xls"
strSQL = "SELECT [d1 FY04-RawData wo zero ac].[Field Office], [d1 FY04-RawData wo zero ac].County, [d1 FY04-RawData wo zero ac].[Unit Cost], [d1 FY04-RawData wo zero ac].[MinOfAve# Cost], [d1 FY04-RawData wo zero ac].[MaxOfAve# Cost], [d1 FY04-RawData wo zero ac].[StDevOfAve# Cost]" & _
" INTO " & strTable & _
" FROM [d1 FY04-RawData wo zero ac]" & _
" WHERE [d1 FY04-RawData wo zero ac].[Field Office]='" & strFO & "';"
DoCmd.RunSQL "DROP TABLE " & strTable & ";"
CurrentDb.Execute strSQL
DoCmd.OutputTo acOutputTable, strTable, acFormatXLS, strFile
rs.MoveNext
Loop

End Sub

Any suggestions? :confused:
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi, because Access 2000 and up use both DAO and ADO, you need to qualify your references. Try...

Public Sub ExportMyQuery()
Dim strRoot As String
Dim strSQL As String
Dim strFO As String
Dim strFile As String
Dim strTable As String
Dim dbs as DAO.Database
Dim rs As DAO.Recordset
'
strRoot = "C:\temp\"
strTable = "tempFOdata"
'
Set dbs = CurrentDb()
Set rs = dbs.OpenRecordSet("FOs", dbOpenSnapshot)
If Not rs.EOF Then rs.MoveFirst
Do While Not rs.EOF
strFO = rs!Field1.Value
strFile = strRoot & strFO & ".xls"
strSQL = "SELECT [d1 FY04-RawData wo zero ac].[Field Office], [d1 FY04-RawData wo zero ac].County, [d1 FY04-RawData wo zero ac].[Unit Cost], [d1 FY04-RawData wo zero ac].[MinOfAve# Cost], [d1 FY04-RawData wo zero ac].[MaxOfAve# Cost], [d1 FY04-RawData wo zero ac].[StDevOfAve# Cost]" & _
" INTO " & strTable & _
" FROM [d1 FY04-RawData wo zero ac]" & _
" WHERE [d1 FY04-RawData wo zero ac].[Field Office]='" & strFO & "';"
DoCmd.RunSQL "DROP TABLE " & strTable & ";"
CurrentDb.Execute strSQL
DoCmd.OutputTo acOutputTable, strTable, acFormatXLS, strFile
rs.MoveNext
Loop

End Sub

That should initialise the database and recordset. Post back if you have any other issues.

Denis
 
Upvote 0
:unsure:

Thanks for the input.

Unfortunately, now I get a message "Compile Error: User Defined Type NOt Defined" when the module reaches the "Dim dbs As DAO.Database" statemenet.

Any aditional suggestions?

Thanks,
SF
 
Upvote 0
:devilish:

Okay, I found my answer on on another web site: http://www.techonthenet.com/access/questions/db_object.htm

To correct the problem, you need to open your Access database. Press Alt-F11 to open the Microsoft Visual Basic window. When the Microsoft Visual Basic window appears, select References under the Tools menu.

The References window should appear. Scroll down until you find the option called "Microsoft DAO 3.6 Object Library" and check this item. Then click on the OK button.

Thanks for checking in.
 
Upvote 0

Forum statistics

Threads
1,221,814
Messages
6,162,132
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top