Solved

Compare two tables in ACCESS and then populate vacant columns

Posted on 2009-05-13
7
297 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
  • 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

746 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