+ Reply to Thread
Results 1 to 12 of 12

Excell cell in sql where clause

Hybrid View

  1. #1
    Registered User
    Join Date
    05-25-2011
    Location
    Ireland
    MS-Off Ver
    Excel 2007
    Posts
    7

    Question Excell cell in sql where clause

    hi guys

    complete newbie to this - looking to create a pivot table from an mysql db hosted remotely, i have everything working at the moment but i would like to expand on it so that a user can enter a date on the excel sheet and click refresh and the query will use the date range from that cell

    eg select * from table1 where date = C1

    this is very sloppy but i've got this far
    Sub SetStartDate()
    Dim rStartDate As Range
        Set rStartDate = Range("D1")
    End Sub
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+M
    '
        
        With ActiveWorkbook.Connections("sales_weekly").ODBCConnection
            .BackgroundQuery = True
            .CommandText = Array( _
            "SELECT * FROM `db`.`table1` WHERE `Month` = ") rStartDate
            .CommandType = xlCmdSql
            .Connection = "ODBC;DSN=Database;"
            .RefreshOnFileOpen = False
            .SavePassword = False
            .SourceConnectionFile = ""
            .ServerCredentialsMethod = xlCredentialsMethodIntegrated
            .AlwaysUseConnectionFile = False
        End With
        With ActiveWorkbook.Connections("table1")
            .Name = "table1"
            .Description = "Last Week"
        End With
    End Sub

    any help with this would be greatly appreciated!!! thanks
    Last edited by romperstomper; 05-26-2011 at 07:54 AM. Reason: tags

  2. #2
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Birmingham, England
    MS-Off Ver
    12.0
    Posts
    808

    Re: Excell cell in sql where clause

    Firstly You need to wrap your code!

    Secondly looks like your almost there buddy:

    Sub Macro1()
    
    Dim MyDate as Date
    
    MyDate = Range("C1").value
    
    With ActiveWorkbook.Connections("sales_weekly").ODBCConnection
      .BackgroundQuery = True
      .CommandText = Array( _
      "SELECT * FROM `db`.`table1` WHERE `Month` = " & MyDate)
      .CommandType = xlCmdSql
      .Connection = "ODBC;DSN=Database;"
      .RefreshOnFileOpen = False
      .SavePassword = False
      .SourceConnectionFile = ""
      .ServerCredentialsMethod = xlCredentialsMethodIntegrated
     .AlwaysUseConnectionFile = False
    End With
    
    With ActiveWorkbook.Connections("table1")
     .Name = "table1"
     .Description = "Last Week"
    End With
    
    End Sub
    However i Notice that the field you are apply the where clause to is called "Month" that what format is that in? e.g "February" or "Feb" or "02" or is it a full date? you'll need to format the value from cell C1 to match the field format and if it contaings letters you'll also have to wrap it in apostraphes (however its spelt?)

    E.g.

     "SELECT * FROM `db`.`table1` WHERE `Month` = " & Format(MyDate,"DD-MMM-YY"))
    Hope this helps?
    Mr MaGoo
    Magoo.Inc MMVII

    If i've helped please add to my Rep by Clicking on the Blue Scales in the top right hand corner of the post

  3. #3
    Registered User
    Join Date
    05-25-2011
    Location
    Ireland
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Excell cell in sql where clause

    thanks for your reply, i did everything as suggested but it keeps coming up with a runtime error 1004?

    any idea what the problem is?

  4. #4
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Birmingham, England
    MS-Off Ver
    12.0
    Posts
    808

    Re: Excell cell in sql where clause

    firstly what format is the date field in the sql table?

  5. #5
    Registered User
    Join Date
    05-25-2011
    Location
    Ireland
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Excell cell in sql where clause

    i've changed the month field to date field to test the formatting command

    this is what i have now

    Sub Macro1()
    
    Dim MyDate As Date
    
    MyDate = Worksheets("Sheet1").Range("D2").Value
    MsgBox Format(MyDate, "YYYY-MM-DD")
    
    With ActiveWorkbook.Connections("sales").ODBCConnection
      .BackgroundQuery = True
      .CommandText = Array( _
      "SELECT * FROM `table1`.`pivot-1month` WHERE `Date` = " & MyDate)
      .CommandType = xlCmdSql
      .Connection = "ODBC;DSN=Database;"
      .RefreshOnFileOpen = False
      .SavePassword = False
      .SourceConnectionFile = ""
      .ServerCredentialsMethod = xlCredentialsMethodIntegrated
     .AlwaysUseConnectionFile = False
    End With
    
    With ActiveWorkbook.Connections("sales")
     .Name = "salesdata"
     .Description = "Last Week"
    End With
    
    End Sub
    i added a message box to test the date output and that is ok, and i also specified the sheet for the date input - but the problem is at the commandtext section for some reason?

  6. #6
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Birmingham, England
    MS-Off Ver
    12.0
    Posts
    808

    Re: Excell cell in sql where clause

    Try This

    Sub Macro1()
    
    Dim MyDate As Date
    
    MyDate = Worksheets("Sheet1").Range("D2").Value
    MsgBox Format(MyDate, "YYYY-MM-DD")
    
    With ActiveWorkbook.Connections("sales").ODBCConnection
      .BackgroundQuery = True
      .CommandText = Array( _
      "SELECT * FROM `table1`.`pivot-1month` WHERE `Date` = " & Format(MyDate, "YYYY-MM-DD"))
      .CommandType = xlCmdSql
      .Connection = "ODBC;DSN=Database;"
      .RefreshOnFileOpen = False
      .SavePassword = False
      .SourceConnectionFile = ""
      .ServerCredentialsMethod = xlCredentialsMethodIntegrated
     .AlwaysUseConnectionFile = False
    End With
    
    With ActiveWorkbook.Connections("sales")
     .Name = "salesdata"
     .Description = "Last Week"
    End With
    
    End Sub
    and if that doesn't wor remove the "Array" crap and its brackets then try that

  7. #7
    Registered User
    Join Date
    05-25-2011
    Location
    Ireland
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Excell cell in sql where clause

    still no luck runtime 1004 highlighting that commandtext line, do i possibly need more info on the odbc connection somewhere - not sure what the issue is at all

+ 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