Solved

Automating Excel Text File Import from VBA

Posted on 2014-10-21
4
992 Views
Last Modified: 2014-10-22
I have been getting a bunch of data files in .csv format and using Excel to eliminate bad rows and scrub bad cells prior to import. Now I am getting them as .txt files and when I use Excel to open them I lose leading zeroes that I need.

I'm trying to adapt this code from an Excel macro into VBA in Access, but I'm using late binding and Access is blowing up on the code.

Any suggestions on how to "import" in Excel using VBA code would be appreciated.

Thanks.
0
Comment
Question by:Buck_Beasom
4 Comments
 
LVL 34

Accepted Solution

by:
PatHartman earned 250 total points
ID: 40394887
Why would you be using Excel at all at this point?  Why not import the text file directly into Access?  We can help you with converting the code to Access VBA but you'll need to post it and tell us where it fails and what error message you are getting.
0
 
LVL 39

Expert Comment

by:als315
ID: 40394896
In Access you can link csv file and create queries for correct import. You will need very small VBA part:
1. Rename (copy) file according to your linked file name (constant)
2. Run import queries
3.  Delete imported csv file
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 40395054
have you tried using Import Specification?

you need to create import specification first.

 To create the Import Specification
  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 then click 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", "TableName", "C:\folderName\myText.csv" , True
0
 

Author Closing Comment

by:Buck_Beasom
ID: 40396792
The 2 suggestions combined to provide a good solution.

Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

864 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

19 Experts available now in Live!

Get 1:1 Help Now