Results 1 to 5 of 5
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts
    On the startup form of the attached mdb, the following code line associated with the button labelled "Report Preview":
    If Format(Forms!GioRep!CGior, "dd-mm-yyyy") < Format(#1/12/2007#, "dd-mm-yyyy") Then
    doesn't work correctly for certain dates; for example, if you select July 9, 2009, the condition evaluates to True.
    How can I modify the line so as to work properly?
    Attached Files Attached Files

  2. #2
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts
    [quote name='grovelli' post='797062' date='09-Oct-2009 19:28']On the startup form of the attached mdb, the following code line associated with the button labelled "Report Preview":
    If Format(Forms!GioRep!CGior, "dd-mm-yyyy") < Format(#1/12/2007#, "dd-mm-yyyy") Then
    doesn't work correctly for certain dates; for example, if you select July 9, 2009, the condition evaluates to True.
    How can I modify the line so as to work properly?[/quote]
    This converts dates to a text string, why dont you convert them to real dates then do the comparison.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    1. Literal dates in VBA use US date format m/d/yyyy, so #1/12/2007# is interpreted as January 12, 2007.

    2. The result of Format is a string, so you are performing a string comparison, not a date comparison:

    If "09-07-2009" < "12-01-2007" Then

    Since 0 comes before 1, the condition is True.

    There is no need to use Format. VBA is very well able to compare dates.

    If your intention was to test whether CGior is before January 12, 2007, use

    If Me!CGior < #1/12/2007# Then

    and if you wanted to test whether it is before December 1, 2007, use

    If Me!CGior < #12/1/2007# Then

    I used Me instead of Forms!GioRep - if you ever decide to rename the form, Me will still work.

  4. #4
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='patt' post='797066' date='09-Oct-2009 09:44']This converts dates to a text string, why dont you convert them to real dates then do the comparison.[/quote]
    Thank you Pat :-)

  5. #5
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='797067' date='09-Oct-2009 09:51']1. Literal dates in VBA use US date format m/d/yyyy, so #1/12/2007# is interpreted as January 12, 2007.

    2. The result of Format is a string, so you are performing a string comparison, not a date comparison:

    If "09-07-2009" < "12-01-2007" Then

    Since 0 comes before 1, the condition is True.

    There is no need to use Format. VBA is very well able to compare dates.

    If your intention was to test whether CGior is before January 12, 2007, use

    If Me!CGior < #1/12/2007# Then

    and if you wanted to test whether it is before December 1, 2007, use

    If Me!CGior < #12/1/2007# Then

    I used Me instead of Forms!GioRep - if you ever decide to rename the form, Me will still work.[/quote]

    Thank you Hans :-)

Posting Permissions

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