Problem with Date Query

Posted on 2005-04-15
Last Modified: 2010-05-02
Hi Guys,
I've just been on getting this sorted a couple of minutes ago!!
Anyway i thought it worked but the following will return results regardless of the month and year, in other words it will return records whithin a the days only,  even if they have a different month or year. Help!!!


txtSQL = "SELECT [Date of PaymentIn] AS [Transaction Date], [Account Name] AS [Account], PaymentNumberIn AS [Payment Number], PaymentDescription AS [Payment Description], [Net Amount] AS [Credit], 0 AS [Debit] From PaymentsIn WHERE [Date of PaymentIn] BETWEEN #" & Format(DateFrom, "dd-mm-yyyy") & "# AND #" & _
                      Format(DateTo, "dd-mm-yyyy") & "# Union SELECT [Date of PaymentOut] AS [Transaction Date], [Account Name] AS [Account], PaymentNumberOut AS [Payment Number], PaymentDescription AS [Payment Description], 0 AS [Net Amount], [Net Amount] AS [Debit] From PaymentsOut WHERE [Date of PaymentOut] BETWEEN #" & Format(DateFrom, "dd-mm-yyyy") & "# AND #" & _
                      Format(DateTo, "dd-mm-yyyy") & "# Union SELECT [Invoice Date] AS [Transaction Date], [Invoice To Name] AS [Account], [Invoice Number] AS [Payment Number], 0 AS [Payment Description], [Net Amount] AS [Credit], 0 AS [Debit] From Invoices WHERE [Invoice Date] BETWEEN #" & Format(DateFrom, "dd-mm-yyyy") & "# AND #" & _
                      Format(DateTo, "dd-mm-yyyy") & "# Union SELECT [Purchase Invoice Date] AS [Transaction Date], [CreditorName] AS [Account], [Purchase Invoice Number] AS [Payment Number], 0 AS [Purchase Payment Description], 0 AS [Credit], [Purchase Invoice Net Amount] AS [Debit] From PurchaseInvoice WHERE [Purchase Invoice Date] BETWEEN #" & Format(DateFrom, "dd-mm-yyyy") & "# AND #" & _
                      Format(DateTo, "dd-mm-yyyy") & "#"

Question by:davemckenna
    LVL 69

    Expert Comment

    by:Éric Moreau
    When sending dates in a SQL query, it often helps to send in this format:

    Format(DateTo, "yyyy-mm-dd")

    Otherwise, confusion may exists because dates are interpreted as mm/dd/yy first if possible.

    Author Comment

    I have changed the order in which they are sent but the same result, it will filter between the day (number) but not the month or year.

    LVL 69

    Accepted Solution

    Your database is Access ?

    Can you paste the content of txtSQL here ?

    Also remove all unions while debugging. If we get the first query running, we will add the unions after that.

    Author Comment

    Thanks for your help, silly me i had my date fileds in 2 tables formatted as text!!! I did it as an attempt to debug earlier on, problem seems to be solved now.


    Featured Post

    Highfive Gives IT Their Time Back

    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!

    Join & Write a Comment

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    734 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

    16 Experts available now in Live!

    Get 1:1 Help Now