Results 1 to 3 of 3

Thread: What's wrong with my code.......???????

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    445

    What's wrong with my code.......???????

    Help!! I cannot get correct value after I minus both Month value...

    lblValue.Caption = Month(CDate("May-2009") - CDate("Mar-2009"))

    The result suppose have to get lblValue.Caption = "2" but I get lblValue.Caption = "3"...
    what's wrong with this simple minus code?
    Last edited by wenight; Mar 16th, 2009 at 03:07 AM.

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: What's wrong with my code.......???????

    This is what happened:
    Code:
    lblValue.Caption = Month(CDate("May-2009") - CDate("Mar-2009"))
    lblValue.Caption = Month(#5/1/2009# - #3/1/2009#)
    lblValue.Caption = Month(61)
    lblValue.Caption = Month(CDate(61))
    lblValue.Caption = Month(#3/1/1900#)
    lblValue.Caption = 3
    If you don't care about the year, change it to:
    Code:
    lblValue.Caption = Month(CDate("May-2009")) - Month(CDate("Mar-2009"))
    Otherwise use DateDiff() function:
    Code:
    lblValue.Caption = DateDiff("m", CDate("Mar-2009"), CDate("May-2009"))
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    445

    Re: What's wrong with my code.......???????

    Quote Originally Posted by anhn
    This is what happened:
    Code:
    lblValue.Caption = Month(CDate("May-2009") - CDate("Mar-2009"))
    lblValue.Caption = Month(#5/1/2009# - #3/1/2009#)
    lblValue.Caption = Month(61)
    lblValue.Caption = Month(CDate(61))
    lblValue.Caption = Month(#3/1/1900#)
    lblValue.Caption = 3
    If you don't care about the year, change it to:
    Code:
    lblValue.Caption = Month(CDate("May-2009")) - Month(CDate("Mar-2009"))
    Otherwise use DateDiff() function:
    Code:
    lblValue.Caption = DateDiff("m", CDate("Mar-2009"), CDate("May-2009"))

    I use the third solution
    Code:
    lblValue.Caption = DateDiff("m", CDate("Mar-2009"), CDate("May-2009"))
    It's works what excacly I want.... Thanks~~~

Posting Permissions

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



Click Here to Expand Forum to Full Width