我对此进行了大量研究,但我不知道出了什么问题。我执行Recordset.GetRows命令时出现错误。 3021错误表示记录集为空,当我检查rs.EOF时,它为true。奇怪的是,如果我接受静态SQL查询并将其放在SQL Developer中,它将运行良好。另外,如果我更改VBA代码以使用类似但更短的查询,它也会正确返回。这两个查询都是静态的,都在SQL Developer中返回。两者都不是动态的。都不使用通配符。
两者均包含至少50条或更多记录。我不知为何要解释为什么记录集对象为空。
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Dim constData As Variant
Worksheets(1).Activate
ActiveSheet.ListObjects("const").DataBodyRange.Value = ""
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
connStr = "Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.1)(PORT=1591)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=somewhere.com)));User ID=SomeLogin;Password=Blahblah"
cn.Open connStr
Debug.Print cn.State 'this comes back as 1, so good.
Set cmd.ActiveConnection = cn
cmd.CommandTimeout = 0
cmd.CommandType = adCmdText
cmd.CommandText = "select c.reported_name,trunc(c.datetime) as MarketDate from sometable "
Set rs = cmd.Execute
Debug.Print rs.EOF ' this is always true for my complex query
constData = rs.GetRows 'this fails then with the 3021 error