title: Using SQL in VBA to Join Data from two Excel Workbooks (Read-Only error)
body: <p>I am seeking to join specific data from two very large excel workbooks, then save this to another workbook. In order to do this, I was using ADODB Objects to use SQL commands, which make performing the join much easier than doing hundreds of thousands of VLookups. My setup prompts the name of a state, and the data source is formatted to depend this state. Let's say you entered "Nevada". It would connect to a file named "Nevada - biological metadata multiple cont" and join this to another file named "Nevada - Site data only multiple cont" and select certain fields from this.</p>
  
  <p>When I am debugging, I can see that the connection is working just fine, and where I change the string variable 'sql' to equal "SELECT * FROM [biologicalresult$]", it will work correctly. The problem seems to be tied to the fact that I am joining it to another workbook through ADODB queries, and I get the error: "Cannot update. Database or object is read-only"</p>
  
  <pre class="lang-vb prettyprint-override"><code>Option Explicit
  
  Sub RunSELECT()
  
      On Error GoTo ErrorHandling
  
      Dim cn As Object, rs As Object, output As String, sql As String, state As String
      state = InputBox("Please insert the state name with the first letter being capital")
  
      Set cn = CreateObject("ADODB.Connection")
  
      '---Connecting to the Data Source---
      With cn
          .Provider = "Microsoft.ACE.OLEDB.12.0"
          .connectionstring = "Data Source=T:\Marketing\Data Analytics\GIS Data\Water Quality Portal Data\State Data\" &amp; state &amp; _
                              " - biological metadata multiple cont;Extended Properties=""Excel 12.0 Xml;HDR=YES;Readonly = false;IMEX = 0"";"
          .Open
      End With
  
      '---Run the SQL SELECT Query---
      sql = "SELECT b.OrganizationIdentifier, b.OrganizationFormalName, b.MonitoringLocationIdentifier, s.LatitudeMeasure, s.LongitudeMeasure, " &amp; _
          "b.ActivityIdentifier , b.ActivityTypeCode, b.ActivityMediaName, b.ActivityMediaSubdivisionName, " &amp; _
          "b.ActivityStartDate, b.ProjectIdentifier, b.CharacteristicName, b.ResultSampleFractionText, b.ResultMeasureValue, b.[ResultMeasure/MeasureUnitCode] " &amp; _
          "FROM [biologicalresult$] b INNER JOIN (SELECT * FROM [Excel 12.0 Xml; IMEX = 0; HDR = Yes; Database = T:\Marketing\Data Analytics\GIS Data\Water Quality Portal Data\" &amp; _
          "State Data\" &amp; state &amp; " - Site data only multiple cont;Readonly = False].[station$]) s " &amp; _
          "ON b.MonitoringLocationIdentifier = s.MonitoringLocationIdentifier;"
  
      Set rs = cn.Execute(sql)
  
      '---Clean up---
      rs.Close
      cn.Close
      Set cn = Nothing
      Set rs = Nothing
  
  Exit Sub
  
  ErrorHandling:
      MsgBox ("Source: " &amp; Err.Source &amp; vbNewLine &amp; "Number: " &amp; Err.Number &amp; vbNewLine &amp; "Description: " &amp; Err.Description &amp; vbNewLine &amp; "Help Context: " &amp; Err.HelpContext)
  done:
      End Sub
  </code></pre>
  
  <p>Thank you for any help!</p>