Compare two date times - vb6

label1 displays the last transaction date/time which I get from a database through a query. label2 is the system date/time. I have a timer that executes a command button after which I want to check if the date/time in label1 is smaller than 5 minutes. If so then I want to show a massage.
But I don’t know why my code is failing to perform this function.
Any 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
I've also tried:
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
As well as:
Private Sub Command1_Click()
If DateDiff("n", Now, label1) > 5 Then
MsgBox ("Not Vending")
End If
End Sub

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 = CDate(Label1.Caption)
If DateDiff("n", date1, Now) < 5 Then
MsgBox ("Not Vending")
End If
End Sub

An other way without using the DateDiff-function would be direct calculation with the Date-variables - since under the hood these are actually Doubles.
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
'#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
If (date1 - Now) < #12:05:00 AM# Then
MsgBox ("Not Vending")
End If
End Sub
If you want to check the time difference you can also use
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
'#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
If Abs(date1 - Now) < #12:05:00 AM# Then
MsgBox ("Not Vending")
End If
End Sub
Where Abs(date1 - Now) returns the time difference as Date-value
Btw. date1 = Format(Now, "yyyy/mm/dd hh:mm:ss") doesn't make sence. Since Now returns a Date-value, Format converts that Date-value into a String and assigning it to date1 converts that String back to a Date-value ùsing the local system settings for date formats - which means that the program will behave different on different systems.

Related

The datagrid is show wrong output in vb6

when i choose same date from month 10(say dtpckr1 = 02-10-2019 and dtpckr2 = 02-10-2019 ) ..data datagrid does not print anything and shows the msgbox not record found which i code for convinence...but when i choose start date from last moth and end date in this month(say dtpckr1 = 30-09-2019 and dtpckr2 = 02-10-2019 ) it shows all the data from month 09 and nothing from month 10 ... and the strange this is when choose date which is from moth 09 even if it is same(say dtpckr1 = 13-09-2019 and dtpckr2 = 13-09-2019 or 22-09-2019) it works perfectly ..so please try to help me out by refering the following code ..so far i found out that the data which i am getting in datagridview is as per days (dd) not as per whole date...means if i choose the date1 = 31/09/2019 and date2 = 01/10/2019 then it will show the data from date 01 to 31 only from month 09.... I also checked the date format of database and my input,they are same...in databse the date datatype is "date/time" and format is "short date"....if have any other solution then please tell me... i will try... my purpose it to show datewise food orders in datagridview and then calculate the total sale... i am newbie in vb6...so if you can edit my code and repost it ..it will be great...because i want to submit this project by tomorrow..and this is the only which is bothering me... thank you
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub cmdSearch_Click()
Dim date1 As Date
Dim date2 As Date
If IsNull(DTPicker1.Value And DTPicker2.Value) Then
MsgBox "You must select date", vbCritical, "Warning"
Exit Sub
End If
DTPicker1.Value = Format(DTPicker1.Value, "dd-mm-yyyy")
DTPicker2.Value = Format(DTPicker2.Value, "dd-mm-yyyy")
date1 = DTPicker1.Value
date2 = DTPicker2.Value
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\OrderMania\ordermania.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient
If DTPicker2.Value < DTPicker1.Value Then
MsgBox "End Date Cannot Be Lesser Then Start Date", vbCritical, "Wrong Input"
Exit Sub
Else
Adodc1.RecordSource = "select * from order1 where (date between #" & date1 & "# and #" & DTPicker2.Value & "#)"
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
MsgBox "Please Enter Another Date", vbCritical, "No Record Found"
Else
Adodc1.Caption = Adodc1.RecordSource
End If
End If
con.Close
Call sale
End Sub
Public Sub sale()
Dim i As Integer
Dim Tot, gst, gtot As Double
For i = 0 To Adodc1.Recordset.RecordCount - 1
Tot = Tot + CDbl(DataGrid1.Columns(5).Text)
Adodc1.Recordset.MoveNext
Next i
Text1.Text = Tot
gst = Tot * 0.05
Text2.Text = gst
gtot = Tot + gst
Text3.Text = gtot
End Sub
Try inverting month and day in your between clause :
..."between #" & Format(date1, "mm-dd-yyyy") & "# and #" & Format(date2, "mm-dd-yyyy")) & "#)"
But concatenation of SQL string with variables values is considered bad practice, as #GSerg remind me, since SQL injection of malicious code could occurs. You should work with parameters. If you want to study this, here is a start point : https://learn.microsoft.com/fr-fr/office/client-developer/access/desktop-database-reference/createparameter-method-ado

How to calculate the minute in DTPICKER vb6

