Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data fetching from SQL Server in VBA

Tags:

vba

Hi Below is my code, i am not able to fetch the data from my SQL server, its throwing error as

Compiler error :  object required.

There is no problem with the connection, connection is sucessful.

please correct my code, help me with this thing

Private Sub CommandButton1_Click()
Set SQLConn = CreateObject("ADODB.Connection")

SQLConn.Open "provider =sqloledb; Data Source = xxxx; Initial Catalog = jjjj; User Id = yyyy; Password = zzzz"

       MsgBox "Connection Succesful"

Set SQLData = CreateObject("ADODB.Recordset")
With SQLData

    ' Assign the Connection object.
    .ActiveConnection = SQLConn

    ' Extract the required records.
    .Open "select invoice_num, invoice_date, invoice_amount from im_invoice where billing_account = 'HS0076A' and invoice_date ='01-apr-2011'"

    ' Copy the records into cell A1 on Sheet1.
    Sheet1.Range("A1").CopyFromRecordset SQLData

    ' Tidy up
     .Close

End With

SQLConn.Close
Set SQLData = Nothing
Set SQLConn = Nothing

End Sub

Thank you

thank you its working .... :)

like image 994
SQLfanatic Avatar asked Oct 15 '25 14:10

SQLfanatic


1 Answers

Missing "Set"...

' Assign the Connection object. 
Set .ActiveConnection = SQLConn 
like image 91
Tim Williams Avatar answered Oct 19 '25 14:10

Tim Williams



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!