Solved

Not returning correct data using sql syntax and vb.net 2005

Posted on 2007-12-05
7
234 Views
Last Modified: 2012-08-13
Hi,

I have a access database that I am talking to using vb.net 2005. The format of the access cell is DD/MM/YYYY. If the date is set as 03/12/2007 in the access cell and I use the following sql statement: -

        rs.Open("Select * from products WHERE Sold = true AND Delivery between #" & DateTimePicker1.Value.ToString("MM/dd/yyyy") & "# AND #" & DateTimePicker2.Value.ToString("MM/dd/yyyy") & "#", conn)

It returns the correct data when I set the between to 02/12/2007 - 04/12/2007

When I use the following sql statement that I need: -

        rs.Open("Select sum(TaxCost + NetDelCost) as DeliveryNetttotal , Sum(NetDelCost) as DeliveryGrossTotal from products WHERE Sold = true AND Delivery between #" & DateTimePicker1.Value.ToString("MM/dd/yyyy") & "# AND #" & DateTimePicker2.Value.ToString("MM/dd/yyyy") & "#", conn)

It does not show any data when the same between to 02/12/2007 - 04/12/2007 is used.

If I use this statement again and set the between to 02/12/2007 - 12/12/2007 it returns the correct data.

What am I doing wrong please.

Best Regards
Lee
0
Comment
Question by:ljhodgett
7 Comments
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
Could TaxCost or NetDelCost be Null?
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
You should use "yyyy-MM-dd" for sql queries
0
 

Author Comment

by:ljhodgett
Comment Utility
Hi,

There both set to numbers in access so no nulls are present. Also I have tried yyyy-MM-dd but it still does not work.

Best Regards
Lee
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

by:MikeToole
Comment Utility
What is rs dimmed as? Is this classic ADO rather than ADO.net?
0
 

Author Comment

by:ljhodgett
Comment Utility
Hi,

I've have attached the full code below. I am using clissic ado at the momment for this program.

Best Regards
Lee
Public Class frmDeliveryReport
 

    Dim rs As ADODB.Recordset

    Dim Statement As String

    Dim rpt As New crItemsSearchResults
 
 

    Private Sub Refresh_Details()
 

        Dim SumDeliveryNett As Double

        Dim SumDeliveryGross As Double
 

        SumDeliveryNett = 0

        SumDeliveryGross = 0
 

        ' MsgBox("Select sum(TaxCost + NetDelCost) as DeliveryNetttotal , Sum(NetDelCost) as DeliveryGrossTotal from products WHERE Sold = true AND Delivery between #" & DateTimePicker1.Value.ToString("MM/dd/yyyy HH:mm:ss") & "# AND #" & DateTimePicker2.Value.ToString("MM/dd/yyyy") & "#")
 

        Connect()

        rs = New ADODB.Recordset

        rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient

        rs.CursorType = ADODB.CursorTypeEnum.adOpenKeyset

        rs.Open("Select sum(TaxCost + NetDelCost) as DeliveryNetttotal , Sum(NetDelCost) as DeliveryGrossTotal from products WHERE Sold = true AND Delivery between #" & DateTimePicker1.Value.ToString("yyyy-MM-dd") & "# AND #" & DateTimePicker2.Value.ToString("yyyy-MM-dd") & "#", conn)

        'rs.Open("Select * from products WHERE Sold = true AND Delivery between #" & DateTimePicker1.Value.ToString("MM/dd/yyyy") & "# AND #" & DateTimePicker2.Value.ToString("MM/dd/yyyy") & "#", conn)
 

        MsgBox(rs.RecordCount)
 
 

        If Not rs.Fields("DeliveryNetttotal").Value Is DBNull.Value Then SumDeliveryNett = rs.Fields("DeliveryNetttotal").Value

        If Not rs.Fields("DeliveryGrossTotal").Value Is DBNull.Value Then SumDeliveryGross = rs.Fields("DeliveryGrossTotal").Value
 

        
 

        rs.Close()

        'conn.Close()
 

        rs = New ADODB.Recordset

        rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient

        rs.CursorType = ADODB.CursorTypeEnum.adOpenKeyset

        rs.Open("Select TaxCost, NetDelCost from products WHERE Sold = true AND Delivery between #" & DateTimePicker1.Text & "# AND #" & DateTimePicker2.Text & "#", conn)
 

        DataSet11.Tables("Delivery_Cost").Clear()
 

        If Not rs.EOF Then rs.MoveFirst()
 

        Do While Not rs.EOF
 

            Dim dr As DataRow = DataSet11.Tables("Delivery_Cost").NewRow()

            dr("DeliveryNett") = "Nett: £" & FormatNumber(rs.Fields("NetDelCost").Value, 2) & "  Gross: £" & FormatNumber(rs.Fields("TaxCost").Value, 2)

            dr("DeliveryGross") = rs.Fields("TaxCost").Value

            dr("DeliveryNettTotal") = SumDeliveryNett

            dr("DeliveryGrossTotal") = SumDeliveryGross
 

            DataSet11.Tables("Delivery_Cost").Rows.Add(dr)

            rs.MoveNext()

        Loop
 

        lstDelivery.ValueMember = "DeliveryNett"

        lstDelivery.DisplayMember = "DeliveryNett"

        lstDelivery.DataSource = DataSet11.Tables("Delivery_Cost")
 

        lblGroosCost.Text = "£" & FormatNumber(SumDeliveryGross, 2)

        lblNettCost.Text = "£" & FormatNumber(SumDeliveryNett, 2)
 
 

        rs.Close()

        conn.Close()

    End Sub
 

    Private Sub frmDeliveryReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Refresh_Details()

    End Sub
 

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

        Refresh_Details()

    End Sub
 

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged

        Refresh_Details()

    End Sub
 

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click

        Me.Close()

    End Sub

End Class

Open in new window

0
 
LVL 27

Expert Comment

by:MikeToole
Comment Utility
Two suggestions:

1) Add a count to each of the two versions of your sql to confirm that we are in fact dealing with the same number of records:
("Select Count(1) as RecordCount, sum(TaxCost + NetDelCost) as DeliveryNetttotal , Sum(...

2) Convert the dates to a Double instead of to a string that needs to be wrapped with #'s
... WHERE Sold = true AND Delivery between " & DateTimePicker1.Value.ToOADate & " AND " & DateTimePicker2.Value.ToOADate, conn)
 
Access stores date-time values as a Double representing the number of days since Dec 31st 1899, anything else that you see is formatting. If you convert to double you can be sure that you're not hitting any international date format problems (and the SQL string construction is nowhere near as messy)
The ToOADate method converts a .Net datevalue to an OLE Automation Date.
0
 
LVL 27

Expert Comment

by:MikeToole
Comment Utility
Correction, COM dates, which is what Access stores, are offset from the beginning of Dec 30th 1899, not Dec 31st.
Mike
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
STDEVP in SQL 2 32
PL/SQL - Leading zeros 7 39
Hide Tab Page 3 18
How to simplify my SQL statement? 9 10
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now