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?
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?