Skip to content
Advertisement

How to update tables in MS ACCESS with query or with VBA & SQL

I have made my database work with all kinds of codes and workarounds, but it is not efficient! So here is a question that would solve a lot of my problems:

Two ACCESS tables: Tab01 & Tab02. Both have the same column headers, but different numbers of columns. Leaving aside primary key and ID for a moment, to check if the contents are the same objects I have selected 3 columns from each table that must be the same: Date, Place and Time.

Now the following:

Tab01.column01 contains values in rows 1, 4, 5, 8, 10

Tab01.column02 contains values in rows 2, 3, 10 values

Tab01.column03 contains values in rows 1, 2, 3, 4, 5, 6, 7, 8, 9


Tab02.column01 contains values in the rows 1, 2, 3, 4, 5, 9, 10

Tab02.column02 contains no values

Tab02.column03 contains values in rows 1, 10

If I want to complete the missing values in Tab01.columnX with the values from Tab02.columnX (by using the 3 values Date, Time, Place set as ID):

How would one proceed here? With a tabular query? With a 3rd table? With VBA & SQL code? I have chosen the way with a whole lot of VBA code and If-Then statements. It takes about 30 minutes (!!!!) to complete the values of the tables. To be exact, a dozen tables, each have about 1000 rows and about 10 columns in which the values look like this and need to be completed.

But regardless of this, stay with the two tables described above, I am looking for a maybe more elegant and faster way to update Tab01. Does anyone have a good suggestion? Thanks a lot!

This is what my code for Tab01 & Tab02 is looking like: (I use ca. 20 -30 of this for all other tables!!!!)

Sub CompTabsExmpl()

Dim x As Long, y As Long
Dim xRecsMain As Long, xRecsSub As Long

Dim db As DAO.Database
Dim rsTab01 As DAO.Recordset
Dim rsTab02 As DAO.Recordset

Set db = CurrentDb
Set rsTab01 = db.OpenRecordset("Table01", dbOpenDynaset)
Set rsTab02 = db.OpenRecordset("Table02", dbOpenDynaset)

xRecsMain = DCount("*", "Table01")
xRecsSub = DCount("*", "Table02")

rsTab01.MoveFirst
rsTab02.MoveFirst

For y = 0 To xRecsMain - 1
    
   For x = 0 To xRecsSub - 1
   
      If rsTab01.Fields("fieldDATE") = rsTab02.Fields("fieldDATE") And _
         rsTab01.Fields("fieldTIME") = rsTab02.Fields("fieldTIME") And _
         rsTab01.Fields("fieldPLACE") = rsTab02.Fields("fieldPLACE") And _
         IsNull(rsTab01.Fields(fieldX)) And _
         Not IsNull(rsTab02.Fields(fieldX)) Then

         rsTab01.Edit
         rsTab01.Fields("fieldX") = rsTab02.Fields("fieldX")
         'same for all other fields ....
         'same for all other fields ....
         'same for all other fields ....
         '...
         rsTab01.Update
       End If
      
         rsTab01.MoveNext
   Next
    
         rsTab01.MoveFirst
         rsTab02.MoveNext
    
Next

rsTab01.Close
rsTab02.Close
db.Close

End Sub

Advertisement

Answer

Ok, so far, we have this problem:

Get the missing values in table2 and move them to table1

(but matching on the 3 columns). Ok, I would use the query builder for this – and it will run very fast.

So, create a new query. Drop in the two tables (make sure the main table (first table) is drop in first.

Now, drop in the 2nd table. To the query grid add the PK id from the left side table (our main table we are to update).

So, I am going to do this with tblHotels, and tblHotelsB

I want to match on FirstName, HotelName, City, and TRANSFER the two columns HotelTax, and Description.

So, the query builder? drag a join line from the left side table (very important to always drag + drop the line in the correct direction).

You can now run the query – does it run??? (and it should run VERY fast – especially if you have indexing on the 3 columns in each table.

Ok GET THIS QUERY WORKING!!!!!!!

It will look like this:

enter image description here

As noted, double click on the PK id (to put it in the grid). Do the same for ALL the columns you want to copy.

Now, right click in any blank area in the query builder, and change the query type of a update query – like this:

enter image description here

Now, just in the expression for the update, type in the field from tableB

You get this:

enter image description here

NOTE VERY carefull – the Update to (the expression can be anything we want – and that also allows you to type in the columns from the 2nd table.

Of course, make a backup with such a dangers opreation.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement