Access VBA recordset string comparison not working with wildcard

夙愿已清 提交于 2019-12-24 03:44:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!