简体   繁体   English

当我认为 NoMatch 应该是 False 时访问 VBA NoMatch = True,我哪里出错了?

[英]Access VBA NoMatch = True when I think NoMatch should be False, Where am I going wrong?

Through various kind souls @jericho johnson, and others.通过各种善良的灵魂@jericho johnson 和其他人。 I have VBA code that appears to be working.我有似乎正在工作的 VBA 代码。 Except for one part.除了一部分。 The final 'Else' condition, "Do While Not StrSQL1.NoMatch".最后的“其他”条件,“Do While Not StrSQL1.NoMatch”。 It always equals true.它总是等于真。 Even if the value being referenced in the first Else statement "StrSQL1.FindFirst ([PrimaryKey] = qs.Fields("external_nmad_id"))"即使在第一个 Else 语句中引用的值“StrSQL1.FindFirst ([PrimaryKey] = qs.Fields("external_nmad_id"))”

When I hover over the 'external_nmad_id' it shows a string value.当我将鼠标悬停在“external_nmad_id”上时,它会显示一个字符串值。 When I hover over [PrimaryKey] it shows '[PrimaryKey]="" '.当我将鼠标悬停在 [PrimaryKey] 上时,它会显示 '[PrimaryKey]="" '。 Is the empty quote set referencing a recordset of values - or does that indicate that nothing is being referenced (hence why NoMatch is always True).空引用集是引用值的记录集 - 还是表示没有引用任何内容(因此 NoMatch 始终为 True)。 Or am I missing something elsewhere?还是我在其他地方遗漏了什么?

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]

There are several issues, this is probably the main one:有几个问题,这可能是主要的一个:

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

FindFirst takes a string as argument. FindFirst字符串作为参数。 What you have is an expression, which probably always evaluates to False or Null, since [PrimaryKey] is an undefined variable because it is outside of the string.你所拥有的是一个表达式,它可能总是计算为 False 或 Null,因为[PrimaryKey]是一个未定义的变量,因为它在字符串之外。

It should be它应该是

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

with CSql() from here: https://stackoverflow.com/a/36494189/3820271使用CSql()从这里: https : CSql()
It works for external_nmad_id being number or string.它适用于external_nmad_id是数字或字符串。

To catch errors like this, put Option Explicit at the top of each module.要捕获此类错误,请将Option Explicit放在每个模块的顶部。 It enforces variable declaration and reports undeclared or misspelled variables/constants at compile time.它强制执行变量声明并在编译时报告未声明或拼写错误的变量/常量。
To have this automatically in new modules, set the Require Variable Declaration option in the VBA Editor.要在新模块中自动使用此功能,请在 VBA 编辑器中设置“ 需要变量声明”选项。 This is really a must have for VBA development.这确实是 VBA 开发的必备条件。

Other issues:其他事宜:

1) It is quite confusing to name a recordset variable StrSQL1 . 1) 将记录集变量命名为StrSQL1非常令人困惑。

2) 2)

Do While Not StrSQL1.NoMatch

makes only sense if you do a .FindNext in the loop.只有在循环中执行.FindNext才有意义。 Normally you always need If with .NoMatch .通常你总是需要If.NoMatch

3) 3)

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

is not a good way to do a recordset loop - .RecordCount is only reliably set after a .MoveLast .不是进行记录集循环的好方法 - .RecordCount只能在.MoveLast之后可靠地设置。 Use this instead:改用这个:

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

It's simpler and reliable.它更简单可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM