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.
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"
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\" & state & _
" - 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, " & _
"b.ActivityIdentifier , b.ActivityTypeCode, b.ActivityMediaName, b.ActivityMediaSubdivisionName, " & _
"b.ActivityStartDate, b.ProjectIdentifier, b.CharacteristicName, b.ResultSampleFractionText, b.ResultMeasureValue, b.[ResultMeasure/MeasureUnitCode] " & _
"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\" & _
"State Data\" & state & " - Site data only multiple cont;Readonly = False].[station$]) s " & _
"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: " & Err.Source & vbNewLine & "Number: " & Err.Number & vbNewLine & "Description: " & Err.Description & vbNewLine & "Help Context: " & Err.HelpContext)
done:
End Sub
Thank you for any help!