Solved

Automating Excel Text File Import from VBA

Posted on 2014-10-21
4
954 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

708 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

11 Experts available now in Live!

Get 1:1 Help Now