Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32

    Help with understanding code and getting query to run

    I'm trying to calculate the amount of WORKING time between two dates in an Access database. At the moment i am just subtracting one date from another but this gives me all of the time in between including weekends and evenings.

    I need this time to be calculated in hours.

    In Excel i know there is a NETWORKINGDAYS function which does something similar but with days rather than hours.

    At least if i could get the working days i could then convert it into hours.

    I found this code:

    Option Compare Database


    Public Function NetWorkhours(dteStart As Date, dteEnd As Date) As Single


    Dim intGrossDays As Integer
    Dim intGrossHours As Single
    Dim dteCurrDate As Date
    Dim i As Integer
    Dim WorkDayStart As Date
    Dim WorkDayend As Date
    Dim nonWorkDays As Integer
    Dim StartDayhours As Single
    Dim EndDayhours As Single


    NetWorkhours = 0
    nonWorkDays = 0
    'Calculate work day hours on 1st and last day


    WorkDayStart = DateValue(Rebuild_Start_Date_Time) + TimeValue("06:00am")
    WorkDayend = DateValue(Rebuild_Complete_Date_Time) + TimeValue("02:00pm")
    StartDayhours = DateDiff("n", Rebuild_Start_Date_Time, WorkDayend)
    EndDayhours = DateDiff("n", WorkDayStart, Rebuild_Complete_Date_Time)
    'adjust for time entries outside of business hours


    If StartDayhours < 0 Then
    StartDayhours = 0
    End If
    If EndDayhours > 8 Then
    EndDayhours = 8
    End If


    'Calculate total hours and days between start and end times


    intGrossDays = DateDiff("d", Rebuild_Start_Date_Time, Rebuild_Complete_Date_Time)
    intGrossHours = DateDiff("n", Rebuild_Start_Date_Time, Rebuild_Complete_Date_Time)


    'count number of weekend days and holidays (from a table called "Holidays" that lists them)
    For i = 0 To intGrossDays
    dteCurrDate = dteStart + i
    If Weekday(dteCurrDate, vbSaturday) < 3 Then
    nonWorkDays = nonWorkDays + 1
    Else
    If Not IsNull(DLookup("[HolDate]", "Holidays", "[HolDate] = #" & Int(dteCurrDate) & "#")) Then
    nonWorkDays = nonWorkDays + 1
    End If
    End If
    Next i


    'Calculate number of work hours


    Select Case intGrossDays
    Case 0
    'start and end time on same day
    NetWorkhours = intGrossHours
    Case 1
    'start and end time on consecutive days
    NetWorkhours = NetWorkhours + StartDayhours
    NetWorkhours = NetWorkhours + EndDayhours
    Case Is > 1
    'start and end time on non consecutive days
    NetWorkhours = NetWorkhours - (nonWorkDays * 1)
    NetWorkhours = (intGrossDays - 1 - nonWorkDays) * 8
    NetWorkhours = NetWorkhours + StartDayhours
    NetWorkhours = NetWorkhours + EndDayhours


    End Select

    In my query I typed: workinghours(([Replacement Event]![Date/Time Start Rebuild],[Replacement Event]![Date/Time Complete Rebuild])/60)



    however when I tried to run the query I got the error message:

    Syntax error (comma) in query expression 'workinghours(([Replacement Event]![Date/Time Start Rebuild],[Replacement Event]![Date/Time Complete Rebuild])/60)'

    How can I get this query to run?

    Can anyone help?

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I did not review the function you pasted here but you would call it like this.

    NetWorkhours(01 - 01 - 2013, 01 - 01 - 2014)

    I believe that is the correct format to assign to a variable type Date.

    What you can do is assign dates to a variable behind a form and then place your declared variables in the function call

    Dim dtStart as date
    Dim dtEnd as date
    Dim sglResult as single

    dtStart = Me.TextBoxOne
    dtEnd = Me.TextBoxTwo

    sglResult = NetWorkhours(dtStart, dtEnd)
    msgbox sglResult

  3. #3
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    I am new to Access so please bare with me.

    will assigning a date to a variable allow me to store (or see in Query) the networkhours between the two dates or will it only display the networkhours in a message box?

    I have also made the changes and this is what is showing:
    Click image for larger version. 

