Solved

Compare two tables in ACCESS and then populate vacant columns

Posted on 2009-05-13
7
298 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

863 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

23 Experts available now in Live!

Get 1:1 Help Now