Results 1 to 10 of 10

Thread: VBA - Importing a CSV file into Access table errors

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Location
    Victoria BC
    Posts
    3

    VBA - Importing a CSV file into Access table errors

    Hello all!!!
    I have a form in Access which imports a CSV file into a table as follows:

    DoCmd.TransferText acImportDelim, , tblName, InputDir & ImportFile, True

    I am having a very strange problem though. There are certain fields in the table of type text but when the imported data appears to be numeric (the first line has data like '34' but subsequent are like '34b') the textual data is not imported. A "TYPE CONVERSION FAILURE" occurs.

    Does anyone have any ideas how to overcome this. The data is from large extracts so resorting to begin with text records manually is not really an option!!

    Any help is appreciated.

    Cheers

  2. #2
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    works fine for me. what field types are you using in your mdb?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Location
    Victoria BC
    Posts
    3
    They are text which really baffles me. When i rearrange the order of records in the csv file to a text looking record appears first the import works correctly. It is just when a numeric data occurs first it gets confused. I was thinking about writing a function that somehow cast all the fileds in the CSV that are supposed to be text (according to the table) into strings. My VBA is VERY rusty so don't have a clue how i'd do that???

    Any suggestions?

  4. #4
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    yah, I just duplicated the error. not sure how to fix that ... sorry

    (memo field gives same error as text field in case you were wondering)

  5. #5
    New Member
    Join Date
    Aug 2006
    Location
    Mumbai
    Posts
    7

    Re: VBA - Importing a CSV file into Access table errors

    stuck with the same problem ... Any solution on this ?....urgent

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA - Importing a CSV file into Access table errors

    Access VBA question moved to Office Development

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: VBA - Importing a CSV file into Access table errors

    Use an Import Specification (under Advanced when you do a manual import). Save that, then use the Import Specification name as a parameter in your code. As far as I know, you first have to do the manual import to create the spec, but can then use it in code. I think offhand it comes in the empty space in your string. You can set data types when you create it.
    Another hackish option would be to swap lines in the csv file so that 34b, the text value, was on the first line. Presumably it would then treat 34 as text. The order of records in a table doesn't matter.
    Code:
    DoCmd.TransferText acImportDelim, "MyImportSpec", tblName, InputDir & ImportFile, True
    Tengo mas preguntas que contestas

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: VBA - Importing a CSV file into Access table errors

    I've noticed the same problem in connecting to an .xls file with ODBC - the data type of the column is determined by the data in the first row. It looks like a bug in the way Microsoft connects to Excel files. (And there's no way to use an import specification in an ODBC connection that I know of.)
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: VBA - Importing a CSV file into Access table errors

    Or, open the csv file in Excel and convert the column to Text format.
    Tengo mas preguntas que contestas

  10. #10
    Lively Member
    Join Date
    May 2007
    Posts
    70

    Re: VBA - Importing a CSV file into Access table errors

    Hi there...

    I'm a novice at VBA compared to some of the other users onboard but if you are having problems with datatype would it be easy to parse the data through set variables of the datatype required? eg.

    source data X is a number

    import it into variable 'a' (which has been dim'ed as a string)

    then pass 'a' into the table?

    Not sure if this will help...

    NeoDan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width