[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

import csv file to Access 2007 using VBA

Posted on 2010-08-14
7
Medium Priority
?
1,480 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 2000 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

656 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