我正在制作一些宏并从用户那里获取日期和数据库。在此基础上,我正在从 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 并注册了它们。我什至没有参考资料。
如果有人遇到这个问题,请帮助...