Solved

Import selected text file records into Access

Posted on 2014-02-24
6
832 Views
Last Modified: 2014-03-26
I need to import into an Access table selected segments of selected data records that are saved in a text file.The text file has a lot of garbage records and data within each record not useful to me that I need to skip over.

In plain English, this is what I need to accomplish:

1. Read the text file from top to bottom
2. If bytes 1 thru 9 = 'Marketing', then import bytes 1 thru 9 of that record into Field1 of the table and bytes 30 thru 40 of the same record into Field2 of the table. Otherwise skip the record.

Thank you.
0
Comment
Question by:dbfromnewjersey
6 Comments
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 167 total points
ID: 39883636
Often the best way is to import the file into a temporary or "staging" table, and then perform your data validation using SQL and VBA. Once you've validated the records, you can then move them over to the live tables.

If this is a one-time deal, then you can just use the Access interface to import the Text file.  I'm not sure what sort of table structure you'd end up with - that depends on what sort of file you're working with, and how you import it - but if the first Field of your Staging table is the one that contains the "Market" word, then you could just do this:

Currentdb.Execute("INSERT INTO YourLiveTable(Field1, Field2, Field3) (SELECT Col1, Col2, Col3 FROM YourStagingTable WHERE Col1='Market'")

Of course, how you do this depends entirely on the file structure and such. If you could post an example file, perhaps we could provide more information.
0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 166 total points
ID: 39883808
All the examples I have are way too complicated to post here.  Look for the syntax for -
Open .... For Input
and
Line Input #FileNum, strLine

To see how to read a text file.

Basically, you have to get a FileNum, Open the file with Open, and then read each record with Line Input.  Beyond that, you're on your own and it depends on the format how you have to process the records.
0
 

Author Comment

by:dbfromnewjersey
ID: 39883868
I'll try to post an example of the type of text files I deal with in a bit.
0
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.

 

Author Comment

by:dbfromnewjersey
ID: 39883970
By the way, I had already tried importing the file directly into a new Access table for it to act as a staging area for further processing. The problem I had with that method is that in my source file, there is a debit column and a credit column.  When I import the text file, both of those columns wind up being combined into one field in Access instead of staying separate.  I can import the file into Excel first but I wind up having to move the verticle lines around manually in the Text Import Wizard window to get them where I need to be. I'm trying to automate the process as much as possible so that I don't always have to deal with manually moving those lines around before importing.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39884050
There is no easy way to import a file with inconsistent row formats which is what it sounds like you have.  If you have debit and credit fields on different rows, your code will need to combine them into one row as you are looping through the import file.
0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 167 total points
ID: 39884192
Sometimes you can clean up a file if you import it into Word and use Word's Search and Replace feature to do the cleanup -- then re-save it as a clean text file for import into Access.
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

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

919 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

16 Experts available now in Live!

Get 1:1 Help Now