I have a form "frmStmtExport", with the Record Source being a query. This continuous form lists the unique Sales Execs who are due commission payments based on teh current month sales.
I have a command button that transfers the sales execs commission data to excel, using the transferspreadsheet function. To do this I have to select each individual record in the form and click the export button.
I would like to do this automatically by looping through the records on the form, but become unstuck using recordsets.
The Tablename of the docmd.transferspreadsheet is also a query, which limits the records to the current record on the form, but my attempts at recordsets provide no results.
Any help would be appreciated.
Here is my code so far:
I have a command button that transfers the sales execs commission data to excel, using the transferspreadsheet function. To do this I have to select each individual record in the form and click the export button.
I would like to do this automatically by looping through the records on the form, but become unstuck using recordsets.
The Tablename of the docmd.transferspreadsheet is also a query, which limits the records to the current record on the form, but my attempts at recordsets provide no results.
Any help would be appreciated.
Here is my code so far:
Code:
Private Sub EXPORTSTMT()
'Stop
On Error GoTo Err_EXPORTSTMT
Dim strSQL As String, strExtension As String, strDocName As String, strPath1 As String
Dim RS As DAO.Recordset
Dim intVal As Integer, i As Integer
'Stop
strSQL = "SELECT tblMonthStmts.SalesExecID,tblSalesExec.SalesExec " & _
"FROM tblMonthStmts INNER JOIN tblSalesExec ON tblMonthStmts.SalesExecID = tblSalesExec.SalesExecID " & _
"GROUP BY tblMonthStmts.SalesExecID, tblSalesExec.SalesExec"
'Query used to export the statements to Excel
strDocName = "statement"
strFileName = strFileName & "\"
strPath1 = strFileName
'
Set RS = CurrentDb.OpenRecordset(strSQL)
RS.MoveLast
RS.MoveFirst
intVal = RS.RecordCount
For i = 1 To intVal
'DoCmd.GoToRecord acDataForm, "frmStmtExport", intVal
strExtension = RS!SalesExec
strExtension = strExtension & " statement.xls"
strFileName = strPath1 & strExtension
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strDocName, strFileName, True
RS.MoveNext
Next i
RS.Close
Exit_EXPORTSTMT:
Exit Sub
Err_EXPORTSTMT:
MsgBox Err.Description
Resume Exit_EXPORTSTMT
End Sub