查看下面的代碼行。使用.FindFirst VBA MS Access函數的意外行爲:.NoMatch始終返回true
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表達值都空然後rst.NoMatch即使當存在與該領域的一個匹配空值的記錄被評估總是返回true。這是行爲預期還是可能存在另一個潛在問題。我檢查了msdn頁面,但沒有提供有關這種行爲的信息。
+1很好的使用'Where 1 = 1'來確保你不需要擔心是否使用基於null的'Where'或'And'。 –
非常感謝。奇蹟般有效。請注意,雖然需要在if語句中添加子句,以便仍然評估Null條件。例如如果不是IsNull(Me.Microwave)那麼strWhere = strWhere&... Else strWhere =「AND Microwave IS NULL」End If – jaromey
更多關於Where 1 = 1 [這裏](http://stackoverflow.com/questions/ 242822/why-would-someone-use-where-1-1-and-conditions-in-a-sql-clause) – jaromey