I'm creating a payroll system I want to calculate the minute late of Employee using two dtpicker
Dtpicker1 is for time in and Dtpicker2 for Timeout
Private Sub calc_Click()
oras = DateDiff("n", DTPicker1, DTPicker2)
Text1.Text = oras
End sub
If all employees are working the same amount of hours (8 hours/day for example):
Private Sub calc_Click()
Dim iWorkdayHours As Integer
Dim iMinutesWorked As Integer
Dim iMinutesLate As Integer
' Get the amount of minutes between two dates
iMinutesWorked = DateDiff("n", DTPicker1, DTPicker2)
' Get number of hours employee should have worked
iWorkdayHours = 8
iMinutesLate = (iWorkdayHours * 60) - iMinutesWorked
If iMinutesLate > 0 Then
Text1.Text = iMinutesLate & " minutes late."
Else
Text1.Text = "On time."
End If
End Sub
If employees have different shift lengths, you can update iWorkdayHours.

Dtpicker in VB6

I am able to format my dtpicker using below code.
Private Sub Form_Load()
DTPicker1.Format = dtpCustom
DTPicker1.CustomFormat = "yyyy/MM/dd"
DTPicker1.Value = Format(Date, "yyyy/MM/dd")
End Sub
After get dtpicker value using below code
Private Sub Command1_Click()
Label34.Caption = DTPicker1.Value
End Sub
But result is not formatted like as dtpicker displayed value.
Dtpicker value : 2015/05/26
Label34 Value : 05/26/2015
How to rectify this issue ...?
Use format function (same you did for DatePicker) when assigning value to your label :
Private Sub Command1_Click()
Label34.Caption = Format(DTPicker1.Value, "yyyy/MM/dd")
End Sub
Or even better, get format from DatePicker:
Private Sub Command1_Click()
Label34.Caption = Format(DTPicker1.Value, DTPicker1.CustomFormat)
End Sub
I don't remember, but maybe DatePicker has a property giving you its value as text (having the correct format).

How to calculate timedifference in vb6

I want to calculate time difference in vb6. i had been done it in vb.net. but i don't know how to convert in vb6.Because the datetime type will not be available in vb6.
i have pasted my vb.net code below. how to do the same in vb6?
Public Sub timecal()
Dim dFrom As DateTime
Dim dTo As DateTime
Dim tempstarttime As DateTime
Dim idletime As String = "00:05:00"
Dim Needtosub As String = "00:01:00"
Dim timeDiff As String
If DateTime.TryParse(start_time, dFrom) AndAlso DateTime.TryParse(end_time, dTo) Then
Dim TS As TimeSpan = dTo - dFrom
Dim hour As Integer = TS.Hours
Dim mins As Integer = TS.Minutes
Dim secs As Integer = TS.Seconds
timeDiff = ((hour.ToString("00") + ":") + mins.ToString("00") + ":") + secs.ToString("00")
sscheck1 = False
If timeDiff >= idletime Then
tempstarttime = System.DateTime.Parse(end_time)
tempstarttime = tempstarttime.AddMinutes(-1)
start_time = Strings.Format(tempstarttime, "yyyy-MM-dd HH:mm:ss")
sscheck1 = True
End If
End If
End Sub
You can calculate the time difference between two Date values (which will include the time) using the DateDiff function
For difference in minutes:
Dim lMinutes as Long
lMinutes = DateDiff("n", dFrom, dTo)
For difference in seconds:
Dim lSeconds as Long
lSeconds = DateDiff("s", dFrom, dTo)
For difference in hours:
Dim lHours as Long
lHours = DateDiff("h", dFrom, dTo)
For the purposes of getting your string time diff in hours:minutes:seconds, I would do:
Dim lSeconds as Long, lMinutes as Long
lSeconds = DateDiff("s", dFrom, dTo)
lMinutes = Fix(lSeconds / 60) '// Gets the whole number (not rounded)
lSeconds = ((lSeconds / 60) - lMinutes) * 60 '// get the remaining seconds
sTimeDiff = "00:" & Format$(lMinutes, "00") & ":" & Format$(lSeconds, "00")
Note: If lMinutes is greater than 60, you'll need to do similar math to pull out the hours portion before pulling the minutes and seconds. This code assumes your timespan is less than an hour based on your example.
use vb DateDiff Function i.e
Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
' Assume Sunday is specified as first day of the week.
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)
Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2)
or can be done with another general way
Dim dtStart As Long
Dim dtEnd As Long
Dim result As Long
Dim i As Integer
dtStart = GetTickCount
dtEnd = GetTickCount
result = dtEnd – dtStart // miliseconds
OutAddIn.objApptItem.Start = CDbl(result) // miliseconds to date

How to get month and year from the date

How to get month & year from the date
Date = 01/02/2012 (dd/mm/yyyy)
From the above date, i want to get 02/2012 (mm/yyyy)
How to write a code in vb6
Need vb6 code help
You could use the Month() and Year() functions which would take a Date parameter.
I.e. Month(Now) would return the Month and Year(Now) would return the Year.
Dim yourDate As Date
yourDate = #01/02/2012#
Dim yourMonth As Integer
Dim yourYear As Integer
yourMonth = Month(yourDate)
yourYear = Year(yourDate)
You can use Format$:
Dim formattedDate as String
Dim myDate as Date
myDate = #1/2/2012#
formattedDate = Format$(myDate, "mm/yyyy")

Resources