object不支持命名参数

时间:2015-08-31 04:14:16

标签: excel vba excel-vba

我正在编写VBA代码来从SQL Server写入数据,连接没有问题。但是当我调试记录集部分系统中的代码时抛出

运行时错误446对象不支持命名参数

这是我的代码

`Sub CopyfromDatabase()
Dim conn As ADODB.Connection
Dim RecordConn As ADODB.Connection
Set conn = New ADODB.Connection
Set RecordConn = New ADODB.Connection
conn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=PH03\Historian;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=OCG;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=HistorianStorage"
conn.Open
'On Error GoTo CloseConnection
With RecordConn
.ActiveConnection = conn
.Source = "connectiontable"
.LockType = adLockReadOnly
.CursorType = adforwardonly
.Open
End With
On Error GoTo CloseRecord
Worksheets.Add
Range("A2").CopyFromRecordset RecordConn
CloseRecord:
RecordConn.Close
'CloseConnection:
conn.Close
End Sub`

帮助我!!

1 个答案:

答案 0 :(得分:2)

Dim RecordConn As ADODB.Connection
Set RecordConn = New ADODB.Connection

应该是

Dim RecordConn As ADODB.Recordset
Set RecordConn = New ADODB.Recordset