通过各种善良的灵魂@jericho johnson 和其他人。我有似乎正在工作的 VBA 代码。除了一部分。最后的“Else”条件“Do While Not StrSQL1.NoMatch”。它总是等于真。即使在第一个 Else 语句中引用的值“StrSQL1.FindFirst ([PrimaryKey] = qs.Fields("external_nmad_id"))”
当我将鼠标悬停在“external_nmad_id”上时,它会显示一个字符串值。当我将鼠标悬停在 [PrimaryKey] 上时,它显示 '[PrimaryKey]="" '。空引号集是否引用了值的记录集 - 还是表示没有引用任何内容(因此 NoMatch 始终为 True)。还是我在其他地方错过了什么?
Public Sub EditFinalOutput2()
'set variables
Dim i As Long
Dim intCount As Long
Dim qs As DAO.Recordset
Dim ss As DAO.Recordset
Dim StrSQL1 As DAO.Recordset
Dim IRSfileFormatKey As String
Dim external_nmad_id As String
Dim nmad_address_1 As String
Dim nmad_address_2 As String
Dim nmad_address_3 As String
Dim mytestwrite As String
Dim PrimaryKey As String
Dim box13c_Address As String
'open reference set
Set db = CurrentDb
Set qs = db.OpenRecordset("SunstarAccountsInWebir_SarahTest")
'turn popup messages off
'DoCmd.SetWarnings False
With qs.Fields
intCount = qs.RecordCount - 1
For i = 0 To intCount
'===
'=== Condition 1 Test - validate address
'===
If (IsNull(!nmad_address_1) Or (!nmad_address_1 = !nmad_city) Or
(!nmad_address_1 = !Webir_Country) And IsNull(!nmad_address_2) Or
(!nmad_address_2 = !nmad_city) Or (!nmad_address_2 = !Webir_Country) And
IsNull(!nmad_address_3) Or (!nmad_address_3 = !nmad_city) Or
(!nmad_address_3 = !Webir_Country)) Then
'=== Address Not Valid, insert into Review table
DoCmd.RunSQL "INSERT INTO Addresses_ToBeReviewed SELECT
SunstarAccountsInWebir_SarahTest.* FROM SunstarAccountsInWebir_SarahTest
WHERE (((SunstarAccountsInWebir_SarahTest.external_nmad_id)='" &
qs!external_nmad_id & "'));"
Else
Set StrSQL1 = db.OpenRecordset("SELECT RIGHT(IRSfileFormatKey, 10) As PrimaryKey, box13c_Address FROM 1042s_FinalOutput_7 WHERE 'PrimaryKey' = 'external_nmad_id';", dbOpenDynaset)
StrSQL1.FindFirst ([PrimaryKey] = qs.Fields("external_nmad_id"))
'===
'=== Condition 2 Test
'===
If StrSQL1.NoMatch Then
'=== ID Not Found, insert into NotUsed table
DoCmd.RunSQL "INSERT INTO Addresses_NotUsed SELECT SunstarAccountsInWebir_SarahTest.* FROM SunstarAccountsInWebir_SarahTest WHERE (((SunstarAccountsInWebir_SarahTest.external_nmad_id)='" & qs!external_nmad_id & "'));"
Else
'=== Address Found, update record
Do While Not StrSQL1.NoMatch
StrSQL1.Edit
StrSQL1.Fields("box13c_Address") = (qs.Fields("nmad_address_1") & qs.Fields("nmad_address_2") & qs.Fields("nmad_address_3"))
StrSQL1.Update
Loop
End If
....[more code below]