I'm having an issue trying to figure out how to use noMatch with my code. I have a spreadsheet where a "PackNum" is compared to my Access recordset (query) and when it finds a match it inputs the data in excel. However, when there is no match it just grabs the first record. I'd like it so if there is no match to call out that there is no match available.
Original Code
For i = 2 To lrow
UpSAddPool.FindFirst ("[Packnum]= '" & Adds.Range("A" & i).Value & "'")
If Adds.Range("A" & i).Value <> "" Then
Adds.Range("C" & i).Value = UpSAddPool.Fields("[Year]").Value
Adds.Range("D" & i).Value = UpSAddPool.Fields("[Catid]").Value
Else
End If
Next i
Code I was messing with but obviously it's not right cause my line If UpSAddPool.FindFirst("[Packnum]= '" & Adds.Range("A" & i).Value & "'").NoMatch
is going to error.
Any help on this would be greatly appreciated!
For i = 2 To lrow
If UpSAddPool.FindFirst("[Packnum]= '" & Adds.Range("A" & i).Value & "'").NoMatch Then
Adds.Range("D" & i).Value = "No match found"
Adds.Range("D" & i).Value = "No match found"
Else
UpSAddPool.FindFirst ("[Packnum]= '" & Adds.Range("A" & i).Value & "'")
If Adds.Range("A" & i).Value <> "" Then
Adds.Range("C" & i).Value = UpSAddPool.Fields("[Year]").Value
Adds.Range("D" & i).Value = UpSAddPool.Fields("[Catid]").Value
Else
End If
End If
Next i
Assuming UpSAddPool
is a DAO.Recordset: According to the documentation, when FindFirst
doesn't find anything, the property NoMatch
is set to True.
If a record matching the criteria isn't located, the current record pointer is unknown, and the NoMatch property is set to True.
But your code uses that wrong. FindFirst
is a Method, but it doesn't return anything (it's not a function). Using UpSAddPool.FindFirst(...).NoMatch
would work only if the Method would return a RecordSet.
So instead, first issue the FindFirst
and after that check the NoMatch
-property (untested as I don't have Access available at the moment, but you get the idea)
UpSAddPool.FindFirst("[Packnum]= '" & Adds.Range("A" & i).Value & "'")
If UpSAddPool.NoMatch Then
(...)
Else
(...)
End If