gpt4 book ai didi

ms-access - .FindFirst VBA MS Access 函数的意外行为 : . NoMatch 始终返回 true

转载 作者:行者123 更新时间:2023-12-04 05:12:26 26 4
gpt4 key购买 nike

查看下面的以下代码行。

Dim rst As DAO.Recordset
Dim strSql As String

strSql = "SELECT * FROM MachineSettingsT;"
Set rst = DBEngine(0)(0).OpenRecordset(strSql)

rst.FindFirst "Microwave = " & "'" & Me.Microwave & "'" & " AND WashingMachine =" & "'" & Me.WashingMachine & "'" & " AND Element1 =" & "'" & Me.Element1 & "'" _
& "AND Element3 =" & "'" & Me.Element3 & "'" & "AND Dryer =" & "'" & Me.Dryer & "'" & "AND SettingID <>" & "'" & Me.SettingID & "'"

If Not rst.NoMatch Then
Cancel = True
If MsgBox("Setting already exists; go to existing record?", vbYesNo) = vbYes Then
Me.Undo
DoCmd.SearchForRecord , , acFirst, "[SettingID] = " & rst("SettingID")
End If
End If
rst.Close

问题:如果 rst.FindFirst 表达式中的任何值是 Null,则 rst.NoMatch 始终返回 true,即使在被评估的字段中存在具有匹配 Null 值的记录。这种行为是预期的还是可能存在另一个潜在问题。我查了 msdn页面,但它没有提供有关此类行为的信息。

最佳答案

考虑一种不同的方法。请注意,这是针对一组文本数据类型字段。

Dim rst As DAO.Recordset
Dim strSql As String
Dim db As Database

Set db=CurrentDB

strSql = "SELECT * FROM MachineSettingsT WHERE 1=1 "
''Set rst = db.OpenRecordset(strSql)

If not IsNull(Me.Microwave) Then
strWhere = " AND Microwave = '" & Me.Microwave & "'"
End if
If not IsNull(Me.WashingMachine) Then
strWhere = strWhere & " AND WashingMachine ='" & Me.WashingMachine & "'"
End if
If not IsNull(Me.Element1) Then
strWhere = strWhere & " AND Element1 ='" & Me.Element1 & "'"
End if
If not IsNull(Me.Element3) Then
strWhere = strWhere & " AND Element3 ='" & Me.Element3 & "'"
End if
If not IsNull(Me.Dryer) Then
strWhere = strWhere & " AND Dryer ='" & Me.Dryer & "'"
End if

Set rst = db.OpenRecordset(strSql & strWhere)

关于ms-access - .FindFirst VBA MS Access 函数的意外行为 : . NoMatch 始终返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14731691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com