hamrthroer
Registered User.
- Local time
- Today, 04:32
- Joined
- Jul 30, 2008
- Messages
- 33
I have this module to export a spreadsheet. The issue is with the SQL statement. I have tried both the stored query and using it in code. When I take out the reference to forms!frmMain!cboRptShift the export is successfull. I need this reference to correctly create the export as well as another reference that I haven't even tried yet because I've spent over 18 hours on this issue literally. I've read about this issue but still cannot find a solution. Here is the module:
Code:
Public Function ExportOvertime(strTQName As String, strSheetName As String, strFilePath As String)
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
' strFilePath is the name and path of the file you want to send this data into.
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As Field
Dim DB As Database
Dim MyQDef As QueryDef
Dim strSQL As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
'On Error GoTo err_handler
strSQL = "SELECT tblStaff.StateEntryDate" & vbNewLine
strSQL = strSQL & " , tblStaff.LastName" & vbNewLine
strSQL = strSQL & " , tblStaff.FirstName" & vbNewLine
strSQL = strSQL & " , tblStaff.MI" & vbNewLine
strSQL = strSQL & " , tblStaff.PriPhone" & vbNewLine
strSQL = strSQL & " FROM tblStaff" & vbNewLine
strSQL = strSQL & " WHERE (((tblStaff.Shift)=[Forms]![frmMain]![cboRptShift]) " & vbNewLine
strSQL = strSQL & " AND ((tblStaff.Rank)=""COI"" " & vbNewLine
strSQL = strSQL & " OR (tblStaff.Rank)=""COII"" " & vbNewLine
strSQL = strSQL & " OR (tblStaff.Rank)=""COS"") " & vbNewLine
strSQL = strSQL & " AND ((tblStaff.WorkOT)=Yes))" & vbNewLine
strSQL = strSQL & " ORDER BY tblStaff.StateEntryDate" & vbNewLine
strSQL = strSQL & " , tblStaff.Random;"
strPath = "X:\Overtime Spreadsheet\OvertimeCallDown"
Set rst = CurrentDb.OpenRecordset(strSQL)
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(strPath)
ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets("OvertimeLocalShift")
xlWSh.Range("A2").Select
rst.MoveFirst
xlWSh.Select
xlWSh.Range("A2").CopyFromRecordset rst
xlWSh.Range("1:1").Select
xlWSh.Range("A2").Select
rst.Close
Set rst = Nothing
Exit Function
'err_handler:
'DoCmd.SetWarnings True
'MsgBox Err.Description, vbExclamation, Err.Number
'Exit Function
End Function
Last edited by a moderator: