+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    895

    Problem with Aggregate Function ADO SQL

    Hi,

    I have a spreadsheet that I populate from an Access database which I have no problem doing, except for the below query.

    The SQL query works fine in Access, but returns no records when running the below code from Excel and there are no error messages. This is the first Aggregate function that I have run from Excel so it's quite possible I'm missing something, but any help would be very much appreciated!

    Code:
    Option Explicit
    
    Public Sub ImportSD()
    
    
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sql As String
    Dim filenm As String
    Dim scrh As String
    Dim x As Double, y As Double
    Dim usernm As String, pword As String
    Dim lSQL As String
    
    filenm = "\\Sheffield-1\data\bus\339-BusContactCentreSales\Private\WIP Reports\Payplan.mdb"
    
    Set conn = New ADODB.Connection
    conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & filenm & ";", _
                  usernm, pword
                  
    lSQL = "SELECT dateadd('m', datediff('m', 0,[entry Date]),0) As [Set_Month]," & _
           " SUM([Points]) AS [Run_Rate] " & _
           "FROM Combined WHERE [Product Name] LIKE '*BROADBAND*' " & _
           "GROUP BY dateadd('m', datediff('m', 0,[entry Date]),0);"
           
    
    Set rs = New ADODB.Recordset
    rs.Open lSQL, conn, adOpenStatic
    
    Sheets("teams").Range("A38").CopyFromRecordset rs
    
    
    rs.close
    Set rs = Nothing
    conn.Close
    Set conn = Nothing
    
    End Sub
    Last edited by Kyle123; 03-10-2010 at 07:49 AM.

  2. #2
    Forum Guru Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Problem with Aggregate Function ADO SQL

    Hi

    I believe it's because in this ADO provider you need to use the % character to denote a wildcard so the SQL should look like this:

    Code:
    lSQL = "SELECT dateadd('m', datediff('m', 0,[entry Date]),0) As [Set_Month]," & _
           " SUM([Points]) AS [Run_Rate] " & _
           "FROM Combined WHERE [Product Name] LIKE '%BROADBAND%' " & _
           "GROUP BY dateadd('m', datediff('m', 0,[entry Date]),0);"
    Richard
    Richard Schollar
    Microsoft MVP - Excel

  3. #3
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    895

    Re: Problem with Aggregate Function ADO SQL

    That's great, works fine now.

    Thanks Richard

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0