I have an Excel workbook in a synced OneDrive for Business folder that I want to use as a data source for an ADODB connection, which is called from code in the workbook itself.
VBA throws an error when I try to establish the connection.
-2147467259
Microsoft OLE DB Provider for ODBC Drivers
[Microsoft][ODBC Excel Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x17e0 Thread 0x3cd4 DBC 0x920bf1c Excel'.
The error does not happen if the file is in an unsynced folder, e.g. My Documents.
All I need is read-only access to the connection. Can I form the connection string in a different way?
I tried a different connection string, which I tried to make read-only (Mode=Read):
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read; _
Data Source=" & ActiveWorkbook.Path & Application.PathSeparator & _
ActiveWorkbook.Name & "; Extended Properties=""Excel 12.0 Macro;HDR=YES"";"
I get a different error:
-2147467259
Microsoft Access Database Engine
Cannot update. Database or object is read-only.
Sub TestExcelADODB()
Dim cnx As New ADODB.Connection
Set cnx = OpenExcelConnection(ActiveWorkbook.Path, ActiveWorkbook.Name)
cn.Close
End Sub
The error occurs on cn.Open
:
Public Function OpenExcelConnection(Path As String, File As String) _
As ADODB.Connection
Dim cn As New ADODB.Connection
If cn.State = adStateOpen Then cn.Close
cn.ConnectionString = "Driver={Microsoft Excel Driver _
(*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
Path & Application.PathSeparator & File
cn.Open
Set OpenExcelConnection = cn
End Function