excel vba query access database code example
Example 1: excel vba query access database
Function QuerySQL(sql$, dbFile$)
Dim cnx
Set cnx = CreateObject("ADODB.connection")
cnx.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFile
Set QuerySQL = CreateObject("ADODB.Recordset")
With QuerySQL
.CursorLocation = 3
.CursorType = 1
.Open sql, cnx
End With
End Function
MsgBox QuerySQL("SELECT * from Titanic", "C:\Ships.accdb").RecordCount
Example 2: vba query access database
Function QuerySQL(sql$, dbFile$)
Dim cnx
Set cnx = CreateObject("ADODB.connection")
cnx.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFile
Set QuerySQL = CreateObject("ADODB.Recordset")
With QuerySQL
.CursorLocation = 3
.CursorType = 1
.Open sql, cnx
End With
End Function
MsgBox QuerySQL("SELECT * from Titanic", "C:\Ships.accdb").RecordCount
Example 3: excel vba query database
Function QuerySQL(sql$, dbFile$)
Dim cnx
Set cnx = CreateObject("ADODB.Connection")
cnx.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFile
Set QuerySQL = CreateObject("ADODB.Recordset")
With QuerySQL
.CursorLocation = 3
.CursorType = 1
.Open sql, cnx
End With
End Function
MsgBox QuerySQL("SELECT * from Titanic", "C:\Ships.accdb").RecordCount