Solved

How To Import Text File From Code

Posted on 2013-01-07
6
536 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 120

Accepted Solution

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

Expert Comment

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


 DoCmd.TransferText acImportDelim, "ImportSpecificationName", "myTableName", "C:\myCsv.csv", True
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

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.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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