A
AirgasRob
First I am trying to export a table to multiple excel spreadsheets. In my
table I have a column called [loc] for locations. For each group of records
with the same [loc] i want to export to excel for that group. I have figured
out how to export excel spreadsheets for each group of locations [loc] but
not how to filter each spreadhsheet to only show that location in it. Any
help would be appreciated and to the person who originally posted this
code...Thank you! Also is there a way to do this without having to reference
DAO?
Public Sub CreateWorksheets()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strPath As String
Set dbs = CurrentDb
On Error Resume Next
dbs.QueryDefs.Delete ("qryloc")
On Error GoTo 0
Set qdf = dbs.CreateQueryDef("qryloc")
Set rst = dbs.OpenRecordset("SELECT DISTINCT [loc] " & _
"FROM [TblMain]")
With rst
Do While Not .EOF
strSQL = "SELECT * FROM [TblMain] WHERE [loc] = """ & _
![Loc] & """"
qdf.SQL = strSQL
strPath = "C:\TransferStation\ExportExcel\ " & _
![Loc] & ".xls"
On Error Resume Next
Kill strPath
On Error GoTo 0
DoCmd.TransferSpreadsheet _
TransferType:=acExport, _
TableName:="TblMain", _
FileName:=strPath
.MoveNext
Loop
.Close
End With
Set rst = Nothing
Set dbs = Nothing
End Sub
table I have a column called [loc] for locations. For each group of records
with the same [loc] i want to export to excel for that group. I have figured
out how to export excel spreadsheets for each group of locations [loc] but
not how to filter each spreadhsheet to only show that location in it. Any
help would be appreciated and to the person who originally posted this
code...Thank you! Also is there a way to do this without having to reference
DAO?
Public Sub CreateWorksheets()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strPath As String
Set dbs = CurrentDb
On Error Resume Next
dbs.QueryDefs.Delete ("qryloc")
On Error GoTo 0
Set qdf = dbs.CreateQueryDef("qryloc")
Set rst = dbs.OpenRecordset("SELECT DISTINCT [loc] " & _
"FROM [TblMain]")
With rst
Do While Not .EOF
strSQL = "SELECT * FROM [TblMain] WHERE [loc] = """ & _
![Loc] & """"
qdf.SQL = strSQL
strPath = "C:\TransferStation\ExportExcel\ " & _
![Loc] & ".xls"
On Error Resume Next
Kill strPath
On Error GoTo 0
DoCmd.TransferSpreadsheet _
TransferType:=acExport, _
TableName:="TblMain", _
FileName:=strPath
.MoveNext
Loop
.Close
End With
Set rst = Nothing
Set dbs = Nothing
End Sub