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
Re: VBA - Importing a CSV file into Access table errors
stuck with the same problem ... Any solution on this ?....urgent :(
Re: VBA - Importing a CSV file into Access table errors
Access VBA question moved to Office Development
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
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.)
Re: VBA - Importing a CSV file into Access table errors
Or, open the csv file in Excel and convert the column to Text format.
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