Results 1 to 4 of 4

Thread: Using Date diff function....

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    1

    Using Date diff function....

    I coded
    Dim d1, d2 As Date
    Private Sub DTPicker1_Change()

    d1 = DTPicker1.Value
    End Sub

    Private Sub DTPicker2_Change()
    d2 = DTPicker2.Value
    End Sub
    Private Sub txt_days_Click()
    Dim d3
    dt = DateDiff("d", d1, d2)
    txt_days = dt
    End Sub


    //Above code is giving a wrong answerlike 32789...pls help...
    //I have added two dtp functions in a form..and want to show the their difference as days in txt_days

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Using Date diff function....

    Moved from the FAQ Section

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Using Date diff function....

    Problem 1: D1 is a variant, not a date.
    Problem 2: The use of D1 and D2 is added junk you don't need. Just access the DTPickers directly when using datediff.
    Problem 3: You're not even using d3.
    Problem 4: You didn't tell us what kind of dates were selected in the DateTimePickers.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Using Date diff function....

    Welcome to VBForums

    One problem is that you haven't declared variables appropriately.. unfortunately VB will not declare d1 as a Date, to do that you need to use this:
    Code:
    Dim d1 As Date, d2 As Date
    Note that you don't actually need these two variables anyway, as you can simply use DTPicker1.Value or DTPicker2.Value instead (as the variables change as soon as the control values do).

    You also haven't declared dt at all, which means that VB creates it for you, with the wrong data type! I would recommend declaring it as a long (a big integer), eg:
    Code:
    Dim dt as Long
    Note that you may get a negative answer (if d1 is an earlier date than d2), so if you just want to know the number of days you should use the Abs function, eg:
    Code:
    dt = Abs(DateDiff("d", d1, d2))

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