【问题标题】:Getting Access form field values in SQL string in the VBA在 VBA 中获取 SQL 字符串中的 Access 表单字段值
【发布时间】:2014-11-26 23:53:45
【问题描述】:

我可能会问一些愚蠢的问题,因为我是 ms 访问和 VBA 的新手,并且一直坚持这一点,

我的 Access VBA 中有一个 SQL 字符串,如下例所示

select customer_id from customer_table where customer_name in ('Max','Jack')

我希望用户使用 ms 访问未绑定文本框 名称作为 txtCusName 在查询中添加/替换 customer_name。我尝试如下修改 SQL 字符串,但出现编译错误,请告诉我我在哪里做错了?

select customer_id from customer_table where customer_name in ( & Me.txtCusName & )

同样,这只是示例,我对我的项目有更大的查询,如果我对值进行硬编码,但想知道如何从访问表单中获取信息,这将起作用。

【问题讨论】:

  • 或许读过基础教程?
  • 我完全同意,感谢您的建议,如果您能分享解决方案,那就太好了。
  • 网上有很多教程资源。
  • 如果是单个值,我本可以使用这个 customer_name = " & "'" & Me.txtCusName & "'",但我想知道如何在SQL“IN”函数,这就是我迷路的地方。
  • 期望用户正确格式化所需的字符串只是自找麻烦,对用户不友好。如果您使用可用值填充列表框并从所选项目构建您需要的内容,您(和您的用户)会更好。

标签: vba ms-access


【解决方案1】:

我编写了如下代码,以允许用户在表单字段中输入“Max”、“Jack”来获取查询过滤器并正常工作。

select customer_id from customer_table where customer_name in ( " & "" & Me.txtCusName & ")"

【讨论】:

    猜你喜欢
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多