title: How do I return a ADODB.Connection from a function in VBA?
body: <p>I have a large Access VBA/SQL Server application. In each function there is the same ADODB connection.</p>
  <p>How do I replace those with a single function that can be called each time, to save space.</p>
  <pre class="lang-vb prettyprint-override"><code>Public Function ConnectionString() As ADODB.Connection
      Dim CN As ADODB.Connection
      
      Set CN = New ADODB.Connection
          
      With CN
          .Provider = &quot;Microsoft.Access.OLEDB.10.0&quot;
          .Properties(&quot;Data Provider&quot;).Value = &quot;SQLOLEDB&quot;
          .Properties(&quot;Data Source&quot;).Value = DLookup(&quot;Source&quot;, &quot;tbl_Connection&quot;)
          .Properties(&quot;Initial Catalog&quot;).Value = DLookup(&quot;Catalog&quot;, &quot;tbl_Connection&quot;)
          .Properties(&quot;Integrated Security&quot;).Value = SSPI
          .Open
      End With
          
      ConnectionString = CN
      
  End Function
  </code></pre>
  <p>It seems like this should return that connection, but there is an error message:</p>
  <blockquote>
  <p>User-Defined Function not found</p>
  </blockquote>
  <p>on the line <code>ConnectionString = CN</code>.</p>