schema.ini file not working for MS Access

前端 未结 3 1514
故里飘歌
故里飘歌 2021-01-29 01:38

I have a bunch of csv files that I import via VBA into tables in Access. I also have a schema.ini file in the same directory as the csv files that I am importing. Despite fields

3条回答
  •  伪装坚强ぢ
    2021-01-29 01:54

    I have a half answer. I found no way to avoid the Col# option in the schema.ini file despite what it says here: https://msdn.microsoft.com/en-us/library/ms709353(v=VS.85).aspx (If you know how to do it, post an answer and I'll give you credit.)

    Other than that I figured out how to force a data type on a field that has a bunch of null values at the top of the file.

    (Note that the link above says you can use ColNameHeader=True MaxScanRows=0 and it will look at the entire file for a format. That didn't work for me.)

    Here is my working sample (my answer)....

    Create this csv file and save it directly on your C drive (no folder) and save it as test.csv:

    +-----------+----------+---------+
    | FirstName | LastName | Anumber |
    +-----------+----------+---------+
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  |         |
    | Robert    | King     |         |
    | Nancy     | Davolio  | 1.1     |
    | Robert    | King     | 2.1     |
    +-----------+----------+---------+
    

    The large number of Null values in the Anumber field is important to this.

    In the SAME LOCATION create a text file and save it has schema.ini with the following in it:

    [test.csv]
    ColNameHeader=True
    MaxScanRows=0
    CharacterSet=ANSI
    Format=CSVDelimited
    Col1="First Name" Char
    Col2="Last Name" Char
    Col3="Anumber" Double 
    

    Then run this code in MS Access VBA (2013 for me):

    Sub ImportSchemaTable()
        Dim db As DAO.Database
        Set db = CurrentDb()
        db.Execute _
        "SELECT * INTO test FROM [Text;FMT=CSVDelimited;HDR=Yes;DATABASE=C:\;].[test#csv];", dbFailOnError
        db.TableDefs.Refresh
        RefreshDatabaseWindow
    End Sub
    

    And a table called "test" will be created using the test.csv file and the schema.ini file (in the same location as the test.csv file) and the "Anumber" field will be formatted as a "Number" despite all of the Null values at the top of the field. If you do not use the schema.ini file, the "Anumber" field will be formatted as a Short Text type.

    Edit: Change "C:\" in the Sub above to where ever you want to have your csv file AND schema.ini file.

提交回复
热议问题