vba - how to create sequence -
I am exporting the file from my MS Access table to my locality, I am doing this VBA coding. I need to create a sequence for the file name like this,
File1PN00001 File1PN00002 File1PN00003 ... ... I am doing this with the following As a code,
private subcommand0_ Click () dim file name as string dim intChoice as integer dim strpath string slow LSProc as set QueryDef as dim db database set db = CurrentDb () Set LSProc = db.CreateQueryDef ("") 'file dialog to user strFilePath = BrowseFolder (please select "Neutrality Report Ph Select Il export path appears to ") if strfilepath & lt; & Gt; "" Then call MSBBC (StuffFilePath, VBInnation, "Save Path") or other message "Please provide a file path before export!", VbCritical + vbOKOnly End if FileName = strfilePath & amp; "File 1pn" & amp; Format (txtBal_Number, "000000") & amp; ".txt" DoCmd.TransferText acExportDelim, "T1", filename, false ending sub How do I create a sequence for it, how to create a sequence and increase the value 1 each time When this code runs, can you help me with this thank you?
If you have a logging system. First of all, you do not need to create a table, just a simple table with two columns. tbl_FileNameLog --------------------------------------- ----------------------------------- FIELD NAME | Data type | Comments ------------ + --------------- + -------------------- ------------------------- fileID | Number | Auto number can be used, but secure for the future. | Use the number so that you can edit it. | | But make sure this is the primary key export date. Date / time Just a date field to store the date Now you can edit your code in some way.
Private subcommand 0 tag (string) in the form of slim straps, string as long as string filename lngChoice as dBObj = CurrentDb () 'file string file path = Browsfolder ("Please select the path to export selection file to export neutrality report") if strfilepath & lt; & Gt; "" Then export the MSBbox file ":" & amp; Please provide a file path before export! ", VbCritical + vbOKOnly exit sub if lngChoice = Nz (DMX (" fileID "," tbl_FileNameLog "), 0) +" StrigiPath, vbInformation, "vbInformation," Save Path " 1 FileName = strfilePath & amp; "File 1pn" & amp; Format (lngChoice, "000000") & amp; Enter ".txt" DoCmd.TransferText acExportDelim, "T1", filename, false DBobbs.execute "tbl_FileNameLog (file ID, export date) values (" & amp; _ lngChoice & amp; CDbl (date ()) & Amp;;) "Set dbObj = None End Sub Then when the code is run for the first time, it will search for the maximum ID in the table. Since there is no entry in it, it will use the NG function and assign 0 + 1, hence the ID of 1 will be received. Then exported to the specified place at the same time an entry in the file log has been entered that has been assigned the ID. So the next time the code runs, the ID of 1 for the file log table will appear because it will not use 2. And so on ....
This is not the way it relies on the filesystem, its own log, so even if the file has been moved or deleted, it is a consistent / Hope that it will be able to provide continuous numbers!
Comments
Post a Comment