Results 1 to 6 of 6

Thread: excel vba sql query help

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    584

    excel vba sql query help

    hi ,how to change the sql query date between as below startdate and enddate string??i want to use date between query from excel vba to retrieve data from sql server.please help.thanks.

    Code:
    Dim enddate As String
    Dim startdate As String
    Dim partno As String
    
    partno = ComboBox1.Text
    startdate = Format(Dtpdata.DTPicker1.Value, "dd MMM yyyy")
    enddate = Format(Dtpdata.DTPicker1.Value, "dd MMM yyyy")
    
    “SELECT SpcData.PartNo, SpcData.Dimension, SpcData.MachineNo, SpcData.Date FROM SPC.dbo.SpcData SpcData WHERE (SpcData.Date Between {ts '2008-03-24 00:00:00'} And {ts '2008-03-27 00:00:00'}) AND (SpcData.PartNo='" & partno & "') GROUP BY SpcData.PartNo, SpcData.Dimension, SpcData.MachineNo, SpcData.Date

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: excel vba sql query help

    Are the dates in question in a specific cell on a specific worksheet?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    584

    Re: excel vba sql query help

    Hack:
    hi,thanks reply.The date is from the sql database table field.
    for example:i want to retrieve the data from 23-03-2008 to 28-03-08.so i use 2 dtpicker to retrieve the data from the sql server to the excel worksheet.
    i just want to know how to change the sql query of date use the dtpicker select date.
    (SpcData.Date Between {ts '2008-03-24 00:00:00'} And {ts '2008-03-27
    00:00:00'})


    please help.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: excel vba sql query help

    Everything you need to know (and based on your previous threads, should know already) is in two articles from our Database Development FAQs/Tutorials (at the top of this forum):

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    584

    Re: excel vba sql query help

    hi,thank ya reply.the previous problem has been solved.but then i face another problem is using "where statement as below statement cannot to sql the query have error message "type mismatch'

    this is the current code:
    Code:
    "SELECT SpcData.PartNo, SpcData.Dimension, SpcData.MachineNo, Sum(SpcData.SampleNo/5) AS 'Sum'" & Chr(13) & "" & Chr(10) & "FROM SPC.dbo.SpcData SpcData WHERE (SpcData.PartNo='" & partno & "')and (SpcData.Date BETWEEN '" & startdate & "' and '" & enddate & "')GROUP BY SpcData.PartNo, SpcData.Dimension, SpcData.MachineNo " _
    this is the previous work able statement is without date between query
    Code:
    "SELECT SpcData.PartNo, SpcData.Dimension, SpcData.MachineNo, Sum(SpcData.SampleNo/5) AS 'Sum'" & Chr(13) & "" & Chr(10) & "FROM SPC.dbo.SpcData SpcData WHERE (SpcData.PartNo='" & partno & "')GROUP BY SpcData.PartNo, SpcData.Dimension, SpcData.MachineNo " _
    please help.thanks.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: excel vba sql query help

    That should be fine for SQL Server, as long as startdate and enddate are formatted appropriately (what you showed in your first post will fail if the client OS language is not English).

Posting Permissions

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



Click Here to Expand Forum to Full Width