Solved

import csv file to Access 2007 using VBA

Posted on 2010-08-14
7
1,429 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 views 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 Access…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

895 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

13 Experts available now in Live!

Get 1:1 Help Now