Propiedad Recordset.NoMatch (DAO)
Se aplica a: Access 2013, Office 2013
Indica si se ha encontrado un registro determinado utilizando el método Seek o uno de los métodos Find (solo en áreas de trabajo de Microsoft Access).
Sintaxis
expression .NoMatch
expression Variable que representa un objeto Recordset.
Comentarios
Cuando abre o crea un objeto Recordset, su propiedad NoMatch se establece en False.
Para localizar un registro, use el método Seek en un objeto Recordset tipo tabla o use uno de los métodos Find en un objeto Recordset de tipo Dynaset o Snapshot. Compruebe el valor de la propiedad NoMatch para ver si se encontró el registro.
Si el método Seek o el método Find no se han ejecutado correctamente y la propiedad NoMatch es True, el registro actual ya no será válido. Si va a necesitar volver a un registro, asegúrese de obtener el marcador del registro actual antes de utilizar el método Seek o un método Find.
Nota:
Cuando se usa alguno de los métodos Move en un objeto Recordset no se afecta al valor de su propiedad NoMatch.
Ejemplo
En este ejemplo se utiliza la propiedad NoMatch para determinar si Seek y FindFirst han tenido un resultado correcto y, en caso contrario, proporcionar información adecuada. Se necesitan los procedimientos SeekMatch y FindMatch para que pueda ejecutarse este procedimiento.
Sub NoMatchX()
Dim dbsNorthwind As Database
Dim rstProducts As Recordset
Dim rstCustomers As Recordset
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 Recordset, _
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
El siguiente ejemplo muestra cómo usar el método Seek para encontrar un registro en una tabla vinculada.
Código de ejemplo proporcionado por la Referencia del programador de Microsoft Access 2010.
Sub TestSeek()
' Get the path to the external database that contains
' the tblCustomers table we're going to search.
Dim strMyExternalDatabase
Dim dbs As DAO.Database
Dim dbsExt As DAO.Database
Dim rst As DAO.Recordset
Dim tdf As DAO.TableDef
Set dbs = CurrentDb()
Set tdf = dbs.TableDefs("tblCustomers")
strMyExternalDatabase = Mid(tdf.Connect, 11)
'Open the database that contains the table that is linked
Set dbsExt = OpenDatabase(strMyExternalDatabase)
'Open a table-type recordset against the external table
Set rst = dbsExt.OpenRecordset("tblCustomers", dbOpenTable)
'Specify which index to search on
rst.Index = "PrimaryKey"
'Specify the criteria
rst.Seek "=", 123
'Check the result
If rst.NoMatch Then
MsgBox "Record not found."
Else
MsgBox "Customer name: " & rst!CustName
End If
rst.Close
dbs.Close
dbsExt.Close
Set rst = Nothing
Set tdf = Nothing
Set dbs = Nothing
End Sub