Solved

Compare two tables in ACCESS and then populate vacant columns

Posted on 2009-05-13
7
300 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

856 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