import tab-delimited txt into Access table using VBA

前端 未结 1 1312
情书的邮戳
情书的邮戳 2021-02-10 01:02

I am trying to import a tab-delimited txt file into an Access table using VBA. In my code, I want to insert it into a table that has not yet been created.

Here is what I

1条回答
  •  情书的邮戳
    2021-02-10 01:23

    As you have seen from the other articles on the topic, there really isn't a generic way to import tab-delimited text files. All of the other solutions I've seen say that you should import the tab-delimited text file once, save the import specification, and then use that import specification for all subsequent imports. The problem there is that if you want to import a different tab-delimited file the specification may not match.

    The only way I've found to do it generically (short of "rolling your own" code using FileSystemObject, Split(s, vbTab), etc.) is to create a completely generic specification for all 255 possible fields and use that. It requires a one-time setup as follows:

    Copy the CSV data from the Pastebin here, paste it into your favorite text editor, and save it as GenericTabSpecification.csv.

    Open that file in Excel, select all 256 rows and 4 columns, then hit Ctrl+C to copy.

    In Access, start the import wizard for text files and choose any tab-delimited file. (We won't actually be importing it.)

    Import1.png

    When you get to the first page in the wizard, click the "Advanced..." button.

    In the Import Specification dialog, verify the settings (Field Delimiter, Text Qualifier, etc.) then click the top-left corner of the Field Information grid so all rows are selected:

    Import2.png

    Hit Ctrl+V to paste the data from Excel into the grid. The grid should now contain 255 rows.

    Import3.png

    Click the "Save As..." button and name the specification GenericTabSpecification. Once that is done, cancel out of the wizard.

    Now we can do a generic import from VBA using a statement like this

    DoCmd.TransferText _
            TransferType:=acImportDelim, _
            SpecificationName:="GenericTabSpecification", _
            TableName:="newTable", _
            FileName:="C:\Users\Gord\Desktop\foo.txt", _
            HasFieldNames:=False
    

    0 讨论(0)
提交回复
热议问题