'How to query MS Access using Excel VBA
I have been using the following method to query an MS Access database using Excel VBA for over a decade. All of a sudden yesterday it stopped working. I tried all of the following - reboot, move files to local machine, reinstall Office, a different PC, compact repair database, new database, new excel file, updating reference library (didn't see anything newer), and I've tried several other snippets of code found online.
If i relaunch excel it will work for 1-2 queries then throws the error: Run-time error '-2147467259 (80004005)': Unspecified error
It breaks on the line: con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
Here is the code I am running:
Public Function getRS(sSql As String) As Variant
Dim con As ADODB.Connection, rs As ADODB.Recordset
Dim AccessFile As String
Dim Rw As Long, Col As Long, c As Long
Dim MyField, Location As Range
'specify path to db
AccessFile = dbPath 'public variable
'On Error Resume Next
'Create the ADODB connection object.
Set con = CreateObject("ADODB.connection")
'Open the connection.
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile
'Create the ADODB recordset object.
Set rs = CreateObject("ADODB.Recordset")
'Set thee cursor location.
rs.CursorLocation = 3 'adUseClient on early binding
rs.CursorType = 1 'adOpenKeyset on early binding
'On Error Resume Next
'Open the recordset.
rs.Open sSql, con
Set getRSOLD = rs
Set Location = Nothing
Set con = Nothing
Set rs = Nothing
End Function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
