简体   繁体   中英

Access VBA recordset string comparison not working with wildcard

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 

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM