SQL Query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Please,
I need sample of VBA code of the best way to:
1) do a SQL connection from Excel
2) Execute a query statement that returns the result to a cell.

thank you in advance.
 
Bret wrote...
I need sample of VBA code of the best way to:
1) do a SQL connection from Excel
2) Execute a query statement that returns the result to a cell.

Why not use the SQL.REQUEST add-in function?
 
Bret said:
I need sample of VBA code of the best way to:
1) do a SQL connection from Excel
2) Execute a query statement that returns the result to a cell.

Sub test()
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
rs.Open _
"SELECT CompanyName FROM Customers", _
"Provider=SQLOLEDB.1;Data Source=(local);" & _
"Initial Catalog=Northwind;" & _
"User Id=sa;Password=;"
Sheet1.Range("A1").CopyFromRecordset rs
End Sub

Jamie.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top