我正在制作一些宏并从用户那里获取日期和数据库。在此基础上,我从db。
获取数据这是我的代码,请查看并分享您是否有任何解决方案。
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Function GetConnectionString() As String
Dim strCn As String
strCn = "Provider=sqloledb;"
strCn = strCn & "Data Source=" & Range("Server") & ";"
strCn = strCn & "Initial Catalog=" & Range("Database") & ";"
If (Range("UserID") <> "") Then
strCn = strCn & "User ID=" & Range("UserID") & ";"
strCn = strCn & "password=" & Range("Pass")
Else
strCn = strCn & "Integrated Security = SSPI"
End If
GetConnectionString = strCn
End Function
Sub Test()
ActiveWorkbook.Sheets("Sheet1").Activate
Dim ws As Worksheet
Dim Sql As String
Dim d As String
d = Range("A2").Value
d = Format(d, "yyyy-mm-dd")
cn.ConnectionTimeout = 100
cn.Open GetConnectionString()
Sql = "select * from config where convert(date,logdate,103)='"& d &"'"
ExecInsert (Sql)
Set rs.ActiveConnection = cn
rs.Open Sql
ActiveWorkbook.Sheets("Sheet2").Activate
Dim ws1 As Worksheet
Range("A2").CopyFromRecordset (rs) 'This is where I'm getting error
cn.Close
End Sub
Sub ExecInsert(selectquery As String)
'End Sub
Dim cmd As New ADODB.Command
cmd.CommandText = selectquery
cmd.CommandType = adCmdText
cmd.ActiveConnection = cn
cmd.Execute
End Sub
Range("A2").CopyFromRecordset (rs)
这是我收到错误的地方
运行类型错误'430'类不支持自动化或不支持预期 接口
我拥有所有的dll并注册了它们。甚至在我的结尾都没有遗漏任何参考。
如果有任何机构面对这个问题,请帮助......
答案 0 :(得分:3)
请更新以下内容
这
Range("A2").CopyFromRecordset (rs)
要
Range("A2").CopyFromRecordset rs
以下是示例代码
Sub sub_success()
Dim rsContacts As ADODB.Recordset
Set rsContacts = New ADODB.Recordset
With rsContacts
.Fields.Append "ContactID", adInteger
End With
rsContacts.Open
rsContacts.AddNew
rsContacts!ContactID = 2123456
rsContacts.Update
Sheet1.Range("A2").CopyFromRecordset rsContacts
End Sub
Sub sub_failure()
Dim rsContacts As ADODB.Recordset
Set rsContacts = New ADODB.Recordset
With rsContacts
.Fields.Append "ContactID", adInteger
End With
rsContacts.Open
rsContacts.AddNew
rsContacts!ContactID = 2123456
rsContacts.Update
Sheet1.Range("A2").CopyFromRecordset (rsContacts)
End Sub