Solved

Compare two tables in ACCESS and then populate vacant columns

Posted on 2009-05-13
7
299 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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 …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

777 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