View Full Version : VBA code to import data


Kicker
10-02-2004, 10:49 PM
I have been playing with this all day and have looked at just about every forum I belong to.

The following code works "sometime" and blows up at others. I don't have to do anything with the AccessTest.csv file, it just changes how it works all by itself.

Private Sub Command0_Click()
DoCmd.RunSQL "delete * from accesstest"
DoCmd.TransferText acImportDelim, , "Accesstest", "AccessTest.csv", True
End Sub

Here is what I have....
I have a file that has been exported from the company database (oracle).
Two of the fields contain data that could be number or string. The AccessTest table has them as text and when it works, it works great.

the most common error is that the Jet engine can't find the csv file.

Is there another (better?) way of getting the data into my access application?

ttfn
Kicker

Uncle Gizmo
10-18-2004, 04:31 PM
>>>works "sometime" and blows up at others<<<<<

Is this consistent? In the sense that if you have a text file that imports correctly will it always import correctly, and does a text file that causes problems always causes problems?

What I am getting at is, can you reproduce the error at will or does it just happen anywhen without rhyme or reason?

I would suggest that you put some error trapping in the function to give you the error number and description at the function level, I don't know where the error report you mentioned came from but it may not be the actual error.

chilko
10-27-2004, 07:52 AM
I used this code and it worked consistantly for me when I put in a full path as below. I'm fairly green too so better check test it well. :) Private Sub Command0_Click()
DoCmd.RunSQL "delete * from accesstest"
DoCmd.TransferText acImportDelim, , "Accesstest", "c:/temp/AccessTest.csv", True
End Sub

cable
10-28-2004, 06:21 AM
If it complains that it can't find it, then its not lying to you!

From the looks of that code you posted you've made the assumption that the current path will have TestAccess.csv in it. Now unless theres a 'chdir' line somewhere before this import that will not always be the case.

You should replace the filename with full path information, ie c:\data\testaccess.csv, or if a network path \\server\x\data\testaccess.csv.