Results 1 to 29 of 29

Populating Cells With SQL Server Data

Threaded View

Salty Sea Dog Populating Cells With SQL... 07-29-2009, 04:47 PM
Salty Sea Dog Re: Populating Cells With SQL... 07-30-2009, 10:54 AM
romperstomper Re: Populating Cells With SQL... 07-30-2009, 11:09 AM
Salty Sea Dog Re: Populating Cells With SQL... 07-30-2009, 11:53 AM
Salty Sea Dog Assign Value to Variable 07-30-2009, 11:54 AM
DonkeyOte Re: Assign Value to Variable 07-30-2009, 01:10 PM
Salty Sea Dog Re: Assign Value to Variable 07-30-2009, 01:16 PM
DonkeyOte Re: Assign Value to Variable 07-30-2009, 01:20 PM
romperstomper Re: Populating Cells With SQL... 07-30-2009, 12:11 PM
Salty Sea Dog Re: Populating Cells With SQL... 07-30-2009, 12:18 PM
Salty Sea Dog Re: Populating Cells With SQL... 08-04-2009, 10:42 AM
Salty Sea Dog Re: Populating Cells With SQL... 08-04-2009, 11:12 AM
Salty Sea Dog Re: Populating Cells With SQL... 08-04-2009, 11:14 AM
romperstomper Re: Populating Cells With SQL... 08-04-2009, 11:15 AM
Salty Sea Dog Re: Populating Cells With SQL... 08-04-2009, 11:24 AM
Salty Sea Dog Re: Populating Cells With SQL... 08-04-2009, 11:28 AM
romperstomper Re: Populating Cells With SQL... 08-04-2009, 11:32 AM
Salty Sea Dog Re: Populating Cells With SQL... 08-04-2009, 12:53 PM
romperstomper Re: Populating Cells With SQL... 08-04-2009, 02:11 PM
gwunta Re: Populating Cells With SQL... 06-27-2012, 12:28 AM
JapanDave Re: Populating Cells With SQL... 06-27-2012, 03:16 AM
romperstomper Re: Populating Cells With SQL... 06-27-2012, 03:07 AM
  1. #1
    Registered User
    Join Date
    07-29-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    31

    Populating Cells With SQL Server Data

    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
    Last edited by Salty Sea Dog; 07-29-2009 at 04:49 PM. Reason: Added Code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1