Importing a text with separators

后端 未结 2 935
失恋的感觉
失恋的感觉 2020-12-19 06:01

I am trying to automate the adding of new text files, which all have the same (known) layout.

The columns are separated using tabs (the TAB button). My question is,

2条回答
  •  眼角桃花
    2020-12-19 06:21

    With the import wizard the downside is that for even the slightest change in file format, you'll have to click through all those steps yet again to get the import working.

    Check out @Remou's answer in ms Access import table from file in a query for a way to do it in straight SQL. I am actually using the same method in a project of mine. I use something like this (see my link for the details):

    insert into MyTable (column-list...)
    select (column-list...)
    from [data-source-specifications].[file-name]
    any-other-clauses...;
    

    Just one caveat. If you put this SQL syntax into a normal Access query object, there's a good chance that Access will mangle it to the point where it won't even be able to open the query object. So compose and save the query in a text file while you try it out in Access. Once the query is tested and working, save it in a VBA subroutine so that Access will run it exactly as is, like so:

    sub MyTableImport()
      sqlStr = "         insert into MyTable (column-list) " ' leave a space at the
      sqlStr = sqlStr & "select (column-list...) " ' end of each line of the string
      sqlStr = sqlStr & "from [data-source-specifications].[file-name] "
      sqlStr = sqlStr & "any-other-clauses... ;"
    
      DoCmd.RunSQL sqlStr
    end sub
    

提交回复
热议问题