0

通过各种善良的灵魂@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]
4

1 回答 1

2

有几个问题,这可能是主要的一个:

StrSQL1.FindFirst ([PrimaryKey] = qs.Fields("external_nmad_id"))

FindFirst接受一个字符串作为参数。你所拥有的是一个表达式,它可能总是计算为 False 或 Null,[PrimaryKey]因为它是一个未定义的变量,因为它在字符串之外。

它应该是

StrSQL1.FindFirst "[PrimaryKey] = " & CSql(qs.Fields("external_nmad_id").Value)

CSql()这里开始:https
://stackoverflow.com/a/36494189/3820271 它适用于external_nmad_id数字或字符串。

要捕获这样的错误,请将其放在Option Explicit每个模块的顶部。它强制执行变量声明并在编译时报告未声明或拼写错误的变量/常量。
要在新模块中自动执行此操作,请在 VBA 编辑器中设置Require Variable Declaration选项。这确实是VBA开发的必备。

其他问题:

1) 命名记录集变量非常令人困惑StrSQL1

2)

Do While Not StrSQL1.NoMatch

.FindNext只有在循环中执行 a 时才有意义。通常你总是需要Ifwith .NoMatch

3)

intCount = qs.RecordCount - 1
For i = 0 To intCount
    ' ...
    qs.MoveNext
Next i

不是进行记录集循环的好方法 -.RecordCount仅在.MoveLast. 改用这个:

Do While Not qs.EOF
    ' ...
    qs.MoveNext
Loop

它更简单可靠。

于 2018-09-13T22:13:03.190 回答