Name:	Networkhours.JPG 
Views:	17 
Size:	22.2 KB 
ID:	17502

    Click image for larger version. 

Name:	Date2.JPG 
Views:	17 
Size:	45.5 KB 
ID:	17503

    Can you help with this?

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by radguy View Post
    ...I have also made the changes and this is what is showing:
    What changes did you make? Can you post the code? Please use the Go Advanced option when posting here in the forum. There is a tool there that you can use to wrap code in Tags to preserve formatting (#).

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Going back to your original post - the error is I think a mis-placed opening bracket:

    workinghours(([Replacement Event]![Date/Time Start Rebuild],[Replacement Event]![Date/Time Complete Rebuild])/60)

    should be

    (workinghours([Replacement Event]![Date/Time Start Rebuild],[Replacement Event]![Date/Time Complete Rebuild])/60)

    HTH

    John

  6. #6
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    Quote Originally Posted by ItsMe View Post
    What changes did you make? Can you post the code? Please use the Go Advanced option when posting here in the forum. There is a tool there that you can use to wrap code in Tags to preserve formatting (#).
    I've changed the input value for the Query so that it reads Networkinghours(([Replacement Event]![Date/Time Start Rebuild],[Replacement Event]![Date/Time Complete Rebuild])/60). I am not sure how to put the changes you suggested into the code so that it can run.

    Here is the code:

    Code:
    Option Compare Database
    
    
    Public Function NetWorkhours(dteStart As Date, dteEnd As Date) As Single
    
    
    Dim intGrossDays As Integer
    Dim intGrossHours As Single
    Dim dteCurrDate As Date
    Dim i As Integer
    Dim WorkDayStart As Date
    Dim WorkDayend As Date
    Dim nonWorkDays As Integer
    Dim StartDayhours As Single
    Dim EndDayhours As Single
    
    
    NetWorkhours = 0
    nonWorkDays = 0
    'Calculate work day hours on 1st and last day
    
    
    WorkDayStart = DateValue(Rebuild_Start_Date_Time) + TimeValue("06:00am")
    WorkDayend = DateValue(Rebuild_Complete_Date_Time) + TimeValue("02:00pm")
    StartDayhours = DateDiff("n", Rebuild_Start_Date_Time, WorkDayend)
    EndDayhours = DateDiff("n", WorkDayStart, Rebuild_Complete_Date_Time)
    'adjust for time entries outside of business hours
    
    
    If StartDayhours < 0 Then
    StartDayhours = 0
    End If
    If EndDayhours > 8 Then
    EndDayhours = 8
    End If
    
    
    'Calculate total hours and days between start and end times
    
    
    intGrossDays = DateDiff("d", Rebuild_Start_Date_Time, Rebuild_Complete_Date_Time)
    intGrossHours = DateDiff("n", Rebuild_Start_Date_Time, Rebuild_Complete_Date_Time)
    
    
    'count number of weekend days and holidays (from a table called "Holidays" that lists them)
    For i = 0 To intGrossDays
    dteCurrDate = dteStart + i
    If Weekday(dteCurrDate, vbSaturday) < 3 Then
    nonWorkDays = nonWorkDays + 1
    Else
    If Not IsNull(DLookup("[HolDate]", "Holidays", "[HolDate] = #" & Int(dteCurrDate) & "#")) Then
    nonWorkDays = nonWorkDays + 1
    End If
    End If
    Next i
    
    
    'Calculate number of work hours
    
    
    Select Case intGrossDays
    Case 0
    'start and end time on same day
    NetWorkhours = intGrossHours
    Case 1
    'start and end time on consecutive days
    NetWorkhours = NetWorkhours + StartDayhours
    NetWorkhours = NetWorkhours + EndDayhours
    Case Is > 1
    'start and end time on non consecutive days
    NetWorkhours = NetWorkhours - (nonWorkDays * 1)
    NetWorkhours = (intGrossDays - 1 - nonWorkDays) * 8
    NetWorkhours = NetWorkhours + StartDayhours
    NetWorkhours = NetWorkhours + EndDayhours
    
    
    End Select
    End Function

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I missed the part about the Query Criteria earlier. So,

    ... ]![Date/Time Complete Rebuild])/60 ...

    Is not going to work with the function you have. The parameters are set to accept Data Type Date. You cannot just divide a date, or a number, by 60 and pass it to your function.

    I hope this sheds some light as to why the error and why post #2 offers examples in VBA how to test the function.

  8. #8
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    This is the query that I put into my database: NetWorkhours([Replacement Event]![Date/Time Start Rebuild],[Replacement Event]![Date/Time Complete Rebuild])

    It still gives me the error that I posted in post #3.

    How should I incorporate post #2 with the code that I am using in order to make it work?

    Sorry for all the questions, but I am new to this.

  9. #9
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    NetWorkhours(01 - 01 - 2013, 01 - 01 - 2014)

    I believe that is the correct format to assign to a variable type Date.
    No, that's not correct for dates. Try:

    NetWorkhours(#01/01/2013#, #01/01/2014")

    Dates used in this context use mm/dd/yyyy format.

    John
    Last edited by John_G; 07-28-2014 at 11:08 AM. Reason: To correct information given

  10. #10
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Going back to your post #3 -

    What is in the form field or variable Rebuild_Start_Date_Time? The DateValue function expects a string that can be interpreted as a valid date, otherwise you will get that Type Mismatch error.

    John

  11. #11
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    Click image for larger version. 

Name:	Screenshot of Form.JPG 
Views:	12 
Size:	160.3 KB 
ID:	17514
    Quote Originally Posted by John_G View Post
    Going back to your post #3 -

    What is in the form field or variable Rebuild_Start_Date_Time? The DateValue function expects a string that can be interpreted as a valid date, otherwise you will get that Type Mismatch error.

    John
    Attached is the screen shot for the table n design view.

    Click image for larger version. 

Name:	screenshot of form field.JPG 
Views:	11 
Size:	141.2 KB 
ID:	17510

    Here is the form in Design View:
    Click image for larger version. 

Name:	form in design view.jpg 
Views:	11 
Size:	270.4 KB 
ID:	17513
    Last edited by radguy; 07-28-2014 at 01:10 PM. Reason: to include form in design view

  12. #12
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I think it's a typo. Going back to post #3, the line with the error has Rebuild_Start_Date_Time.

    Your form control is called Rebuild Start Date/Time, so your code should be using that.

    Here's a suggestion:

    In EVERY module of code, include Option Explicit following Option Compare Database.

    This forces you to explicitly declare all variables with a Dim statement.

    Then, after ANY coding change, compile your code (Debug - Compile in the VBA coding window).

    You would be amazed at how many mistakes this will catch, and I'm almost certain it would have caught this one.

    John

  13. #13
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    I have compiled my code and this is what occurs:

    Click image for larger version. 

Name:	Variable not defined.JPG 
Views:	11 
Size:	140.7 KB 
ID:	17518

    How do I declare my variable?

    Is it Dim Rebuild_Start_Date_Time Double


    ​Will it also still be linked to the field that I am trying to reference?

  14. #14
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I took a look at your function and I do not see it working because Rebuild_Start_Date_Time is not defined anywhere. It seems to be a constant or something that was declared somewhere and now is missing. It should be dimensioned as a String and the value should be a date.

  15. #15
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    Thanks for your help John. I am now able to run the code.

    I have one more question, toward the bottom it says that I should create a separate table labeled "Holidays." There I can list out all of the days that I would like to be excluded from the calculation of time between two points. Do I have the data type correct as a text or should it be date & time?

    Attached are two screen shots of the table:

    Click image for larger version. 

Name:	Holidays (2).JPG 
Views:	9 
Size:	30.8 KB 
ID:	17538Click image for larger version. 

Name:	Holidays.JPG 
Views:	9 
Size:	118.9 KB 
ID:	17539

    This is my code:
    Code:
    Option Compare Database
    Option Explicit
    
    
    Public Function NetWorkhours(Rebuild_Start_Date_Time As Date, Rebuild_Complete_Date_Time As Date) As Single
    
    
    Dim intGrossDays As Integer
    Dim intGrossHours As Single
    Dim dteCurrDate As Date
    Dim i As Integer
    Dim WorkDayStart As Date
    Dim WorkDayend As Date
    Dim nonWorkDays As Integer
    Dim StartDayhours As Single
    Dim EndDayhours As Single
    
    
    
    
    NetWorkhours = 0
    nonWorkDays = 0
    'Calculate work day hours on 1st and last day
    
    
    WorkDayStart = DateValue(Rebuild_Start_Date_Time) + TimeValue("06:00am")
    WorkDayend = DateValue(Rebuild_Complete_Date_Time) + TimeValue("02:00pm")
    StartDayhours = DateDiff("n", Rebuild_Start_Date_Time, WorkDayend)
    EndDayhours = DateDiff("n", WorkDayStart, Rebuild_Complete_Date_Time)
    'adjust for time entries outside of business hours
    
    
    If StartDayhours < 0 Then
    StartDayhours = 0
    End If
    If EndDayhours > 8 Then
    EndDayhours = 8
    End If
    
    
    'Calculate total hours and days between start and end times
    
    
    intGrossDays = DateDiff("d", Rebuild_Start_Date_Time, Rebuild_Complete_Date_Time)
    intGrossHours = DateDiff("n", Rebuild_Start_Date_Time, Rebuild_Complete_Date_Time)
    
    
    'count number of weekend days and holidays (from a table called "Holidays" that lists them)
    For i = 0 To intGrossDays
    dteCurrDate = Rebuild_Start_Date_Time + i
    If Weekday(dteCurrDate, vbSaturday) < 3 Then
    nonWorkDays = nonWorkDays + 1
    Else
    If Not IsNull(DLookup("[HolDate]", "Holidays", "[HolDate] = #" & Int(dteCurrDate) & "#")) Then
    nonWorkDays = nonWorkDays + 1
    End If
    End If
    Next i
    
    
    'Calculate number of work hours
    
    
    Select Case intGrossDays
    Case 0
    'start and end time on same day
    NetWorkhours = intGrossHours
    Case 1
    'start and end time on consecutive days
    NetWorkhours = NetWorkhours + StartDayhours
    NetWorkhours = NetWorkhours + EndDayhours
    Case Is > 1
    'start and end time on non consecutive days
    NetWorkhours = NetWorkhours - (nonWorkDays * 1)
    NetWorkhours = (intGrossDays - 1 - nonWorkDays) * 8
    NetWorkhours = NetWorkhours + StartDayhours
    NetWorkhours = NetWorkhours + EndDayhours
    
    
    End Select
    End Function

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. SQL Query - Understanding the logic.
    By BayEnder in forum Access
    Replies: 2
    Last Post: 02-17-2013, 12:45 PM
  2. Need help understanding capabilities
    By squall in forum Database Design
    Replies: 2
    Last Post: 08-27-2012, 04:28 PM
  3. Understanding afterupdate
    By MAM8433 in forum Access
    Replies: 8
    Last Post: 05-23-2011, 02:05 PM
  4. Not understanding Running Sum Query
    By dynamictiger in forum Queries
    Replies: 4
    Last Post: 08-30-2010, 11:50 AM
  5. Understanding SQL Querys
    By jacobbiljo in forum Access
    Replies: 8
    Last Post: 11-17-2009, 05:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums