我使用 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
在我的计划中,我想 Dim 一个整数变量来存储
month
在 VBA 中。然后,变量 - month
获取组合框结果,以便将 3 存储到 month
.最后,
integer variable-month
可以传递给SQL语句*** sSQLSting = "SELECT officer ,sum(mkt) from [$DB] where month=***group by officer
这是我的计划。
是否可以捕获组合框结果并将值传递给 SQL 语句?
最佳答案
像这样的东西将能够在 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"
粗略地说,您将不得不编辑代码以适合您的组合框名称等。
关于sql - 是否能够将 VBA 变量传递给 ADO SQL 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38392371/