VBA Exec SQL语句

时间:2014-01-10 10:44:46

标签: sql excel-vba vba excel

我多年来一直在看这个陈述,我确信它有点小。但是我无法通过VBA获取数据。奇怪的是它在SQL中作为一个语句起作用,但是一旦我通过VBA运行它就会出现运行时错误。我真的可以用第二双眼睛。

stSQL = "EXEC('SELECT T1.Name FROM [dbo].[Tbl1] T1 " & _
            "INNER JOIN [dbo].[Tbl2] T2 ON T1.ID = T2.ID " & _
            "WHERE T2.NAME = '" & sBizUnit & "' AND Desc = '''' ORDER BY Name')"

sBizUnit是组合框中的值:

sBizUnit = CB_BizU.Value

提前致谢

1 个答案:

答案 0 :(得分:2)

您运行应用程序的帐户是否有权运行EXEC命令?

我注意到的另一点是你没有逃过第一组引号,也许试试:

stSQL = "EXEC('SELECT T1.Name FROM [dbo].[Tbl1] T1 " & _
        "INNER JOIN [dbo].[Tbl2] T2 ON T1.ID = T2.ID " & _
        "WHERE T2.NAME = ''" & sBizUnit & "'' AND Desc = '''' ORDER BY Name')"