Date Time Calculations

G

Guest

I have a form which has a StartDate, StartTime, EndDate and EndTime. I need
to be able to calculate how long a process takes in hours and minutes, but it
could start on one day and finish on another. Can it be done with these
fields or would it be better to (if possible) have date and time in one field?

Thanks in advance

Nigel
 
G

Guest

Insert the following into a module. I called mine "SLAdatediff".

You need to have a table called "tblHolidays" with a field called
"HolidayDate".

In the query

Expr1: Workingdays2([Startdate],[enddate])




Public Function WorkingDays2(StartDate As Date, EndDate As Date) As Integer

'....................................................................
' Name: WorkingDays2
' Inputs: StartDate As Date
' EndDate As Date
' Returns: Integer
' Author: Arvin Meyer
' Date: May 5,2002
' Comment: Accepts two dates and returns the number of weekdays
' between them
' Note that this function has been modified to account for holidays.
' It requires a table
' named tblHolidays with a field named HolidayDate.
'....................................................................
On Error GoTo Err_WorkingDays2

Dim intCount As Integer
Dim rst As DAO.Recordset
Dim db As DAO.Database

Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays",
dbOpenSnapshot)

'StartDate = StartDate + 1
'To count StartDate as the 1st day comment out the line above

intCount = 0

Do While StartDate < EndDate

rst.FindFirst "[HolidayDate] = #" & Format(StartDate, "mm-dd-yyyy") & "#"
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday Then
If rst.NoMatch Then intCount = intCount + 1
End If

StartDate = StartDate + 1

Loop

WorkingDays2 = intCount

Exit_WorkingDays2:
Exit Function

Err_WorkingDays2:
Select Case Err

Case Else
MsgBox Err.Description
Resume Exit_WorkingDays2
End Select

End Function
 
G

Guest

Will this give me days and minutes between two date and times?

scubadiver said:
Insert the following into a module. I called mine "SLAdatediff".

You need to have a table called "tblHolidays" with a field called
"HolidayDate".

In the query

Expr1: Workingdays2([Startdate],[enddate])




Public Function WorkingDays2(StartDate As Date, EndDate As Date) As Integer

'....................................................................
' Name: WorkingDays2
' Inputs: StartDate As Date
' EndDate As Date
' Returns: Integer
' Author: Arvin Meyer
' Date: May 5,2002
' Comment: Accepts two dates and returns the number of weekdays
' between them
' Note that this function has been modified to account for holidays.
' It requires a table
' named tblHolidays with a field named HolidayDate.
'....................................................................
On Error GoTo Err_WorkingDays2

Dim intCount As Integer
Dim rst As DAO.Recordset
Dim db As DAO.Database

Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays",
dbOpenSnapshot)

'StartDate = StartDate + 1
'To count StartDate as the 1st day comment out the line above

intCount = 0

Do While StartDate < EndDate

rst.FindFirst "[HolidayDate] = #" & Format(StartDate, "mm-dd-yyyy") & "#"
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday Then
If rst.NoMatch Then intCount = intCount + 1
End If

StartDate = StartDate + 1

Loop

WorkingDays2 = intCount

Exit_WorkingDays2:
Exit Function

Err_WorkingDays2:
Select Case Err

Case Else
MsgBox Err.Description
Resume Exit_WorkingDays2
End Select

End Function





Nigel said:
Yes please.
 
G

Guest

Not sure to be honest.




Nigel said:
Will this give me days and minutes between two date and times?

scubadiver said:
Insert the following into a module. I called mine "SLAdatediff".

You need to have a table called "tblHolidays" with a field called
"HolidayDate".

In the query

Expr1: Workingdays2([Startdate],[enddate])




Public Function WorkingDays2(StartDate As Date, EndDate As Date) As Integer

'....................................................................
' Name: WorkingDays2
' Inputs: StartDate As Date
' EndDate As Date
' Returns: Integer
' Author: Arvin Meyer
' Date: May 5,2002
' Comment: Accepts two dates and returns the number of weekdays
' between them
' Note that this function has been modified to account for holidays.
' It requires a table
' named tblHolidays with a field named HolidayDate.
'....................................................................
On Error GoTo Err_WorkingDays2

Dim intCount As Integer
Dim rst As DAO.Recordset
Dim db As DAO.Database

Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays",
dbOpenSnapshot)

'StartDate = StartDate + 1
'To count StartDate as the 1st day comment out the line above

intCount = 0

Do While StartDate < EndDate

rst.FindFirst "[HolidayDate] = #" & Format(StartDate, "mm-dd-yyyy") & "#"
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday Then
If rst.NoMatch Then intCount = intCount + 1
End If

StartDate = StartDate + 1

Loop

WorkingDays2 = intCount

Exit_WorkingDays2:
Exit Function

Err_WorkingDays2:
Select Case Err

Case Else
MsgBox Err.Description
Resume Exit_WorkingDays2
End Select

End Function





Nigel said:
Yes please.

:


Do you want to exclude weekends and public holidays?



:

Persumably then use the Datediff function?

:

It would be muuuuuch better to have date and time in one field.

I have a form which has a StartDate, StartTime, EndDate and EndTime. I
need
to be able to calculate how long a process takes in hours and minutes, but
it
could start on one day and finish on another. Can it be done with these
fields or would it be better to (if possible) have date and time in one
field?

Thanks in advance

Nigel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top