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