Solved

Compare two tables in ACCESS and then populate vacant columns

Posted on 2009-05-13
7
302 Views
Last Modified: 2013-11-27
Good day,

I have a data base which has a table that will be continually updated.  It will be updated with information regarding images.

Presently there is a table with all the data needed to populate the other table.  

I'm wondering how to go through Table 1 and when it hits a record, it looks at for instance the image name and then go and search Table 2.  If it sees the same image name, the query/script will then populate the remaining columns in Table 1 with the information from Table 2.

The Tables would be structured the same way, but they might not be in the future so perhaps the query/script can look at the column title and compare and update and/or the query can be run one column at a time with the user selecting the columns to compare and populate based on the key variable "image title" in this case.

Thank you in advance.
0
Comment
Question by:FocusedEnergy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 24383892
So do u have just one column that holds the image name? Need a little more info on your tables but if one column, it should be relatively straight forward.

eg table1 has fields f1,f2,f3 and imgName
table2 has f1a,f2a,f3a and imgName

a update statement to update all rows

update table1, table2
set table1.f2 = table2.f2a,
table1.f3 = table2.f3a
where table1.imgName = table2.imgName

0
 

Author Comment

by:FocusedEnergy
ID: 24387744
Hi rockiroads,

I think you are definitely onto the right way to do this with what I described.

How I think I need to have some extra flexibility.  Let me describe.

As you have it thus far, you are correct, table 1 has fields f1 - fn and one of them is imgName.

table2 has f1a through fna and one is imgName.

updating table 1 via the info in table 2 so if I understand it
the set table1.fn field equates it to table2.fna
and this happens with the where command
which necessitates that table1 imgName  = table 2 imgName.


If for instance, table1.f2 isn't empty for instance, is there a means of making it so that table.f2a doens't overwrite the contents of table 1?

Thanks
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 24389051
how about using a recordset then, easier to check for nulls. bit more trickier with sql

eg best to index imgName (dup index is fine)

public sub updatetable2

    dim rsT1 as dao.recordset
    dim rsT2 as dao.recordset
    dim i as integer

'Get all records from table1
    set rsT1 = currentdb.openrecordset("select * from table1")
    do while rsT1.eof = false

        'find record in table2
        set rsT2 = currentdb.openrecordset("select * from table2 where imgName = '" & rsT1!imgName & "'")

         rsT1.edit

'Iterate thru all columns in that row, any empty from table1, copy value from table2 (assumes same name, same order of fields)
         for i = 0 to rsT1.fields.count-1
             if isnull(rsT1.fields(i)) then rsT1.fields(i) = rsT2.fields(i)
         next i

         rsT1.update

         rsT2.close
         
         rsT1.movenext
    loop

    rsT1.close
    set rsT1 = nothing
    set rsT2 = nothing
end sub
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 500 total points
ID: 24389058
if field ordering is different but names are exactly the same, this should work also

if isnull(rsT1.fields(i)) then rsT1.fields(i) = rsT2(rsT1.fields(i).name)

0
 

Author Comment

by:FocusedEnergy
ID: 24389204
Thanks so much, I'm going to try out this code and make it work.  The names wil be exactly matched, just not necessarily in the same order so the second part helps a lot.

Thank you again for your help.
0
 

Author Closing Comment

by:FocusedEnergy
ID: 31581098
rockiroads was very clear and quick to respond to me.  It helped me out a lot see how the code worked and saved me a lot of time trying to figure it out on my own. Thank you muchly
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24389498
No worries, glad to have helped.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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