Rastro_Jetson
Registered User.
- Local time
- Today, 09:04
- Joined
- Mar 1, 2017
- Messages
- 12
Dear All,
Thank you in advance for any direction you may have. This is similar to my last post (looping, exporting to PDF). I'm trying to do that same thing with a query, exporting each Carrier's data into their own Excel file. The code runs. It creates separate files and they are correctly named. However, the records are not for a specific carrier. The records are for all carriers So, I know my strWhere is amiss. I'm using "OpenQuery" which doesn't seem to function like OpenReport in regards to the strWhere.
My table with the looped value is "tbl_Carrier_IDs". The looped field is "Carrier_ID". The query is "qry_For_Report".
*****
Option Compare Database
Option Explicit
Public Sub ExportToXLSX()
Const Folder = "C:\Test"
Const Domain = "tbl_Carrier_IDs"
Const LoopedField = "Carrier_ID"
Const QueryName = "qry_For_Report"
Dim rs As DAO.Recordset
Dim LoopedFieldValue As String
Dim FileName As String
Dim FullPath As String
Dim strWhere As String
Set rs = CurrentDb.OpenRecordset(Domain)
Do While Not rs.EOF
LoopedFieldValue = rs.Fields(LoopedField)
FileName = LoopedFieldValue & ".xlsx"
FullPath = Folder & "" & FileName
strWhere = LoopedField & " = '" & LoopedFieldValue & "'"
'DoCmd.OpenQuery QueryName, acViewPreview *** WHERE TO PUT strWhere ***
DoCmd.OutputTo acOutputQuery, QueryName, acFormatXLSX, FullPath
DoCmd.Close acQuery, QueryName
rs.MoveNext
Loop
MsgBox "Process Finished."
End Sub
Thank you in advance for any direction you may have. This is similar to my last post (looping, exporting to PDF). I'm trying to do that same thing with a query, exporting each Carrier's data into their own Excel file. The code runs. It creates separate files and they are correctly named. However, the records are not for a specific carrier. The records are for all carriers So, I know my strWhere is amiss. I'm using "OpenQuery" which doesn't seem to function like OpenReport in regards to the strWhere.
My table with the looped value is "tbl_Carrier_IDs". The looped field is "Carrier_ID". The query is "qry_For_Report".
*****
Option Compare Database
Option Explicit
Public Sub ExportToXLSX()
Const Folder = "C:\Test"
Const Domain = "tbl_Carrier_IDs"
Const LoopedField = "Carrier_ID"
Const QueryName = "qry_For_Report"
Dim rs As DAO.Recordset
Dim LoopedFieldValue As String
Dim FileName As String
Dim FullPath As String
Dim strWhere As String
Set rs = CurrentDb.OpenRecordset(Domain)
Do While Not rs.EOF
LoopedFieldValue = rs.Fields(LoopedField)
FileName = LoopedFieldValue & ".xlsx"
FullPath = Folder & "" & FileName
strWhere = LoopedField & " = '" & LoopedFieldValue & "'"
'DoCmd.OpenQuery QueryName, acViewPreview *** WHERE TO PUT strWhere ***
DoCmd.OutputTo acOutputQuery, QueryName, acFormatXLSX, FullPath
DoCmd.Close acQuery, QueryName
rs.MoveNext
Loop
MsgBox "Process Finished."
End Sub