|
-
Jul 10th, 2007, 12:56 PM
#1
Thread Starter
New Member
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
-
Jul 10th, 2007, 01:00 PM
#2
Re: Using Date diff function....
Moved from the FAQ Section
-
Jul 10th, 2007, 01:34 PM
#3
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
-
Jul 10th, 2007, 01:36 PM
#4
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|