遍历特定列以匹配条件并更新列

时间:2019-02-17 00:15:25

标签: database vba ms-access access-vba

我正在尝试使用VBA代码重建一些MS Access更新查询逻辑,因为更新逻辑缺少一些参数。

Google drive link table

在我的数据库“发票帐户分配”中,有3个表:

  • 帐户映射
  • Load_ID
  • 状态ID。

表(Load_IDStatus_ID)中的默认发票帐户由Account_Mapping表中的发票帐户更新。

主键是“ ID”,因为它存在于表中-Load_ID和Status_ID。表Account_Mapping和Load_ID可以通过以下方式联接:
原籍国和原籍国,
原始邮政编码和邮政编码,
目的地国家和地区,以及
目的地邮政编码和目的地邮政编码。

在account_mapping表中,有几个“ isnotnull”,这意味着字符串值可以接受任何值。

使用更新查询进行此更新并不可靠。理想情况下,应执行循环以首先检查起源国家/地区,然后根据起源邮政编码,目的地国家/地区和邮政编码找到匹配项。

UPDATE 
   ([Load_ID] 
    INNER JOIN 
    Account_Mapping 
        ON  ([Load_ID].Origin country = Account_Mapping.[Origin country]) AND 
            ([Load_ID].Destination = Account_Mapping.[destination country]) AND 
            ([Load_ID].Origin country = Account_Mapping.[Origin country])) 
    INNER JOIN 
    Status_ID 
        ON [Load_ID].ID = Status_ID.[ID] 
SET 
    [Load_ID].Default Invoice Account = [Invoice Account], 
    Status_ID.[ID] = [Invoice Account]
WHERE 
    ((([Load_ID].Origin country)=));

理想情况下是“ for”循环

'A loop that iterates 4 columns looking for a match based on 4 matches.
'Also, incase of "isnotnull", it simply means ""not empty. 

Dim db as Database
Dim rs as Recordsset

Set db = CurrentDb
Set rs = db.OpenRecordset("tblestudents",dbOpenSnapshot)

my_rs.MoveLast
lastrecord = my_rs.RecordCount

For i = 0 To rs.RecordCount 
    For j = 0 To rs.RecordCount 
        For k = 0 To rs.RecordCount
            'if the [Load_ID].Origin country = Account_Mapping.[Origin country] 
            'And
            ' if the [Load_ID].Origin Postal code = Account_Mapping.[Postal code]
            '     And
            '  if the [Load_ID].Destination country = 
            'Account_Mapping.[Destination]
            '     And
            'if the [Load_ID]. Destination Postal code= 
            'Account_Mapping.[Destination Postal code]
            'update [Load_ID].Default Invoice Account with Account_Mapping.Invoice 'Account
            'for the "isnotnull" criteria, 
            '     Else
            'If Account_Mapping.[Origin Postal code] = "Isnotnull"
            '     Or
            'if the Account_Mapping. Destination= "Isnotnull"
            '     Or
            'if the Account_Mapping. Destination Postal code= "Isnotnull" 
            'update [Load_ID].Default Invoice Account with Account_Mapping.Invoice 'Account
            '   rs.MoveNext
        Next k
    Next j
Next i

rs.Close

Set.Close
Set rs = Nothing
db.Close

如何循环3个表,匹配条件并更新表列值。 非常感谢您对我的帮助。

0 个答案:

没有答案