create connection in excel code example
Example: excel accessing sql database
Function QuerySQL_DAO(sql$, dbFile$)
Const DAO = "DAO.DBEngine.120"
Set QuerySQL_DAO = CreateObject(DAO).OpenDatabase(dbFile).OpenRecordset(sql)
QuerySQL_DAO.MoveLast
QuerySQL_DAO.MoveFirst
End Function
MsgBox QuerySQL_DAO("SELECT * from Titanic", "C:\Ships.accdb").RecordCount
Function QuerySQL_ADO(sql$, dbFile$)
Dim cnx
Set cnx = CreateObject("ADODB.Connection")
cnx.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFile
Set QuerySQL_ADO = CreateObject("ADODB.Recordset")
With QuerySQL_ADO
.CursorLocation = 3
.CursorType = 1
.Open sql, cnx
End With
End Function
MsgBox QuerySQL("SELECT * from Titanic", "C:\Ships.accdb").RecordCount