Solved

How To Import Text File From Code

Posted on 2013-01-07
6
532 Views
Last Modified: 2013-01-07
I am using this code to attempt an import of a fixed width .csv text file:

Private Sub cbImport_Click()
    Dim strFileName As String
    Dim strSQL As String
    Dim strImportSpec As String
   
    If IsNull(Me.txtFile) = True Then
        MsgBox "You must find a file for import!", vbExclamation, "File Required"
        Exit Sub
    End If

    strSQL = "DELETE GL_ImportTable.* FROM GL_ImportTable"
    DoCmd.RunSQL strSQL

    strFileName = Trim(Me.txtFile)
    strImportSpec = "Import_GL"
   
    DoCmd.TransferText acImportFixed, "Import_GL", "GL_ImportTable", strFileName, True
       

End Sub

I have used it with both a variable and the string shown for the Specification name and in each case I get a message that says:

"The text file specification "Import_GL" does not exist."

Obviously it does, and I have even copied the name right out of the specification list into the code.

Any help will be appreciated.

Thanks.
0
Comment
Question by:Buck_Beasom
  • 2
  • 2
  • 2
6 Comments
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38752053
Are you on Access 2007 or newer? If so, using SavedImports is easier.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 38752061
have you created the import specification?

what version of access are you using?


'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/fixed
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

Now you can use the code below to import the CSV file via code

   DoCmd.TransferText acImportFixed, "ImportSpecificationName", "myTableName", "C:\myCsv.csv", True
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 38752069
..if you are importing a .csv file, you should be using


 DoCmd.TransferText acImportDelim, "ImportSpecificationName", "myTableName", "C:\myCsv.csv", True
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Buck_Beasom
ID: 38752088
Using Access 2007 (and some folks using 2010.) I just want to be able to park the import behind a button so users can get the data without having to go through the process themselves.

Going to try Capricorn's suggestions, as the raw file has no column headings.

Thanks.
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38752127
You can create a Saved Import (if you have Access 2007/2010).  Just click Text File in the Import section of the External Data tab of the ribbon.  When you complete the import wizard it will ask you if you want to save the import steps.  This Saved Importt will now be listed as a task you could run whenever you click Saved Imports in the Import section of the External Data tab.

You can call this Saved Import from code using this line:
DoCmd.RunSavedImportExport "Name Of Your Saved Import"
0
 

Author Closing Comment

by:Buck_Beasom
ID: 38752174
As always from Capricorn, easy, fast and right. I had not gone through the "Advanced/Save As" step because I thought simply saving the import spec would accomplish that. Once I did that step everything was Jake.

Thanks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

18 Experts available now in Live!

Get 1:1 Help Now