I have a list of Dealers and multiple rows for each Dealer. I need to export all records to Excel and save separate files for each Dealer in the list. I have the code behind a command button on a blank form. It was working but it saved all records into the one excel file. I’m not sure how to filter the query for one Dealer without having to create a report or form first. I just want it to export all records direct from the query. When you see the code below you will know that I am a novice at recordsets (I get mixed up with DAO and ADO) and loops. I'm using Access 2002. I appreciate your help in advance.
Public cnn As ADODB.Connection
Public rst As New ADODB.Recordset
Private Sub Command0_Click()
Dim Path As String
Dim StrDealer As String
Dim intDcode As Long
Dim strQry As String
Dim StrExt As String
'strQry = "qryletterclosure"
Set cnn = CurrentProject.Connection
rst.Open "qryletterclosure", cnn, adOpenKeyset, adLockBatchOptimistic
'Set rst = CurrentDb.OpenRecordset("SELECT * from qryletterclosure ORDER BY qryletterclosure.dealercode;")
Path = "C:\ExcelExport\"
StrDealer = rst("DealerName")
intDcode = rst("DealerCode")
StrExt = ".xls"
'strQry = "SELECT * qryletterclosure ORDER BY qryletterclosure.dealercode WHERE qryletterclosure.dealercode = intDcode;"
Do Until rst.EOF
rst.Filter "dealercode" = intDcode
DoCmd.OutputTo acOutputQuery, rst, acFormatXLS, Path & StrDealer & StrExt, True
rst.MoveNext
Loop
MsgBox ("Export Complete")
End Sub
Public cnn As ADODB.Connection
Public rst As New ADODB.Recordset
Private Sub Command0_Click()
Dim Path As String
Dim StrDealer As String
Dim intDcode As Long
Dim strQry As String
Dim StrExt As String
'strQry = "qryletterclosure"
Set cnn = CurrentProject.Connection
rst.Open "qryletterclosure", cnn, adOpenKeyset, adLockBatchOptimistic
'Set rst = CurrentDb.OpenRecordset("SELECT * from qryletterclosure ORDER BY qryletterclosure.dealercode;")
Path = "C:\ExcelExport\"
StrDealer = rst("DealerName")
intDcode = rst("DealerCode")
StrExt = ".xls"
'strQry = "SELECT * qryletterclosure ORDER BY qryletterclosure.dealercode WHERE qryletterclosure.dealercode = intDcode;"
Do Until rst.EOF
rst.Filter "dealercode" = intDcode
DoCmd.OutputTo acOutputQuery, rst, acFormatXLS, Path & StrDealer & StrExt, True
rst.MoveNext
Loop
MsgBox ("Export Complete")
End Sub