You could have a textfile (lastfile.txt) located in the same folder that your trying to write to. Have this textfile contain the last number in the sequence.
Your export code could have the following setup...
Option Compare Database
Option Explicit
Function ExportQueryToFile()
On Error GoTo Error_ExportQueryToFile
'
' Variable Declaration
'
Const intSeqLen As Integer = 4 'The length of the sequential number at the end of each file
Const strDrivePath As String = "d:\temp\" 'The drive and path where output file should go
Dim intPos As Integer 'Use to identify the current position/length of reference
Dim intLastFile As Integer 'The last sequential number used as imported from lastfile.txt
Dim intNextFile As Integer 'The value representing the number to be used for export on this occasion
Dim strNextFile As String 'The string equivelant of intNextFile including pre-padded zero(s)
Dim strSeqFile As String 'The full path and filename of the Sequential Number generator
Dim FileIsOpen As Boolean 'Is the Input file open?
'
' Variable initialisation
'
strDrivePath = "d:\temp\"
strSeqFile = strDrivePath & "lastfile.txt"
FileIsOpen = False
'
' Open input file - import last sequential file number
'
If Dir(strSeqFile) = "" Then Error 9998
Open strSeqFile For Input As #1
FileIsOpen = True
Input #1, intLastFile
Close #1
FileIsOpen = False
New_ExportQueryToFile:
'
' Check integrity of file number sequence returned
'
If intLastFile = ((10 ^ intSeqLen) - 1) Then Error 9999
intNextFile = intLastFile + 1
'
' Open output file - export last sequential file number
'
Open strSeqFile For Output As #1
FileIsOpen = True
Write #1, intNextFile
Close #1
FileIsOpen = False
'
' Pad file number with zeros
'
strNextFile = Trim(Str(intNextFile))
If Len(strNextFile) < intSeqLen Then
For intPos = 1 To (intSeqLen - Len(strNextFile))
strNextFile = "0" & strNextFile
Next
End If
'
' Export query to requested file
'
strNextFile = strDrivePath & "File" & strNextFile & ".txt"
DoCmd.TransferText acExportDelim, , "qryTest", strNextFile
Exit_ExportQueryToFile:
If FileIsOpen Then Close #1
Exit Function
Error_ExportQueryToFile:
If Err.Number = 9999 Then
MsgBox "The maximum number of export files has been met.@Change the code of this program " _
& "to increase the constant variable known as@intSeqLen.", vbOKOnly + vbCritical, "Error: 9999"
Resume Exit_ExportQueryToFile
ElseIf Err.Number = 9998 Then
intLastFile = (CInt(InputBox("The " & strSeqFile & " will be created now. You need to specify what " _
& "the first sequential number will be. It must be less than " & (10 ^ intSeqLen) & ". Start numbering at...", _
strSeqFile & " not found!", 1)) - 1)
Resume New_ExportQueryToFile
Else
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume Exit_ExportQueryToFile
End If
End Function