Hi All,
After more digging, I found this code that does what I need.
All data from FBKO_Query1 should be exported into separate workbooks - it works. The query contains only active suppliers but when the workbooks are exported, all suppliers are exported (606 to be exact) so I'm ending up with a lot of empty workbooks.
All weekend I've been working on exporting only those suppliers that are current but cannot do so. How do I update the code to export only those suppliers in the FBKO_Query1?
Option Explicit
Option Compare Database
Sub ExportCustData()
Dim blnReachedEnd As Boolean
Dim strCurrCust As String, strFilePath As String
Dim strQueryDefName As String, strSummaryQuery As String
Dim strDetailQuery As String, strSummaryField As String
Dim rstCustomers As Recordset
Dim qdfCustData As QueryDef
'***** The file path for where you want data exported goes here - make sure to put a backslash at the end! *****
strFilePath = "N:\Merch-Global_Supply\Inventory_Planning\Supply_Planning\R egional Purchasing\Buyer Folders\Staci\suppliers\"
'************************************************* ************************************
'* You should have a query that summarizes the value you want to base your export on *
'* for example, if you want a spreadsheet per customer id you will build a query *
'* that groups by the customer id field. This can be a basic single-field query. *
'************************************************* ************************************
strSummaryQuery = "SuppliersQuery1"
'***** This is where you specify the query that is summarized in the summary query *****
strDetailQuery = "FBKO_query1"
'***** This is where you tell the code what field you are summarizing on *****
strSummaryField = "Vendor Name"
'***** The code adds a query which will be deleted at the end. Enter the name here. *****
strQueryDefName = "Query3"
Set qdfCustData = CurrentDb.CreateQueryDef(strQueryDefName)
Set rstCustomers = CurrentDb.OpenRecordset(strSummaryQuery)
blnReachedEnd = rstCustomers.EOF
Do While blnReachedEnd = False
strCurrCust = rstCustomers!Supplier
qdfCustData.SQL = "SELECT * FROM [" & strDetailQuery & "] WHERE ([" & strSummaryField & "] = " _
& Chr$(34) & strCurrCust & Chr$(34) & ");"
DoCmd.OutputTo acOutputQuery, qdfCustData.name, acFormatXLS, strFilePath _
& strCurrCust & ".xls", False
rstCustomers.MoveNext
blnReachedEnd = rstCustomers.EOF
If blnReachedEnd Then rstCustomers.MovePrevious
Loop
rstCustomers.Close
CurrentDb.QueryDefs(strQueryDefName).Close
CurrentDb.QueryDefs.Delete strQueryDefName
End Sub