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.
Related
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
I am trying to make a loop that keeps subtracting from the initial amount of each item until the amount is less than the price of an item. It is also supposed to show the last item purchased before the amount was less than the price of an item. Cars cost $310,000, rings cost $12,500, Diamonds cost $150,000 and chocolate costs $51. The items are in a file on my computer, and the following is an example of what it should look like.
Sample Input:
350000
Car
Ring
Diamond
Car
Chocolate
Diamond
Sample Output:
Ring
$27, 500 left
For some reason, the value I get the wrong value when I subtract, but I can't figure out why. I've declared the prices for each item and checked multiple times to make sure they are correct, and I've checked my code, but I still don't know why I get the wrong output.
Private Sub btnS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnS.Click
Dim inFile As StreamReader = New StreamReader("serena.txt")
'Declare the varibles
Dim variableName As String
' the current items from the file
Dim amount As Integer
Dim price As Integer
amount = Val(inFile.ReadLine())
Do
'read in the words
variableName = inFile.ReadLine()
'determine each item's price
If variableName = "car" Then price = 310000
If variableName = "ring" Then price = 12500
If variableName = "diamond" Then price = 150000
If amount >= price Then amount = amount - price
Loop Until amount < price
'output the results
Me.lblOutput.Text = variableName & _
vbNewLine & "Serena has " & Format(amount, "currency") & " left"
End Sub
Take a close look at the COMMENTS I've placed into the code below:
Private Sub btnS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnS.Click
Dim fileName As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "serena.txt")
Dim inFile As New StreamReader(fileName)
Dim itemToPurchase As String
Dim lastThingBought As String = "{ Nothing Bought }" ' what if they can't afford anything?
Dim balance As Integer
Dim price As Integer
Dim somethingBought As Boolean
balance = Val(inFile.ReadLine())
Do
somethingBought = False ' we don't know yet if we can purchase the next item
'read in the words
itemToPurchase = inFile.ReadLine().ToLower().Trim() ' make sure it has no spaces and is lowercase to match below
'determine each item's price
Select Case itemToPurchase
Case "car"
price = 310000
Case "ring"
price = 12500
Case "diamond"
price = 150000
Case "chocolate" ' FYI: you were missing chocolate in your code
price = 51
Case Else ' it could happen!
MessageBox.Show(itemToPurchase, "Unknown Item!")
lblOutput.Text = "Error in file"
Exit Sub
End Select
If balance >= price Then
somethingBought = True
balance = balance - price
' we need to store the last thing purchased,
' because "itemToPurchase" gets replaced with the thing you couldn't afford
lastThingBought = itemToPurchase
End If
' Need to check for the end of file being reached...
' ...they may purchase everything in the file and still have money left!
Loop While Not inFile.EndOfStream AndAlso somethingBought
'output the results
Me.lblOutput.Text = lastThingBought &
vbNewLine & "Serena has " & Format(balance, "currency") & " left"
End Sub
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
i'm creating a microwave oven app for class. I have the majority of the app working great, the only problem is i'm getting a weird display output, I believe it has to do with my sub string format, but i'm not entirely sure. Basically whats happening is that if the user were to enter say 1:25 cook time, the output reads 1:125 and if start is hit, the microwave only counts down from 1:00. Any help would be greatly appreciated!!
Private Sub DisplayTime()
Dim hour As Integer
Dim second As Integer
Dim minute As Integer
Dim display As String ' String displays current input
' if too much input entered
If timeIs.Length > 5 Then
timeIs = timeIs.Substring(0, 5)
End If
display = timeIs.PadLeft(5, "0"c)
' extract seconds, minutes, and hours
second = Convert.ToInt32(display.Substring(2))
minute = Convert.ToInt32(display.Substring(1, 2))
hour = Convert.ToInt32(display.Substring(0, 1))
' display number of hours, minutes, ":" seconds
displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}",
hour, minute, second)
End Sub ' DisplayTime
' event handler displays new time each second
Private Sub clockTimer_Tick(sender As System.Object,
e As System.EventArgs) Handles clockTimer.Tick
' perform countdown, subtract one second
If timeObject.Second > 0 Then
timeObject.Second -= 1
displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}",
timeObject.Hour, timeObject.Minute, timeObject.Second)
ElseIf timeObject.Minute > 0 Then
timeObject.Minute -= 1
timeObject.Second = 59
displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}",
timeObject.Hour, timeObject.Minute, timeObject.Second)
ElseIf timeObject.Hour > 0 Then
timeObject.Hour -= 1
timeObject.Minute = 59
timeObject.Second = 59
displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}",
timeObject.Hour, timeObject.Minute, timeObject.Second)
Else ' countdown finished
clockTimer.Enabled = False ' stop timer
Beep()
displayLabel.Text = "Done!" ' inform user time is finished
windowPanel.BackColor = Control.DefaultBackColor
End If
End Sub ' clockTimer_Tick
End Class ' MicrowaveOvenForm
You Substring() for extracting the seconds portion is wrong.
Change:
second = Convert.ToInt32(display.Substring(2))
To:
second = Convert.ToInt32(display.Substring(3, 2))
*Are you required to use "timeObject" though? There are much better ways of keeping a countdown...
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.