Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy data from db to excel if db name is taken from user?

Tags:

excel

vba

I am making some macro and taking date and db from the user. On that basis i am fetching the data from db.

here is my code please take a look and share if any solution you have for this.

    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) this is where i'm getting error

run type error '430' class does not support automation or does not support expected interface

i have all the dlls and registered them as well. and even no reference is missing from my end.

if any body have face this issue please help...

like image 280
honey Avatar asked Dec 06 '25 00:12

honey


1 Answers

kindly update the below line

From

 Range("A2").CopyFromRecordset (rs)

To

Range("A2").CopyFromRecordset rs

Below is sample code

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
like image 131
Santosh Avatar answered Dec 12 '25 05:12

Santosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!