Recordset2.NoMatch 属性 (DAO)
适用于:Access 2013、Office 2013
指示特定记录是通过使用 Seek 方法还是 Find 方法找到的(仅适用于 Microsoft Access 工作区)。
语法
表达式 .NoMatch
表达 一个代表 Recordset2 对象的变量。
说明
打开或创建 Recordset 对象时,其 NoMatch 属性设置为 False。
若要定位记录,请对表类型的 Recordset 对象使用 Seek 方法,或者对动态集类型或快照类型的 Recordset 对象使用 Find 方法之一。 检查 NoMatch 属性设置以查看是否找到了该记录。
如果 Seek 或 Find 方法不成功,且 NoMatch 属性为 True,则当前记录将不再有效。 如果需要返回到当前记录,请在使用 Seek 方法或 Find 方法之前获取当前记录的书签。
注意
对 Recordset 对象使用任何 Move 方法将不会影响其 NoMatch 属性设置。
示例
以下示例使用 NoMatch 属性确定 Seek 和 FindFirst 是否成功,如果未成功,则提供相应的反馈。 若要使该过程运行,需要使用 SeekMatch 和 FindMatch 过程。
Sub NoMatchX()
Dim dbsNorthwind As Database
Dim rstProducts As Recordset2
Dim rstCustomers As Recordset2
Dim strMessage As String
Dim strSeek As String
Dim strCountry As String
Dim varBookmark As Variant
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
' Default is dbOpenTable; required if Index property will
' be used.
Set rstProducts = dbsNorthwind.OpenRecordset("Products")
With rstProducts
.Index = "PrimaryKey"
Do While True
' Show current record information; ask user for
' input.
strMessage = "NoMatch with Seek method" & vbCr & _
"Product ID: " & !ProductID & vbCr & _
"Product Name: " & !ProductName & vbCr & _
"NoMatch = " & .NoMatch & vbCr & vbCr & _
"Enter a product ID."
strSeek = InputBox(strMessage)
If strSeek = "" Then Exit Do
' Call procedure that seeks for a record based on
' the ID number supplied by the user.
SeekMatch rstProducts, Val(strSeek)
Loop
.Close
End With
Set rstCustomers = dbsNorthwind.OpenRecordset( _
"SELECT CompanyName, Country FROM Customers " & _
"ORDER BY CompanyName", dbOpenSnapshot)
With rstCustomers
Do While True
' Show current record information; ask user for
' input.
strMessage = "NoMatch with FindFirst method" & _
vbCr & "Customer Name: " & !CompanyName & _
vbCr & "Country: " & !Country & vbCr & _
"NoMatch = " & .NoMatch & vbCr & vbCr & _
"Enter country on which to search."
strCountry = Trim(InputBox(strMessage))
If strCountry = "" Then Exit Do
' Call procedure that finds a record based on
' the country name supplied by the user.
FindMatch rstCustomers, _
"Country = '" & strCountry & "'"
Loop
.Close
End With
dbsNorthwind.Close
End Sub
Sub SeekMatch(rstTemp As Recordset2, _
intSeek As Integer)
Dim varBookmark As Variant
Dim strMessage As String
With rstTemp
' Store current record location.
varBookmark = .Bookmark
.Seek "=", intSeek
' If Seek method fails, notify user and return to the
' last current record.
If .NoMatch Then
strMessage = _
"Not found! Returning to current record." & _
vbCr & vbCr & "NoMatch = " & .NoMatch
MsgBox strMessage
.Bookmark = varBookmark
End If
End With
End Sub
Sub FindMatch(rstTemp As Recordset, _
strFind As String)
Dim varBookmark As Variant
Dim strMessage As String
With rstTemp
' Store current record location.
varBookmark = .Bookmark
.FindFirst strFind
' If Find method fails, notify user and return to the
' last current record.
If .NoMatch Then
strMessage = _
"Not found! Returning to current record." & _
vbCr & vbCr & "NoMatch = " & .NoMatch
MsgBox strMessage
.Bookmark = varBookmark
End If
End With
End Sub