Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    tbjmobile@gmail.com is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    127

    How many Thursdays in current month

    Hi,
    Can anyone tell me how I can find the integer of how many Thursdays there are in any current month please?
    I've got as far as;
    =DateDiff("w",DateSerial(Year(Date()),Month(Date() ),1)-1,DateSerial(Year(Date()),Month(Date())+1,1)-1,5)


    But changing the system month, it fails at March 2018 giving 4 weeks. I've tried enclosing a -1 in ()s in the first Dateseial part because Datediff doesn't count the first day but, from there, I'm lost.
    Thanks,
    Trevor.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    send the function a date (or today)
    usage:
    msgbox getThursdayCount(Date)

    Code:
    Function getThursdayCount(byval pvDate)
    Dim vDat
    Dim i As Integer
    
    
    vDat = Month(pvDate) & "/1/" & Year(pvDate)
    While Format(vDat, "w") <> vbThursday
       vDat = DateAdd("d", 1, vDat)
    Wend
    
    
    While Month(vDat) = Month(pvDate)
       i = i + 1
       vDat = DateAdd("d", 7, vDat)
    Wend
    getThursdayCount = i
    End Function

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Here is another

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : NumSpecificDayInMonth
    ' Author    : mellon
    ' Date      : 08-Feb-2018 (from google search)
    ' Purpose   : To get the number of specific weekdays in a month
    '
    '  datThis starting day of the Month in question eg 1-Feb-2018  is #2/1/2018#
    '  intDay  is the weekday you want to count  eg 1-sun 2-Mon.....7-Sat (defaults to MOnday)
    '---------------------------------------------------------------------------------------
    'Public Function NumSpecificDayInMonth(ByVal datThis As Date, _
                                Optional ByVal intDay As Integer = vbMonday) As Integer
                                
                                
    10  datThis = DateAdd("d", 1 - Day(datThis), datThis)
    20  intDay = (intDay Mod 7) + 1
    30  intDay = WeekDay(datThis, intDay) - 1
    40  NumSpecificDayInMonth = DateDiff("d", datThis, DateAdd("m", 1, datThis))
    50  NumSpecificDayInMonth = (NumSpecificDayInMonth + intDay) \ 7
    End Function
    Test for Feb 2018

    Code:
    ?NumSpecificDayInMonth(#2/1/2018#,5) '5 is Thursday (based on Sun=1, Mon=2.....)
    
    4

  4. #4
    tbjmobile@gmail.com is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    127
    Thank you both for your responses. I've not attempted any vba yet but I guess that it's the best way for this one so I've started this afternoon.
    Trevor.

  5. #5
    tbjmobile@gmail.com is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    127
    Hello,
    I've done some studying of vab in this code and I'm now pretty sure of how the code works but I have two questions;
    1) If my system date is European e.g. 15/03/2018 do I need to change anything (maybe
    2) I want to use the result integer 3 times in my report so how would I do that, how would I get that value (getthursdaycount or numspecificdayinmonth or whatever) into a textbox that is on my report to get that value?
    Thanks.

  6. #6
    tbjmobile@gmail.com is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    127
    I forgot to say that I want to run the procedure on reportLoad so that the number of Thursdays in Month is already in the form and being used to calculate weekly costs.

  7. #7
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by tbjmobile@gmail.com View Post
    1) If my system date is European e.g. 15/03/2018 do I need to change anything (maybe
    I would have to say "Yes", you will have to convert the date to American format, based on the info from Allen Browne at
    http://www.allenbrowne.com/ser-30.html
    Look for "Dates in Strings" at the bottom of the page.

    After testing, I had to modify the SQLDate function.

    So the function to convert European date format to American format is
    Code:
    Function fSQLDate(vDate As Variant) As Date
        If IsDate(vDate) Then
            fSQLDate = CDate(Format(vDate, "mm\/dd\/yyyy"))
        End If
    End Function
    NOTE: I added a prefix "f" (now fSQLDate) to distinguish this function from the original.

    Example usage
    Code:
    ?NumSpecificDayInMonth(fSQLDate(#15/3/2018#),5)


    -----------------------------------------------------------------------

    Quote Originally Posted by tbjmobile@gmail.com View Post
    2) I want to use the result integer 3 times in my report so how would I do that, how would I get that value (getthursdaycount or numspecificdayinmonth or whatever) into a textbox that is on my report to get that value?
    Not sure I understand what you are asking, but there is nothing stopping you from referencing the form control (getthursdaycount or numspecificdayinmonth or whatever) multiple times in a report.

  8. #8
    tbjmobile@gmail.com is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    127
    Hi Steve and thanks for responding again. Understood re. the date and thank yoiu for the code.
    Regarding the other question, although I've been trying with this for 3 days and using all the online resources I can find, they don't seem to have helped me. Your word referencing is a perfect one. I want to have the number of Thursdays in the month, appear on the report straight away when it opens. Access is giving me Private Sub Report_Load() and as I want a return I suppose I must change that to a function not a sub, and As Integer. At the moment I've been filling in the number in a field in the table each month, but I've seen that I shouldn't use table field names on the report so it will have to be a textbox, namely numspecificdayinmonth. Is this unbound and how do I reference it from the vba? I have the [Event procedure] and click through to the code window. Access has already made a Report_rptAccount item in my Microsoft Access Class Objects folder. What I have failed to get right so far is to get the numspecificdayinmonth.value into it's textbox or any other property on the report. I know that I am trying to run before I can walk but this will be just one database for my daughter's business so I won't have the time to learn everything right the way through, to only use a small part of Access' power, once.
    Another thing which is puzzling me is that when I compile what code I have tried, I don't get any error message, just a message box asking if I want to save the Macro ??
    Thanks again.

  9. #9
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    In your reports load event simple set
    Me.YourUnboundTextBox = NumSpecificDayInMonth(fSQLDate(#15/3/2018#),5)

    Or have I missed something ?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  10. #10
    tbjmobile@gmail.com is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    127
    Hello Minty,
    My original question was to ask for some code to find the amount of Thursdays in any month (from system date). Guys have kindly sent me code but I want it to run on report load and it's that that I'm stuck with. Everything I try compiles alright but doesn't fill my field from the table, or a textbox.
    Regards,
    Trevor.

  11. #11
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    but I want it to run on report load and it's that that I'm stuck with.
    Why are you stuck with/ Why do you want to execute the function from the report load event? I think I have only ever used report events 2 or 3 times.

    What is the record source of the report? A table or a query?

    I (almost) always use queries as the record sources for forms/reports. In the query , you can have a column for the "TotalDaysInMonth". When the report starts to open, the query is executes, the report controls are populated.

    Maybe I am missing something...........

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  13. #13
    tbjmobile@gmail.com is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    127

    Re. How many Thursdays in current month

    LPS 07032018 1017.zipHello again All,
    This is my database so far. There is no real data in it. The rptAccount will only be accessed/used once per month so I wanted to calculate the Thursdays in the month on/before opening it, so that we didn't forget to do so and lose money by only charging for 4 weeks instead of 5. Why Thursdays? Historically flights only arrived here on Thursdays from the UK, our principal visitors, so regular weekly charges are based on how many Thursdays are in the month. ChgWks is a different concept, it is dependent on how long the visitor stays and what level of service is required called LimpCode. There are 4 queries to run before the report is opened and I know I can later reduce the amount of them. I hope this explains better the requirement and concept. As I have said earlier in the thread, I have tried many times to place the calculated value from the vba into the report with no success yet, I either get prompts for Thursdays or nothing at all. At the moment the report is using the tblAccount field called ThursInMonth to calculate the other controls.
    Thanks again,
    Trevor.

  14. #14
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Sorry I can't open that I'm using Access 2010.
    It appears though as you are storing the value for Thursdays in the month in a table? That should be unnecessary if you can calculate it.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  15. #15
    tbjmobile@gmail.com is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    127
    Hi Minty,
    I've tried to save as compatible with 2003 or 2007 but can't because it apparently uses non-compatible things. I know I'm using a table field at the moment and I did refer to this in the post. It's so that folk can see what I want when my calculation problem is solved.
    Thanks for looking though.

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

Similar Threads

  1. Replies: 2
    Last Post: 02-12-2015, 05:14 AM
  2. Get first day and month of current year
    By Ruegen in forum Programming
    Replies: 3
    Last Post: 12-01-2014, 06:45 PM
  3. Replies: 6
    Last Post: 05-05-2014, 10:51 PM
  4. Data validation: input Thursdays only
    By dgtampa in forum Forms
    Replies: 2
    Last Post: 06-22-2012, 04:42 PM
  5. How to get a specific number for the current month?
    By Ronald Mcdonald in forum Access
    Replies: 1
    Last Post: 06-14-2012, 11:55 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