Excel VBA ADO Recordset - SELECT列的问题

时间:2018-01-09 09:36:20

标签: excel vba excel-vba adodb

我得到了

  

运行时错误'-2147467259(80004005)':未指定错误

当我使用以下代码时:

$user->notify()

但是,当我在Set conn1 = CreateObject("ADODB.Connection") conn1.Open strcon1 strquery3 = "SELECT * FROM TABLE_NAME" Set rs3 = CreateObject("ADODB.Recordset") Set rs3 = conn1.Execute(strquery3) 语句中使用较少数量的列时,代码可以正常工作:

SELECT

该表共包含17列。列数有限制吗?如果没有,是否有任何理由我无法获得记录集中的所有17列?

我正在连接Oracle DB。使用的驱动程序 - “Microsoft ODBC for Oracle”

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim cs As String
Dim sql As String


' Initialize objects
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command

' Create connection string and query string
cs = "Provider=SQLOLEDB; Data Source=<YOURSERVER>; Initial Catalog=<YOURDATABSE>; User ID=<YOURUSER>; Password=<YOURPASSWORD>"

sql = "select column1, column2 from table"

' Open connection and execute query
con.Open cs
cmd.CommandType = adCmdText
cmd.ActiveConnection = con
cmd.CommandText = sql

Set rs = cmd.Execute

然后,您可以访问列字段,例如:

Dim col1, col2 As String
col1 = rs.Fields("Column1").Value
col2 = rs.Fields("Column2").Value