Solved

import csv file to Access 2007 using VBA

Posted on 2010-08-14
7
1,419 Views
Last Modified: 2012-05-10
Hi,

I am having difficulty importing a .csv file to Access 2007. The file will import using the transfertext method but one field which is a text type keeps being imported as a numeric field which results in errors in the table created.

The procedure which will be automated will import the file on a regular basis and once the data is imported then the  code deletes the source file.

The datafile Data08142010.csv  has the current date in the name to ensure that the correct data is imported. Field16 or column "P" in the Excel file is the field causing the problem. Below is the code.

Private Sub TableData_Import()

Dim SourceFile As String
Dim ThsDay As String

On Error GoTo error_handler:

ThsDay = Date$
ThsDay = Right([ThsDay], 4) & Left([ThsDay], 2) & Mid([ThsDay], 4, 2)
SourceFile = "L:\VBA Tool Project\Data\Data" & ThsDay & ".csv"

DoCmd.SetWarnings False

DoCmd.TransferText acImportDelim, , "MainData1", SourceFile
Kill SourceFile

DoCmd.SetWarnings True
Exit Sub

error_handler:

On Error Resume Next
DoCmd.SetWarnings True

End Sub

Open in new window


I then created an import specification to use with the DoCmd.Transfertext command in order to make sure that the correct field types were used but VBA would not recognize the import specification file. Here is the error message.

Run-time error '3625':
The text file specification 'Import-MainDataDtl_Data' does not exist.
You cannot import, export, or link using the specification.

From what I understand this is a problem with Access 2007. Apparently, Access 2007 VBA does not have the capability to recognize file specifications - at lest for comma delimited files.

Does anyone know of a work-around to deal with this problem? I guess that I will probably have to use another method but I am not sure which one to use.

Thanks in a advance for your help.

scurvylion Data08142010.csv
0
Comment
Question by:scurvylion
  • 4
  • 3
7 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33438493
is this how you created the import specification

to create an import specification

1. File>get external data>import
2. select in the File of types box   Text files (*.txt etc..
3. select the file
4. in the import text wizard window select  delimited
5. Click advanced
6. in the import specification window
    type the name of the field in the Field Name column
    (here you can use the field names of the destination table, specify data type,
      check the box Skip if you do not want to import the column)


7 click save as, give the specification a name  <-- this is the specification name that you will use in the command line below


   DoCmd.TransferText acImportDelim, "ImportSpecificationName", "myTableName", "C:\myCsv.csv", True
0
 

Author Comment

by:scurvylion
ID: 33438620
In Access 2007 there is no File > get external data> import process. The same procedure is created off of the ribbon commands.

1) Click on external data> text file which then opens another window called "Get external data - Text file"
2) Use radio button to select "Import the source data into a new table in the current database"
3) specify the source of the file using the browse procedure the clicjk OK
4) Choose radio button to select delimited format and then click next
5) this window allows you to choose delimiter and text qualifier and if first row contains names - click next
6) This window allows you to type the name of the field in the Field Name column, choose data type, and if you want field indexed - also can choose to skip field - then click next
7) This window allows you to add primary key or designate field as such - click next
8) In this window you specify table to import to - click finish
9) last window asks if you wish to save import steps - check box and fill in box with file specification name to save as "ImportSpecificationName" + add description - then you click save import

code then becomes
DoCmd.TransferText acImportDelim, "ImportSpecificationName", "StopLightDtlMain1", SourceFile, True


Result: error code stated above
   
I just recreated another importspecificationfile and reran the code to make sure I didn't screw up before but unfortunately ended up with the exact same result.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 33438675
< last window asks if you wish to save import steps -> you saved the import steps not the import specification..


follow this

1) Click on external data> text file which then opens another window called "Get external data - Text file"
2) Use radio button to select "Import the source data into a new table in the current database"
3) specify the source of the file using the browse procedure the clicjk OK
4) Choose radio button to select delimited format and then click next
5) this window allows you to choose delimiter and text qualifier and if first row contains names - click next
6) This window allows you to type the name of the field in the Field Name column, choose data type, and if you want field indexed - also can choose to skip field - then click next
7) This window allows you to add primary key or designate field as such - click next

8) Click on ADVANCED button

9. in the import specification window
type the name of the field in the Field Name column
(here you can use the field names of the destination table, specify data type,
check the box Skip if you do not want to import the column)


10 click save as, give the specification a name <-- this is the specification name that you will use in the command line below


DoCmd.TransferText acImportDelim, "ImportSpecificationName", "myTableName", "C:\myCsv.csv", True

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33438705
see this sample db


db-Q-26403392-ImportSpec.accdb
0
 

Author Comment

by:scurvylion
ID: 33438765
That worked perfectly. Thanks a million!

You know I saw the advanced tab and even clicked on it to check it out but somehow I didn't see the save Import Specification part. Oh well.....my bad.

You know its funny because I was looking up this method and in "Access 2007 VBA Bible" it states that... "Access 2007 VBA code doesn't recognize saved specifications..... only code that avoids using specifications will work". pg 310. I'm glad that they are mistaken.

Thanks for all of your help. I really appreciate it!!
0
 

Author Closing Comment

by:scurvylion
ID: 33438767
Excellent job! Thanks again!
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 33438813
what is the complete title of the book and who is the author?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now