【问题标题】:Is it able to pass the VBA variable into ADO SQL statement?是否能够将 VBA 变量传递给 ADO SQL 语句?
【发布时间】:2016-07-15 17:25:16
【问题描述】:

我使用 ADO 在 excel 中运行 SQL。

SQL 是

 sSQLSting = "SELECT  officer ,sum(mkt) from [$DB] where month=2 group by officer"

在我的系统中,有一个由 YES 和 NO 选项组成的组合框。

对于 Yes ,SQL where 语句将变为 where month=3

对于 No ,SQL where 语句将变为 where month=4

在我的计划中,我想将一个整数变量调暗以将month 存储在 VBA 中。然后,变量 -month 获取组合框结果,以便将 3 存储到 month


最后,integer variable-month 可以传递给 SQL 语句 ***

     sSQLSting = "SELECT  officer ,sum(mkt) from [$DB] where month=***group by officer

这是我的计划。 是否可以捕获组合框结果并将值传递给 SQL 语句?

【问题讨论】:

    标签: sql vba excel ado


    【解决方案1】:

    这样的东西将能够在 SQL 语句中传递一个变量。它仍然是一个字符串,所以你只需要把它当作一个字符串来对待。

    Dim month As Long
     month = 2
     sSQLSting = "SELECT  officer ,sum(mkt) from [$DB] where month= " & month & " group by officer"
    

    对于组合框,类似这样的,

    Dim month As Long
         month = combobox1.Value
         sSQLSting = "SELECT  officer ,sum(mkt) from [$DB] where month= " & month & " group by officer"
    

    您将不得不编辑代码以适合您的组合框名称等。

    【讨论】:

    • 现在我将组合框值更改为 1-12 .Dim month As Long .AddItem 1 .AddItem 2 .... month = combobox1.Value where month= " & month &" 它不起作用
    • @Vito 如果您使用的是/否,则必须将整数传递给变量。
    • 如果我将组合框选项更改为additem 1 .additem 2 .... .additem 12 (整数格式的12个月),wt怎么样。然后通过month = combobox1.Value捕捉月份。最后,将值传递给SQLwhere month= " & month &.it不工作
    • @Vito 组合框到底是什么?
    • 组合框让用户选择月份。因此用户可以从 1-12 中选择月份。然后选择将存储到变量 month 并传递给 SQL - where 子句 where month ='&month&"
    猜你喜欢
    • 2015-08-08
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2014-08-22
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多