how to create sequence

ravi chaudhary

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.

PaulFrancis

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How create a chained delayed animation sequence in jQuery?

来自分类Dev

Does RandomStringUtils create a deterministic or reproducible sequence?

来自分类Dev

使用Oracle的CREATE SEQUENCE作为安全随机

来自分类Dev

CREATE SEQUENCE错误:“ INCREMENT BY”的值无效

来自分类Dev

How to assign a value to a particular sequence in a column in r?

来自分类Dev

Regex: How to match sequence of SAME characters?

来自分类Dev

How get only 5mers from a sequence

来自分类Dev

How get only 5mers from a sequence

来自分类Dev

How to create popup menu

来自分类Dev

how to create AbstractUser

来自分类Dev

How to create SOAP request with CDATA

来自分类Dev

How to create a constructor initialized with a list?

来自分类Dev

How to create a Randomly Generated number

来自分类Dev

How to create basic recursion on folders?

来自分类Dev

How to create combinations of values in Java?

来自分类Dev

How to create a Concrete WSDL in tibco

来自分类Dev

How to create a DOT file in Python?

来自分类Dev

How to create unique keys in KeystoneJS

来自分类Dev

How to create an internal multipath gateway

来自分类Dev

using panda dataframe, how to calculate average of sequence of data in csv log file?

来自分类Dev

How can I create a histogram in R?

来自分类Dev

How to create a frame less Dialog box?

来自分类Dev

How to create new excel document in java

来自分类Dev

How to create Dynamic factory in Angular js?

来自分类Dev

How to manually create and load namespaces in R

来自分类Dev

How to create a simple share linkedIn link?

来自分类Dev

How to create a Topic in Kafka through Java

来自分类Dev

AngularJS how to create a reusable template for Bootstrap modal

来自分类Dev

How to dynamically create regex to use in .match Javascript?

Related 相关文章

热门标签

归档