title: Excel VBA Script for DB Connection
body: <p>I'm new to VBA and need a few pointers on writing a script that opens a connection from an Excel file to a SQL Server database. I'm referencing the VBA language reference at <a href="https://msdn.microsoft.com/en-us/library/office/gg264383.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/office/gg264383.aspx</a> and can't find information on how this is done. The following is what I have managed to scrape from an macro-enabled excel file that I found floating around:</p>
  
  <pre><code>Sub CopyData()
      Dim sSQL As String
      Dim rs As ADODB.Recordset
      Dim cn As ADODB.Connection
      Set cn = New ADODB.Connection
      cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[Server Name];Initial Catalog=[Catalog];"
      End With
  End Sub
  </code></pre>
  
  <p>When I run the script I receive an error 'User-defined type not defined'. And the line <code>Dim rs AS ADODB.Recordset</code> is highlighted. So my understanding here is that ADODB.Recordset has not been defined as a class yet...but I thought this was an built-in class? And assuming this is not a built-in class, I can't find any where in the source code where this class would have been defined. My end goal here is to run a SQL query based on input into the excel file and output the results of the query into adjacent columns. So my questions:</p>
  
  <p>1) Where can I find Microsoft documentations on database connection objects?
  2) Perhaps more of a SQL question, but how should I format the connection string?</p>