tags:

views:

53

answers:

1

Hi All;

Newbie to VB6 please help, Struggling with something I just can’t get right, label1 display a last transaction date/time which I get from a date base throw a query label2 is the system date/time. I have a timer that execute a command button after executing the command button I want to check if the date/time in label1 is smaller than 5 minutes and if so then I get a massage. But I don’t know how all my code fails to perform this function help will be much appreciated.

Private Sub Command1_Click()
    Dim date1 As Date
    Dim date2 As Date

    date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
    date2 = Format(label1, "yyyy/mm/dd hh:mm:ss")
    If DateDiff("n", date1, date2) < 2 Then
       MsgBox ("Not Vending")
    End If
End Sub

ALSO

Private Sub Command1_Click()
    Dim date1 As Date
    Dim label1 As Date

    date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
    date2 = label1
    If DateDiff("m", Now, date1) > DateDiff("m", Now, label1) Then
       MsgBox ("Not Vending")
    End If
End Sub

Also

Private Sub Command1_Click()  
    If DateDiff("n", Now, label1) > 5 Then
       MsgBox ("Not Vending")
    End If
End Sub
A: 

If the date pulled from the DB is earlier than Now, DateDiff will always return a negative number if you pass Now as the second parameter. It looks like you're checking for the passage of time, so I'll assume the dates in DB will always be before Now. You need to switch the order of Now and the date to which it is being compared (DateDiff("n", date1, Now) instead of DateDiff("n", Now, date1).

Private Sub Command1_Click()
    Dim date1 As Date
    date1 = Format(Label1, "yyyy/mm/dd hh:mm:ss")
    If DateDiff("n", date1, Now) < 5 Then
       MsgBox ("Not Vending")
    End If
End Sub
raven
Beaner
Thanks for the reply got it sorted.