Results 1 to 13 of 13
  1. #1
    Dutch1956 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    12

    First week or business day of the month

    Hi,



    as a novice I was able to find below code on the internet. It works great, it results in giving me the first Monday of the month. What do I need to change for the Function to return the first week or business day?
    Any help is very much appreciated.


    Public Function FirstMonday(Optional InputDate As Variant) As Date
    Dim i As Integer
    Dim dteTemp As Date

    If IsMissing(InputDate) Or Not IsDate(InputDate) Then
    dteTemp = DateAdd("m", 1, Date)
    Else
    dteTemp = DateAdd("m", 1, InputDate)
    End If

    For i = 1 To 7
    dteTemp = DateSerial(Year(dteTemp), Month(dteTemp), i)
    If Weekday(dteTemp) = vbMonday Then
    FirstMonday = dteTemp
    Exit For
    End If
    Next i
    End Function

    Bert,
    Swansea, UK

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Wouldn't that still be the 1st Monday of the month or are you wanting to adjust for that day being a holiday?

  3. #3
    Dutch1956 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    12
    Thank you for the quick reply.
    I am looking for that day to be the first working day of the Month and with working day I mean Monday thru Friday. That would make it for September Thursday 01/09/11, for October it would be 03/10/2011, and for November 01/11/11, etc. With other words I am looking to find the first date of the month as long as it is not a Saturday or Sunday.

    Thanks for your help.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    D'oh! Time for a little more coffee. That task should be pretty easy. Be right back.

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    This should do it:
    Code:
    Public Function FirstWorkDay(Optional InputDate As Variant) As Date
     
       If IsMissing(InputDate) Or Not IsDate(InputDate) Then
          FirstWorkDay = DateSerial(Year(Date), Month(Date) + 1, 1)
       Else
          FirstWorkDay = DateSerial(Year(InputDate), Month(InputDate) + 1, 1)
       End If
       Do Until Weekday(FirstWorkDay, vbMonday) < 6
          FirstWorkDay = FirstWorkDay + 1
       Loop
       
    End Function
    EDIT: My bad...be right back.
    Last edited by RuralGuy; 09-17-2011 at 07:43 PM. Reason: Okay...fixed it!

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Code in post #5 should now do what you want.

  7. #7
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by RuralGuy View Post
    This should do it:
    Code:
    Public Function FirstWorkDay(Optional InputDate As Variant) As Date
     
       If IsMissing(InputDate) Or Not IsDate(InputDate) Then
          FirstWorkDay = DateSerial(Year(Date), Month(Date) + 1, 1)
       Else
          FirstWorkDay = DateSerial(Year(InputDate), Month(InputDate) + 1, 1)
       End If
       Do Until Weekday(FirstWorkDay, vbMonday) < 6
          FirstWorkDay = FirstWorkDay + 1
       Loop
       
    End Function
    EDIT: My bad...be right back.
    Allan,

    how long has the function ISMISSING() been in Access? I didn't know it had such a function. thanks.

  8. #8
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529

  9. #9
    Dutch1956 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    12
    Hi RuralGuy,

    I hope you enjoyed your coffee because you deserve every drop of it. Your code works exactly the way I want it. Thank you so much.

    Bert,
    Rainy Swansea, UK.

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Quote Originally Posted by ajetrumpet View Post
    Allan,

    how long has the function ISMISSING() been in Access? I didn't know it had such a function. thanks.
    I don't know but it is in Access XP.

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Quote Originally Posted by Dutch1956 View Post
    Hi RuralGuy,

    I hope you enjoyed your coffee because you deserve every drop of it. Your code works exactly the way I want it. Thank you so much.

    Bert,
    Rainy Swansea, UK.
    Glad we could help Bert.

  12. #12
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by alansidman View Post
    so when you apply this technique:

    Code:
    Optional MyVar As String = "specialvalue"
    do you have to provide a value for the optional? I pretty much always do this to check for missings:

    Code:
    Optional MyVar As Boolean
    
    if MyVar then
       'value was provided
    else
       'value was NOT
    end if
    so the question is, can you use ISMISSING() for types other than variants? and if you don't have Acc 2007 and above, do you have to provide a sentinel value for optionals if you want to check for their existence?

  13. #13
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I believe a visit to the help files and MSDN is in order here.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Track capacity changes month by month
    By Nils in forum Database Design
    Replies: 1
    Last Post: 02-18-2011, 03:03 PM
  2. Replies: 5
    Last Post: 11-15-2010, 06:12 PM
  3. Replies: 1
    Last Post: 07-26-2010, 06:22 AM
  4. Loading data for our business
    By fsmikwen in forum Programming
    Replies: 2
    Last Post: 04-16-2010, 09:13 AM
  5. Small Business Question
    By P5C768 in forum Access
    Replies: 1
    Last Post: 09-28-2009, 08:27 AM

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