ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   SQL Query (https://www.excelbanter.com/excel-programming/322346-sql-query.html)

Bret

SQL Query
 
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.

Harlan Grove

SQL Query
 
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?


Jamie Collins

SQL Query
 

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.


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.

--



All times are GMT +1. The time now is 05:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com