I'm getting the "Item not in this collection" error. Also I need to change
two of the fields in the query, how would I do that in the strWhere? Here's
what I'm working with, maybe you can see why it's not completing. The
exports will run themselves but not when the criteria change part is
inserted. Thanks.
Public Function ERN_Output()
On Error GoTo Err_ERN_Output
Dim strFilePath As String
Dim strFileName As String
Dim strOutputFile As String
Dim strMessage As String
Dim strOutputQuery As String
Dim strWhere1 As String
Dim strSql1 As String
Dim strWhere2 As String
Dim strSql2 As String
strFilePath = "H:\Projects\ERN"
strFileName = "ERN " & Format(Date, "yyyy-mm-dd") & " -- PCA Alertcast
List.xls"
strOutputFile = strFilePath & "\" & strFileName
strOutputQuery = "qryERN_Information"
strWhere1 = "(Time = " & "Night" & ")"
strSql1 = "SELECT qryERN_Information.* FROM qryERN_Information WHERE " &
strWhere & _
"ORDER BY Time;"
strWhere2 = "(Time = " & "Day" & ")"
strSql2 = "SELECT qryERN_Information.* FROM qryERN_Information WHERE " &
strWhere & _
"ORDER BY Time;"
'************************ Create Files *************************
'Sheet 1
CurrentDb.QueryDefs("strOutputFile").SQL = strSql1
DoCmd.TransferSpreadsheet acExport, _
acSpreadsheetTypeExcel8, _
strOutputQuery, _
strOutputFile, _
True, _
"Sheet1"
'Sheet 2
CurrentDb.QueryDefs("strOutputFile").SQL = strSql2
DoCmd.TransferSpreadsheet acExport, _
acSpreadsheetTypeExcel8, _
strOutputQuery, _
strOutputFile, _
True, _
"Sheet2"
'************************ Format Worksheets 1&2 *************************
'Set variables to format the download
Dim objXLApp As Object
Dim objXLBook As Object
Dim objXLSheet1 As Object
Dim objXLSheet2 As Object
'Set the objects to format
Set objXLApp = CreateObject("Excel.Application")
Set objXLBook = objXLApp.Workbooks.Open(strFilePath & "\" & strFileName)
Set objXLSheet1 = objXLBook.Worksheets("Sheet1")
Set objXLSheet2 = objXLBook.Worksheets("Sheet2")
'Sheet 1
'Bold
objXLSheet1.range("A1:G1").Font.Bold = True
'Autofit columns
objXLSheet1.range("A:G").columns.autofit
'Sheet 2
'Bold
objXLSheet2.range("A1:G1").Font.Bold = True
'Autofit columns
objXLSheet2.range("A:G").columns.autofit