Results 1 to 6 of 6
  1. #1
    Mary Fall is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2011
    Posts
    3

    Question Date Comparison swapping month and day in VB

    I wonder if someone can help. I have two unbound date columns Date From and Date To. A macro on the form works perfectly!!!! the ‘where’ statement shows:
    [PurchaseDate]>=[Forms]![GeneralDateEntry]![DateFrom] And [PurchaseDate]<=[Forms]![GeneralDateEntry]![DateTo]

    However, due to the amount of where statements – i am trying to amalgamate them into a VB string. All strings work - but not the dates....

    ---
    If Not IsNull(Me.DateFrom) And Me.PurchasedTick = -1 Then
    strWhere = strWhere & " AND [PurchaseDate] >= " & [Forms]![GeneralDateEntry]![DateFrom]
    End If
    If Not IsNull(Me.DateTo) And Me.PurchasedTick = -1 Then
    strWhere = strWhere & " AND [PurchaseDate] <= " & [Forms]![GeneralDateEntry]![DateTo]
    End If

    DoCmd.OpenReport stDocument, acPreview, , strWhere
    ---
    I put in dates from 01/10/2010 to 05/10/2010



    It outputs everything from 10th Jan – 10th May! Swapping the month and day around.

    I output a contents of strWhere just before calling the report – and it shows PurchaseDate>=#01/10/2010# And PurchaseDate<=#10/01/2010#

    The dates are text fields formatted showing yearmonthday. In query :

    PurchaseDate: CDate(DateSerial(Left([vehicles]![PurchaseDate],4),Mid([vehicles]![PurchaseDate],5,2),Right([vehicles]![PurchaseDate],2)))

    I’m Confused ...especially as it works on the macro....any help very welcome.

    Thanks
    Mary

  2. #2
    NTC is offline VIP
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    2,392
    not sure I follow as to the issue. Just the display of the date?

    I pull out this statement: " The dates are text fields formatted showing yearmonthday." what is the syntax entered into the format field of the textbox property?

    because really behind the scenes all dates are sequential integers handled by the OS - everything else is simply display. So display formats generally this are handled at the display point i.e. form's field property. (I myself generally avoid special date displays and just go with the formats embedded into Access....)

    Maybe this helps.

  3. #3
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    If I remember correctly, SQL always interprets dates as mm/dd/yyyy. I have been looking at a couple of other forums and found that people use the format like this to force SQL to evaluate the dates differently:

    Format(yourdate, "\#yyyy\-mm\-dd\#")

  4. #4
    Mary Fall is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2011
    Posts
    3

    Smile

    thanks guys. The format in the unbound date fields is 'Short Date' showing 19/06/2011 when i select it. All normal. In the report I use based on the query - it doesn't have Short Date option in the pull-down...may have something to do with it.... Will play around with the Format option.

  5. #5
    Mary Fall is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2011
    Posts
    3

    Thumbs up Sorted it!

    Sorted it!

    In VB rather than having :
    strWhere = strWhere & " AND [RegDate] <= #" & Me.DateTo & "#"

    put the whole lot in quotes...
    strWhere = strWhere & " AND [RegDate] <= [Forms]![GeneralDateEntry]![DateTo]"

    Worked a treat

  6. #6
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I'm glad you got it worked out.

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

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2011, 02:43 PM
  2. Short date comparison
    By andy101 in forum Programming
    Replies: 2
    Last Post: 03-17-2011, 04:36 AM
  3. Month To Date Query
    By jmorse in forum Queries
    Replies: 3
    Last Post: 03-11-2011, 11:25 AM
  4. Track capacity changes month by month
    By Nils in forum Database Design
    Replies: 1
    Last Post: 02-18-2011, 03:03 PM
  5. Replies: 4
    Last Post: 10-27-2010, 02:57 PM

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