访问VBA记录字符串比较不工作通配符 [英] Access VBA recordset string comparison not working with wildcard

查看:209
本文介绍了访问VBA记录字符串比较不工作通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel中使用VBA的经验,负载,但在Access 2010中使用VBA经验非常少,我想从一个记录集,其中的领域之一_X结尾删除记录。当我使用通配符比较失败。当我使用一个特定的字符串,比较按预期工作。下面是我使用的code。

 昏暗DB数据库:设置DB = CurrentDb
昏暗TBL作为记录:设置TBL = db.OpenRecordset(WRITEON)
随着TBL
    做直到.EOF
        如果![ACOD] =* _X那么'$ ICP_X工程
            Debug.Print![ACOD]对于测试
            '。删除
        结束如果
    .MoveNext
    循环
结束与
tbl.Close
 

解决方案

使用如同而不是 = 当你真是个文本值比较的模式。

如果[ACOD]类似于* _X呢!     Debug.Print![ACOD] 结束如果

这是应该做的,你想要什么,但我不敢肯定评价每个记录在记录删除的比赛是最好的方式。你可以通过执行删除查询,它使用了相同的模式,以识别哪些行应删除实现相同的结果。

DELETE FROM WRITEON WHERE [ACOD]类似于* _X;

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

这篇关于访问VBA记录字符串比较不工作通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