I need to run multiple queries and insert their results into Excel cells. Each query will return only one result (a number, or a null). What is the best way to accomplish this?
Below is an example of the code I'm currently using. It does return data to the cell specified, but I'm guessing there's an easier way to populate multiple cells with the results of multiple queries.
Sub GetData()
Dim qt As QueryTable
sqlstring = "SELECT DISTINCT(r.result_text)" & vbCrLf & _
"FROM dt_sample s" & vbCrLf & _
"LEFT OUTER JOIN dt_field_sample fs ON (s.facility_id = fs.facility_id and s.sample_id = fs.sample_id)" & vbCrLf & _
"INNER JOIN dt_test t ON (s.sample_id = t.sample_id and s.facility_id = t.facility_id)" & vbCrLf & _
"INNER JOIN dt_result r ON (t.test_id = r.test_id and t.facility_id = r.facility_id)" & vbCrLf & _
"INNER JOIN rt_analyte a ON (r.cas_rn = a.cas_rn)" & vbCrLf & _
"INNER JOIN dt_well d ON (s.sys_loc_code = d.sys_loc_code)" & vbCrLf & _
"LEFT OUTER JOIN vw_locatiON l ON s.facility_id = l.facility_id and s.sys_loc_code = l.sys_loc_code" & vbCrLf & _
"LEFT OUTER JOIN dt_task ts ON (s.task_code = ts.task_code and s.facility_id = ts.facility_id)" & vbCrLf & _
"WHERE ((s.sample_date between '09/01/2008' and '12/31/2008') or s.sample_date is null)" & vbCrLf & _
"AND r.result_text is not null" & vbCrLf & _
"AND r.cas_rn = '79-01-6'" & vbCrLf & _
"AND s.sys_loc_code = 'K2'"
connstring = "ODBC;DSN=Fsn-db1\FSN_DB1;UID=;PWD=;Database=4040"
Set qt = ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("H12"), Sql:=sqlstring)
With qt
.FieldNames = False
.AdjustColumnWidth = False
.PreserveFormatting = True
End With
qt.Refresh BackgroundQuery:=True
End Sub
Bookmarks