Results 1 to 3 of 3
  1. #1
    John Southern is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    31

    Formatting dates

    I have the following code. It gets two dates using input boxes, calculates the corresponding dates for the previous year and then uses these dates as parameters in a series of Queries.

    Code:
     
    Sub Comparisons()
    Dim FROM1 As Date
    Dim FROM2 As Date
    Dim TO1 As Date
    Dim TO2 As Date
    Dim strSQL As String
    Dim strSQL2 As String
    FROM1 = Format(InputBox("Enter Date from", "Input"), "mm/dd/yy")
     
    TO1 = Format(InputBox("Enter Date to", "Input"), "mm/dd/yy")
    FROM2 = DateAdd("yyyy", -1, CDate(FROM1))
    TO2 = DateAdd("yyyy", -1, CDate(TO1))
    Call addBudget(FROM1)
    strSQL = "SELECT SalesComp.Expr4, SalesComp.WT_NOMCC, Sum(SalesComp.WT_NET_TOTAL) AS SumOfWT_NET_TOTAL, First(SalesComp.Expr1) As FirstOfExpr1 INTO SumComp FROM SalesComp WHERE (((SalesComp.WM_INVOICE_DATE) > # " & FROM1 & " # AND(SalesComp.WM_INVOICE_DATE) < # " & TO1 & "#) OR (( SalesComp.WM_INVOICE_DATE) > #" & FROM2 & " # AND (SalesComp.WM_INVOICE_DATE) < # " & TO2 & " #)) GROUP BY SalesComp.Expr4, SalesComp.WT_NOMCC;"
    CurrentDb.QueryDefs("SumSalesComp").SQL = strSQL
    DoCmd.OpenQuery "SUMSalesComp"
    strSQL2 = "SELECT EQryCredits.NOMCC, EQryCredits.Expr4, Sum(EQrycredits.Amount) as SumOfAmount, [Expr4] & [NOMCC] AS Expr1,First(EQryCredits.Expr3) as FirstOfExpr3 INTO SelCredits FROM EQrycredits WHERE(((EQryCredits.Date)># " & FROM1 & " # AND (EQryCredits.Date) < #" & TO1 & "#) OR (( EQryCredits.Date) >#" & FROM2 & " # AND (EQryCredits.Date )< # " & TO2 & " #)) GROUP BY EQryCredits.Expr4,EQryCredits.NOMCC;"
    CurrentDb.QueryDefs("FQrySumCredits").SQL = strSQL2
    DoCmd.OpenQuery "FQrySumCredits"
    End Sub
    The dates are entered in the English way dd-mm-yyyy and should be reformatted into mm-dd-yyyy for MS Query to work

    My problem is that the dates FROM1 and FROM2 appear to be formated differently from the dates TO1 and TO2, and this means that the queries give anomalous results.
    As far as I can see both dates are treated in the same way it seems very strange that they should appear differently in the SQL statements of the queries.
    Any help would be much appreciated.


    John

  2. #2
    llkhoutx is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Location
    Houston, Texas USA
    Posts
    373
    A date is a date is a date. Formating a date is to display a date in a specific format.

    It's possible that your date in you table is actually a string, not a date. Check that out.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Actually dates in VBA/SQL need to be in US format. I use a variation of this:

    http://www.mvps.org/access/datetime/date0005.htm

    More info and an alternate method here:

    http://support.microsoft.com/default...b;en-us;210069
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Conditional Formatting
    By cevatyildiz in forum Forms
    Replies: 8
    Last Post: 04-30-2010, 12:01 PM
  2. Conditional formatting
    By ylivne in forum Reports
    Replies: 1
    Last Post: 07-12-2009, 06:18 PM
  3. Lost report formatting
    By kiltedcueball in forum Database Design
    Replies: 2
    Last Post: 06-23-2009, 12:28 AM
  4. Export Formatting
    By arthura in forum Import/Export Data
    Replies: 0
    Last Post: 02-20-2009, 08:44 AM
  5. Need Help Formatting a field.
    By marshallgrads in forum Access
    Replies: 4
    Last Post: 12-06-2007, 03:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums