title: ODBC lock error using Excel workbook in synced OneDrive folder as ADODB connection
body: <p>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.</p>
  <p>VBA throws an error when I try to establish the connection.</p>
  <blockquote>
  <p>-2147467259</p>
  <p>Microsoft OLE DB Provider for ODBC Drivers</p>
  <p>[Microsoft][ODBC Excel Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x17e0 Thread 0x3cd4 DBC 0x920bf1c                                                            Excel'.</p>
  </blockquote>
  <p>The error does not happen if the file is in an unsynced folder, e.g. My Documents.</p>
  <p>All I need is read-only access to the connection. Can I form the connection string in a different way?</p>
  <p>I tried a different connection string, which I tried to make read-only (Mode=Read):</p>
  <pre><code>cn.ConnectionString = &quot;Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read; _
  Data Source=&quot; &amp; ActiveWorkbook.Path &amp; Application.PathSeparator &amp; _
  ActiveWorkbook.Name &amp; &quot;; Extended Properties=&quot;&quot;Excel 12.0 Macro;HDR=YES&quot;&quot;;&quot;
  </code></pre>
  <p>I get a different error:</p>
  <blockquote>
  <p>-2147467259</p>
  <p>Microsoft Access Database Engine</p>
  <p>Cannot update. Database or object is read-only.</p>
  </blockquote>
  <pre class="lang-vb prettyprint-override"><code>Sub TestExcelADODB()
      Dim cnx As New ADODB.Connection
      Set cnx = OpenExcelConnection(ActiveWorkbook.Path, ActiveWorkbook.Name)
      cn.Close
  End Sub
  </code></pre>
  <p>The error occurs on <code>cn.Open</code>:</p>
  <pre class="lang-vb prettyprint-override"><code>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 = &quot;Driver={Microsoft Excel Driver _
        (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=&quot; &amp; _
        Path &amp; Application.PathSeparator &amp; File
      cn.Open
      Set OpenExcelConnection = cn
  End Function
  </code></pre>