SQL Query无法识别工作表名称并引发错误

时间:2015-06-23 15:42:08

标签: sql excel-vba vba excel

myName = GetmyName() ' gets the name of sheet starting with 'Data' For e.g. sheet name can be 'Data-20150205'
myName = "[" & myName & "$]"
rs.Open " SELECT [State] FROM myName WHERE (([Country]='United States') AND ([Capital]='Boston')) ", cn, adOpenKeyset, adLockReadOnly

这里我收到一个错误,它无法找到myName,其中它实际上是其中一个工作表。请更正

1 个答案:

答案 0 :(得分:3)

myName是可变的,你需要像下面这样使用它:

rs.Open " SELECT [State] FROM " & myName & " WHERE (([Country]='United States') AND ([Capital]='Boston')) ", cn, adOpenKeyset, adLockReadOnly