+ Reply to Thread
Results 1 to 7 of 7

Use column heading and row label as sql query parameters for multiple cells

Hybrid View

TheCyrusVirus Use column heading and row... 05-10-2010, 07:22 AM
TheCyrusVirus Re: Use column heading and... 05-10-2010, 10:57 AM
PingPing Re: Use column heading and... 05-10-2010, 01:23 PM
TheCyrusVirus Re: Use column heading and... 05-11-2010, 02:21 AM
TheCyrusVirus Re: Use column heading and... 05-11-2010, 05:25 AM
PingPing Re: Use column heading and... 05-11-2010, 07:24 AM
TheCyrusVirus Re: Use column heading and... 05-12-2010, 07:12 AM
  1. #1
    Registered User
    Join Date
    04-30-2010
    Location
    Wales, Boyo
    MS-Off Ver
    Excel 2007
    Posts
    42

    Use column heading and row label as sql query parameters for multiple cells

    I currently have a worksheet (attached) where I need to pass some parameters to a SQL query.

    For each cell where I have to retrieve a value I need to pass the date range and row label to a sql query.

    So for example for C3:

    SELECT totalsales From tblSales WHERE SeasonCode = "text value from A3" AND Date BETWEEN "value from C2"
    The result of this query needs to then be inserted into C3

    Ideally I need to then loop through each cell in this row up until the last column of data (in this case column Q) but number of columns will vary.

    I then need to do the same for each row where there is a season code

    I cant for the life of me figure out a neat way of doing this and would really appreciate any help that anyone can offer.
    Attached Files Attached Files
    Last edited by TheCyrusVirus; 05-12-2010 at 07:13 AM.

  2. #2
    Registered User
    Join Date
    04-30-2010
    Location
    Wales, Boyo
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Use column heading and row label as sql query parameters for multiple cells

    Anyone got any ideas on this one, pretty please!

  3. #3
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Use column heading and row label as sql query parameters for multiple cells

    You could use a nested loop, ie. one For...Next loop inside another For...Next loop.
    For example the inner loop would move across the columns running the SQL query for each BETWEEN condition, while the outer loop would move down the rows for each season code.

    To get your upper bounds for each loop, you could use the Count property of the Range object like so:

    For the # of columns: Range("C2", Range("C2").End(xlToRight)).Count
    For the # of rows: Range("B3", Range("B3").End(xlDown)).Count

    For the SQL code you could use the CopyFromRecordset method of the Range object. For example, if your SQL query was returned to a recordset called rstSQL then you would have:

    Range("C3").CopyFromRecordset rstSQL

    Of course you'd replace the "C3" with your loop variables using the Cells object:

    Range(Cells(lngRowCount, lngColCount), Cells(lngRowCount, lngColCount)).CopyFromRecordset rstSQL

    where lngRowCount and lngColCount are the row/column loop values (note that I've used longs instead of integers as Excel 2003+ has rows > 32767.

    The above code is quite simple/rough but it should be enough to get you started.

  4. #4
    Registered User
    Join Date
    04-30-2010
    Location
    Wales, Boyo
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Use column heading and row label as sql query parameters for multiple cells

    Thanks very much PingPing, will have a play around with that today and see how far I get.

    Will post back here if I get stuck again.

  5. #5
    Registered User
    Join Date
    04-30-2010
    Location
    Wales, Boyo
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Use column heading and row label as sql query parameters for multiple cells

    How do I reference the date range found in the relevant column header and the season code (from the start of the row) for the cell that is currently being processed in the loop as I need to pass these variables to the query.

    How do I then insert the value returned by the query into the cell.

    Below is the code I have so far which may be completely wrong.


    Sub MultiSQL()
    
        Dim cnn As ADODB.Connection
        Dim cmd As ADODB.Command
        Dim rstSQL As ADODB.Recordset
        Dim sqlQRY As String
        Dim sqlCN As String
        Dim sCusName As String
        Dim sDateRange As String
        Dim sSCode As String
        Dim i As Long
            
        Dim colCount As Long
        colCount = Range("C3", Range("C3").End(xlToRight)).Count
    
        For i = 1 To colCount
        Set cnn = New ADODB.Connection
            sqlCN = "Provider=SQLOLEDB; Server=BBB-SQL; Database=BBB-LIVE; Trusted_Connection=yes;"
            cnn.Open sqlCN
               sqlQRY = "SELECT SUM of [salesamount] As 'Summed Amount' " _
                & "FROM [Value Entry] " _
                & "WHERE [Customer] = '" & sCusName & "' AND [Date] BETWEEN '" & sDateRng & "' " _
                & "AND [Season Code] = '" & sSCode & "' ;"
                
                Set rstSQL = New ADODB.Recordset
                rstSQL.Open sqlCN, cnn, adOpenDynamic
        Next i
    Last edited by TheCyrusVirus; 05-11-2010 at 06:02 AM.

  6. #6
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Use column heading and row label as sql query parameters for multiple cells

    Quote Originally Posted by TheCyrusVirus View Post
    How do I reference the date range found in the relevant column header and the season code (from the start of the row) for the cell that is currently being processed in the loop
    Use the Value property of the Cells object, eg.

    sDateRng = Cells(2, lngColCount).Value
    sSCode = Cells(lngRowCount,1).Value

    How do I then insert the value returned by the query into the cell
    Use the CopyFromRecordset method of the Range object:

    Range(Cells(lngRowCount, lngColCount), Cells(lngRowCount, lngColCount)).CopyFromRecordset rstSQL
    Last edited by PingPing; 05-11-2010 at 07:31 AM.

  7. #7
    Registered User
    Join Date
    04-30-2010
    Location
    Wales, Boyo
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Use column heading and row label as sql query parameters for multiple cells

    Thanks for all of your help on this pingping, I have made some progress with this but need to change the approach slightly so am going to start a new post.

    Thanks again

+ Reply to Thread

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