+ 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

    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?

  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 what format is the date field in the sql table?
    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

    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?

  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

    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

  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

    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

  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

    Its hard to help when idont have the connection lol

    once last try:

    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

  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

    i added this line to output to a message box to see what the query looks like and it looks fine

    Sub Macro1()
    
    Dim MyDate As Date
    
    MyDate = Worksheets("Sheet1").Range("D2").Value
    MyDate2 = Format(MyDate, "YYYY-MM-DD")
    brackets = "'"
    Test1 = "SELECT * FROM pivot1month WHERE Date = '" & MyDate2 & "'"
    MsgBox Test1
    
    
    With ActiveWorkbook.Connections("sales").ODBCConnection
      .BackgroundQuery = True
      .CommandText = "SELECT * FROM pivot1month 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 = "sales"
     .Description = "Last Week"
    End With
    
    End Sub
    really stumped! i appreciate you taking the time to help thought this would be straight forward :s
    Last edited by cacoyle45; 05-26-2011 at 11:12 AM. Reason: addition

  8. #8
    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

    what about:

    Sub Macro1()
    
    Dim MyDate As Date
    
    MyDate = Worksheets("Sheet1").Range("D2").Value
    MyDate2 = Format(MyDate, "YYYY-MM-DD")
    brackets = "'"
    Test1 = "SELECT * FROM pivot1month WHERE Date = '" & MyDate2 & "'"
    MsgBox Test1
    
    
    With ActiveWorkbook.Connections("sales").ODBCConnection
      .BackgroundQuery = True
      .CommandText = Test1
      .CommandType = xlCmdSql
      .Connection = "ODBC;DSN=Database;"
      .RefreshOnFileOpen = False
      .SavePassword = False
      .SourceConnectionFile = ""
      .ServerCredentialsMethod = xlCredentialsMethodIntegrated
     .AlwaysUseConnectionFile = False
    End With
    
    With ActiveWorkbook.Connections("sales")
     .Name = "sales"
     .Description = "Last Week"
    End With
    
    End Sub

+ 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