batch import data from txt to a MS Access 2007 field

Posted on 2014-07-16
Last Modified: 2016-06-07
I have a MS Access 2007 database with a list of thousands of people, that includes their id number, name, status, link to photo file location, etc.  I took a couple hundred pictures that had to wait to be entered into the "photo" field, because I had no id numbers for them at the moment.  Now, I do have a txt file with a list of a couple hundred files that are named after their id numbers.  It looks like this:


What I want to do is follow a few steps in Access 2007 (compacted into a macro or a couple of functions) that would:

1. read the txt file
2. find an entry for each id number listed, by taking, say, 007733 out of 007733.jpg and looking it up in the "idnumber" column
3. update those couple hundreds of entries so that, for instance, an entry with 007733 in the "idnumber" column it would have: N:\photos\007733.jpg in the "photo column

You suggestions are most welcome!
Question by:lolekbolek
    LVL 84

    Accepted Solution

    Is this a one time thing? Sounds like it, and if so then first import the Text file into Access using the Get External Data tab. When you do that, you should have a single column table. Now add a second column to that table named "IDNumber", and run this code:

    CurrentDb.Execute "update YourTable set IDNumber=Left(YourTextField, Len(YourTextField)-4)"

    You can run this from the Immediate window, of from the click event of a button, etc. You'd obviously have to change the Table and Field names to match your own.

    Now update your "live" table to show the Photo field:

    Currentdb.Execute "UPDATE YourLiveTable SET YourImageField='F:\Photos\" & YourImportTable.ImageName & "'"

    You can do most this in the query interface as well - just import the table, and run the first code above in the Immediate window, then create a new query with your two tables, and "join" them on the IDNumber field, and the "ID" field of your existing table. Then select to create an Update query, and add this expression to the Criteria row of the YourImageField column:

    "F:|Photos\" & [YourImportTable].FirstTextField

    Author Comment

    Thank you, Scott.  I usually never have to work with Access, so if you could give me an idea of what the "immediate window" means or how to run your code from it (i.e., what menu options or toolbar buttons to use before getting to a place where I can paste the code), that would be peachy.
    LVL 26

    Expert Comment

    No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

    I have recommended this question be closed as follows:

    Accept: Scott McDaniel (Microsoft Access MVP - EE MVE ) (http:#a40200809)

    If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

    Experts-Exchange Cleanup Volunteer

    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

    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…
    I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
    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 specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    732 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

    20 Experts available now in Live!

    Get 1:1 Help Now