问题
I have loads of experience using VBA in Excel but very little experience using VBA in Access 2010. I'm trying to delete records from a recordset where one of the fields ends in "_X". When I use a wildcard the comparison fails. When I use a specific string, the comparison works as expected. Here is the Code that I am using.
Dim db As Database: Set db = CurrentDb
Dim tbl As Recordset: Set tbl = db.OpenRecordset("WRITEON")
With tbl
Do Until .EOF
If ![ACOD] = "*_X" Then '"$ICP_X" works
Debug.Print ![ACOD] 'For testing
'.Delete
End If
.MoveNext
Loop
End With
tbl.Close
回答1:
Use Like
instead of =
when you're comparing a text value to a pattern.
If ![ACOD] Like "*_X" Then
Debug.Print ![ACOD]
End If
That should do what you want, but I'm not so sure evaluating each record in a recordset to delete the matches is the best way. You could accomplish the same result by executing a DELETE
query which uses that same pattern to identify which rows should be deleted.
DELETE FROM WRITEON
WHERE [ACOD] Like "*_X";
来源:https://stackoverflow.com/questions/28842454/access-vba-recordset-string-comparison-not-working-with-wildcard