I am exporting file from MS Access table to my local, I am doing this VBA coding. I need to create Sequence for the file names in such a manner,
File1PN00001
File1PN00002
File1PN00003
...
...
I am doing this by following code
Private Sub Command0_Click()
Dim FileName As String
Dim intChoice As Integer
Dim strPath As String
Dim LSProc As QueryDef
Dim db As Database
Set db = CurrentDb()
Set LSProc = db.CreateQueryDef("")
'make the file dialog visible to the user
strFilePath = BrowseFolder("Please Select Path to Export Neutrality Report file to")
If strFilePath <> "" Then
Call MsgBox(strFilePath, vbInformation, "Save Path")
Else
MsgBox "Please Provide a file path before exporting!", vbCritical + vbOKOnly
End If
FileName = strFilePath & "File1PN" & Format(txtBal_Number, "000000") & ".txt"
DoCmd.TransferText acExportDelim, , "T1", FileName, False
End Sub
I am confused how to create sequence for that, how to create sequence and how to increase the value by 1 every time when this code runs. Could you please help me out with this Thanks.
You can get this sorted if you have a logging system. First you need to create a table nothing fancy, just a simple table with two columns.
tbl_FileNameLog
--------------------------------------------------------------------------
FIELD NAME | DATA TYPE | COMMENTS
------------+---------------+---------------------------------------------
fileID | Number | Could use Auto Number, but for future safe.
| | use Number, so you can edit it.
| | But make sure it is a Primary Key.
exportDate | Date/Time | Just a Date field to store the Date.
Now you can edit your code to something like.
Private Sub Command0_Click()
Dim strPath As String, FileName As String
Dim lngChoice As Long
Dim dbObj As Database
Set dbObj = CurrentDb()
'make the file dialog visible to the user
strFilePath = BrowseFolder("Please Select Path to Export Neutrality Report file to")
If strFilePath <> "" Then
MsgBox "Exporting File to : " & strFilePath, vbInformation, "Save Path"
Else
MsgBox "Please Provide a file path before exporting!", vbCritical + vbOKOnly
Exit Sub
End If
lngChoice = Nz(DMax("fileID", "tbl_FileNameLog"), 0) + 1
FileName = strFilePath & "File1PN" & Format(lngChoice, "000000") & ".txt"
DoCmd.TransferText acExportDelim, , "T1", FileName, False
dbObj.Execute "INSERT INTO tbl_FileNameLog (fileID, exportDate) VALUES (" & _
lngChoice & ", " & CDbl(Date()) & ")"
Set dbObj = Nothing
End Sub
So the first time when the code is run, it will look for the Max ID in the table. Since there is no entry it will make use of the Nz function and assign a 0+1, so an ID of 1 is obtained. Then Export happens to the specified location. At the same time an entry into the file log is put stating the ID has been assigned. So the next time when the code runs, it will look for the file log table since ID of 1 is availabe it will not use 2. So on and so forth....
This way it does not rely upon the file system. It has its own log, so even if the file is moved or deleted it will still be able to provide a consistent/continious numbering. Hope this helps !
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句