Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Shawn Lopez   on Jan 28 In MS Office Category.

  
Question Answered By: Jackson Williams   on Jan 28

Please find the code mentioned below.

I need a help  in displaying  recordset values(from SQL) in excel  VBA form. I am
importing the data  from SQL to EXCEL using vba  and I could get the result  in the
excel sheet. But now, I would like to create  one form  and I want the values
retrieved from SQL Query through recordset  be displayed  on the User form. I am
able to display  only one record  on the form, but not all the records.

Can some one please suggest  me on this , how to display a set  of records  we
retrieve from SQL on the Form.

Private Sub CommandButton1_Click()
Set cnhrbpo = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the hrbpo database.
strConn = "driver={SQL Server};server=hrbpochennai" & sServer & _
";database=hrbpo;uid=hr;pwd=hr"
'Now open the connection.
cnhrbpo.Open strConn
' Create a recordset object.
Dim rshrbpo As ADODB.Recordset
Set rshrbpo = New ADODB.Recordset
With rshrbpo
' Assign the Connection object.
.ActiveConnection = cnhrbpo
' Extract the required records.
.Open "SELECT * FROM orders "
' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A1").CopyFromRecordset rshrbpo
' Tidy up
.Close
End With
cnhrbpo.Close
Set rshrbpo = Nothing
Set cnhrbpo = Nothing

End Sub

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Help needed in SQL to Excel VBA Form Or get search suggestion and latest updates.


Tagged: