MS Access更新记录基于另一个的值

时间:2018-08-07 12:11:25

标签: vba ms-access access-vba

我有下表(tmpManifest-ID为PK)-数据是从条形码扫描仪填充的,因此我无法控制其创建方式:

enter image description here

但是,我需要填充Box列,如下所示-它需要知道何时更改为正确的Box:

enter image description here

通过更新查询实现此目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

尝试此解决方案-将遍历每条记录并逐一更新。

Dim SQL As String, sUPC As String, sID As String
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT [ID], [UPC], [Description] FROM [tmpManifest] ORDER BY [ID] ASC")

If Not (rs.EOF And rs.BOF) Then
    Do While Not rs.EOF
        sID = CStr(rs![ID])
        If IsNull(rs![Description]) Then
            SQL = "UPDATE [tmpManifest] SET [BOX] = '" & sUPC & "' WHERE [ID] = " & sID
            CurrentDb.Execute SQL
        Else
            sUPC = CStr(rs![UPC])
            SQL = "UPDATE [tmpManifest] SET [BOX] = '" & sUPC & "' WHERE [ID] = " & sID
            CurrentDb.Execute SQL

        End If
        rs.MoveNext
    Loop
End If