如何匹配访问和Excel记录并更新访问数据库?



>我在访问表中有一列带有供应商名称的列,该列每天更新(从空白到名称(的交货编号(唯一(。

每天,我都想从SAP中提取数据并更新供应商名称在系统中更新的记录。

我需要一个SQL查询,它将匹配Access和Excel工作簿中的交货编号,并根据Excel工作捕获供应商名称并更新Access数据库。

我编写的代码一次只能更新一个名称,但我想一次性更新所有内容。

Sub Update()
Sheets("Carrier Updated").Select
'Initialize all variables
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim stDB As String, stSQL As String, stProvider As String
Dim X As Variant
X = Range("A2").Value
Dim Y As Variant
Y = Range("B2").Value
stDB = "C:UsersyemdemaOneDrive - EcolabDesktopDrive - ADelivery CreationDB BackupTest_1.accdb"
stProvider = "Microsoft.ACE.OLEDB.12.0"
'Opening connection to database
With cn
.ConnectionString = stDB
.Provider = stProvider
.Open
End With
'SQL Statement of what I want from the database
stSQL = "UPDATE Delivery_Creation set [Carrier Updated Later] = '" & Y & "' where[Delivery] = '" & X & "'"
Set rs = cn.Execute(stSQL)
MsgBox ("Carrier has been updated")
'Looping through the records I pulled and inserting the data into the comboBox
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub

由于工作表中找到的每对值的 X 和 Y 值可能不同,因此可以更新一组值(就像现在一样(,也可以将它们收集在(临时(表中并使用该表调用更新查询。不过,没有太大的区别。

或者,您可以"反向"该过程,使用Access将工作表中的区域链接为链接表,然后使用该表运行更新查询。

最新更新