Solved

import csv file to Access 2007 using VBA

Posted on 2010-08-14
7
1,454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

724 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