Sample data (local Access table called 'Pets_data_table')
ID | Pet_Type | Pet_Owner
1 Dog Jane Doe
2 Cat John Doe
3 Hamster Bob Doe
4 Dog Melissa Doe
5 Cat Aaron Doe
At the moment, I can export the data in this table to one Excel workbook, and split the data into multiple sheets within that Excel workbook according to distinct values of a specific field. I use the following VBA to split the data according to distinct values of the 'Pet_Type' field:
Dim db As DAO.Database
Set db = CurrentDb()
Dim strPath As String
strPath = "C:\Desktop\" & "Pets_dataset_export_" & format(date(),"yyyy-mm-dd") & ".xlsx"
DoCmd.TransferSpreadsheet acExport, 10, "Qry - Dog", strPath, True, "Dog"
DoCmd.TransferSpreadsheet acExport, 10, "Qry - Cat", strPath, True, "Cat"
DoCmd.TransferSpreadsheet acExport, 10, "Qry - Hamster", strPath, True, "Hamster"
Set db = Nothing
MsgBox "Export operation completed"
This performs well when the field I am splitting the data with has a small number of distinct values.
However, it is inefficient when there are a large number of distinct values in the field I want to split the data with.
I would like to implement a more dynamic approach that allows me to split a dataset with a field that has 1...n number of distinct values.