Sub foo()
Dim sql As String
Dim exportBatchNum As Long
Dim numRecordsToExport As Long
Dim numRecordsExported As Long
Dim i As Long
'--------------------------------------------------------------------------------------------------------------
'//Assumptions
' 1) There is a temp table that the records to export are in, and it has an autonumber field (called TempID)
' The autonumber field is important for getting an accurate count - if this is changed, then you must have
' a primary key in the query when you select TOP 400, because you do not want to have ties. It also has
' a field called ExportBatch to keep track of the batches.
' 2) There is a second temp table that is used by the TransferText function. It is the same as the first,
' but without the autonumber field or ExportBatch field (ie., just fields that actually need to be exported).
' 3) There is an export spec for the transfer text function (do an export to text on the table in Access using
' the wizard and hit the "advanced" button to get to where you can give the spec a name and save it.)
' 4) Some kind of naming convention is used for the export files (so we can delete the old ones before starting)
' Execution steps:
' Assuming records are in 1st temp table already,
' * delete existing batch files (if any)
' * flag next 400 records in 1st Temp table
' * clear second temp table
' * insert next 400 records in 1st Temp table to 2nd Temp table
' * TransferText to move records to file from 2nd Temp table
' * Repeat for next 400 records as needed.
'--------------------------------------------------------------------------------------------------------------
'//Assuming records to be exported are in Table3 already and this is a temp table
numRecordsToExport = DCount("*", "Table3")
'//Delete existing files (don't want these getting confused with current batches)
CreateObject("Scripting.FileSystemObject").DeleteFile "C:\myTemp\ExportFile*.txt"
'//Just in case - be sure batches flag is null at start of routine.
sql = "UPDATE Table3 SET Table3.ExportBatch = Null"
DoCmd.SetWarnings False
DoCmd.RunSQL sql
DoCmd.SetWarnings True
i = 1
Do While (numRecordsExported < numRecordsToExport)
DoCmd.SetWarnings False
'//flag records for next batch in 1st Temp Table
sql = "UPDATE Table3 SET ExportBatch = " & i
sql = sql & " WHERE TempID IN (SELECT top 400 TempID FROM Table3 WHERE ExportBatch Is Null)"
DoCmd.RunSQL sql
'//Clear 2nd Temp Table
sql = "DELETE Table4.* FROM Table4"
DoCmd.RunSQL sql
'//push records for next batch into 2nd Temp Table
sql = "INSERT INTO Table4 (Field1, Field2)"
sql = sql & " SELECT Field1, Field2 FROM Table3"
sql = sql & " WHERE ExportBatch = " & i
DoCmd.RunSQL sql
'//get a count of records exported so far (for reporting to user and also for our loop condition)
numRecordsExported = numRecordsExported + DCount("*", "Table4")
'//Create text file(s)
DoCmd.TransferText acExportDelim, "MySpec", "Table4", "C:\MyTemp\ExportFile" & i & ".txt", True
i = i + 1
DoCmd.SetWarnings True
Loop
MsgBox numRecordsToExport & " records exported in " & i - 1 & " batches."
End Sub