简体   繁体   English

如何使用查询或 VBA 和 SQL 更新 MS ACCESS 中的表

[英]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.两个访问表: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.暂时搁置主键和 ID,为了检查内容是否相同,我从每个表中选择了 3 个必须相同的列:日期、地点和时间。

Now the following:现在如下:

Tab01.column01 contains values in rows 1, 4, 5, 8, 10 Tab01.column01 包含第 1、4、5、8、10 行中的值

Tab01.column02 contains values in rows 2, 3, 10 values Tab01.column02 包含第 2、3、10 行中的值

Tab01.column03 contains values in rows 1, 2, 3, 4, 5, 6, 7, 8, 9 Tab01.column03 包含第 1、2、3、4、5、6、7、8、9 行中的值


Tab02.column01 contains values in the rows 1, 2, 3, 4, 5, 9, 10 Tab02.column01 包含第 1、2、3、4、5、9、10 行中的值

Tab02.column02 contains no values Tab02.column02 不包含任何值

Tab02.column03 contains values in rows 1, 10 Tab02.column03 包含第 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):如果我想用 Tab02.columnX 中的值完成 Tab01.columnX 中的缺失值(通过使用日期、时间、地点设置为 ID 的 3 个值):

How would one proceed here?怎么会在这里进行? With a tabular query?使用表格查询? With a 3rd table?有第三张桌子? With VBA & SQL code?使用 VBA 和 SQL 代码? I have chosen the way with a whole lot of VBA code and If-Then statements.我选择了大量 VBA 代码和 If-Then 语句的方式。 It takes about 30 minutes (!!!!) to complete the values of the tables.完成表的值大约需要 30 分钟 (!!!!)。 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.准确地说,十几个表,每个表大约有 1000 行和大约 10 列,其中的值看起来像这样,需要完成。

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.但无论如何,请继续使用上面描述的两个表,我正在寻找一种可能更优雅、更快速的更新 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!!!!)这就是我的 Tab01 和 Tab02 代码的样子:(我对所有其他表使用了大约 20 -30 个!!!!)

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

Ok, so far, we have this problem:好的,到目前为止,我们有这个问题:

Get the missing values in table2 and move them to table1获取 table2 中的缺失值并将它们移动到 table1

(but matching on the 3 columns). (但匹配 3 列)。 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).向查询网格添加左侧表(我们要更新的主表)中的 PK id。

So, I am going to do this with tblHotels, and tblHotelsB所以,我要用 tblHotels 和 tblHotelsB 来做这件事

I want to match on FirstName, HotelName, City, and TRANSFER the two columns HotelTax, and Description.我想匹配 FirstName、HotelName、City 和 TRANSFER 两列 HotelTax 和 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. (它应该运行得非常快 - 特别是如果你在每个表的 3 列上都有索引。

Ok GET THIS QUERY WORKING!!!!!!!好的,让这个查询工作!!!!!!!

It will look like this:它看起来像这样:

在此处输入图片说明

As noted, double click on the PK id (to put it in the grid).如前所述,双击 PK id(将其放入网格中)。 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:现在,右键单击查询构建器中的任何空白区域,并更改更新查询的查询类型 - 如下所示:

在此处输入图片说明

Now, just in the expression for the update, type in the field from tableB现在,就在更新的表达式中,输入 tableB 中的字段

You get this:你得到这个:

在此处输入图片说明

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.当然,对这种危险的操作进行备份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM