Good morning all,
First, lets start with my goal:
I've got a VBA which creates a temporary table with some data fields, including one field called Forwarding Agent. This table is created based on forwarding agents selected in a multi-select box on a form. The field can contain one forwarding agent up to 292 (current number of agents). These agents are listed in a source table. What I'm aiming to do is export the records in the temporary table to different export folders per forwarding agent. For example, if the table contains data of Agent1 and Agent2, the table should be split and exported to folder CurrentProject.Path \Export\Agent1\ etc. If the folder doesn't exist, it should be created.
What have I been thinking of:
I can drive this based on below code, which means I have to create 292 different strSQL for each agent.
Preferably, I would have a code which checks the unique entries in the temporary table and populates a variable based on these, with a strSQL which cycles through this variable.
This would ease the amount of coding and has the benefit that I can replace all the "Agent1" in the code above with the variable entry.
Any help getting this up and running is appreciated
First, lets start with my goal:
I've got a VBA which creates a temporary table with some data fields, including one field called Forwarding Agent. This table is created based on forwarding agents selected in a multi-select box on a form. The field can contain one forwarding agent up to 292 (current number of agents). These agents are listed in a source table. What I'm aiming to do is export the records in the temporary table to different export folders per forwarding agent. For example, if the table contains data of Agent1 and Agent2, the table should be split and exported to folder CurrentProject.Path \Export\Agent1\ etc. If the folder doesn't exist, it should be created.
What have I been thinking of:
I can drive this based on below code, which means I have to create 292 different strSQL for each agent.
Code:
strSQL "SELECT TBL_Gross_Performance_Forwarder_Temp.* " _
& "INTO TBL_Gross_Performance_Forwarder_Temp_2" _
& "FROM TBL_Gross_Performance_Forwarder_Temp" _
& "WHERE (((TBL_Gross_Performance_Forwarder_Temp.[Forwarding agent])="Agent1"));"
DoCmd.Run strSQL
If DCount("*", "TBL_Gross_Performance_Forwarder_Temp_2") > 0 Then
If Len(Dir(CurrentProject.Path & "\Export\Agent1\", vbDirectory)) = 0
Then MkDir CurrentProject.Path & "\Export\Agent1\"
End If
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "TBL_Gross_Performance_Forwarder_Temp_2", CurrentProject.Path & "\Export\Agent1\" & Format(Date, "yyyymmdd") & " - " & Format(Time, "hhnn") & " - Gross Performance Agent1 (" & Environ("Username") & ").xlsx", True
MsgBox "Export completed. File exported to:" & vbNewLine & vbNewLine & CurrentProject.Path & "\Export\Agent1\", vbInformation, "Export completed"
Else
MsgBox "No records in master data for given criteria.", vbInformation, "No records found"
End If
Preferably, I would have a code which checks the unique entries in the temporary table and populates a variable based on these, with a strSQL which cycles through this variable.
This would ease the amount of coding and has the benefit that I can replace all the "Agent1" in the code above with the variable entry.
Any help getting this up and running is appreciated
