使用表名别名获取列在VBA中不起作用

时间:2015-10-22 14:07:30

标签: oracle vba excel-vba excel

我正在尝试通过VBA连接到Oracle表并获取数据。我的情况是多次引用一个表来进行数据透视。但是在通过VBA运行查询时,由于我通过表别名获取数据,所以它会抛出错误:下面是一个虚拟代码。

Sub Button1_Click()
    Dim strDataSource, strUsername, strPassword, strConnectionString, strquery As String
    Dim objADODBConnection, objADODBRecordset As Object
        strDataSource = "xxxx"
        strUsername = "xxxx"
        strPassword = "xxxx"

        strConnectionString = "Provider=MSDAORA;Data Source=" & strDataSource & ";Persist Security Info=True;Password=" & strPassword & ";User ID=" & strUsername
        Set objADODBConnection = CreateObject("ADODB.Connection")

        objADODBConnection.Open strConnectionString
        Set objADODBRecordset = CreateObject("ADODB.Recordset")

        strquery = "SELECT T1.Column1 FROM Table1 AS T1 WHERE Column2='XXXX' AND Column3='XXXX'"

        Set objADODBRecordset = objADODBConnection.Execute(strquery)

        MsgBox objADODBRecordset.Fields(0).Value

        objADODBRecordset.Close
        objADODBConnection.Close
        Set objADODBRecordset = Nothing
        Set objADODBConnection = Nothing

End Sub

相同的查询运行正常,没有表别名。请建议!!

1 个答案:

答案 0 :(得分:0)

FROM Table1 AS T1 

你应该删除' AS'从条件 -

FROM Table1 T1