EXCEL(VBA) 如何将 sql 查询值传递给变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12191766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EXCEL(VBA) How to pass an sql query value into a variable?
提问by Gulredy
I'm using MySQL and EXCEL(VBA) How can I pass an sql query valueinto a variable?
我正在使用 MySQL 和 EXCEL(VBA) 如何将 sql 查询值传递给变量?
I have a variable Dim-ed:
我有一个变量 Dim-ed:
Dim sqlVar As Variant
It's variant because I'm not sure which type I should use. I have set the sql query to the variable like this:
它是变体,因为我不确定应该使用哪种类型。我已经将 sql 查询设置为这样的变量:
sqlVar = "SELECT datum FROM arfolyam ORDER BY datum ASC LIMIT 1 "
But like this, it stores the sql query, not the value of the query!
但是像这样,它存储的是sql查询,而不是查询的值!
Any ideas?
有任何想法吗?
回答by Gulredy
...
Dim strSQL as String
...
strSQL = "SELECT column FROM table ORDER BY column DESC LIMIT 1"
Set rs = cn.Execute(strSQL)
variable1 = rs.Fields(0).Value
This way the sql query's value which is in this case is one cell, will be given to the variable1
这样,在这种情况下,sql 查询的值是一个单元格,将被赋予变量 1