Date Field to be the next given Monday (1 Viewer)

RussPhi

New member
Local time
Today, 12:56
Joined
Jan 3, 2020
Messages
12
Hi
I have a date field called (EndDate) that i can pick on the date picker "cumbersome" what I want to do on a "dble click" is to change the date to the monday of next week. So if today is Monday the 28th of Nov I want the date field to show next Monday the 5th of Dec. and if today is Nov 29th Tuesday I want it to show Monday the 5th of Dec. so no matter what the current day is this week I want the next week Monday. hope this makes sense.

Much tanks for any help.
 

bob fitz

AWF VIP
Local time
Today, 20:56
Joined
May 23, 2011
Messages
4,727
The attached file has a number of Useful Date Functions. I'm sure it will have something of use to you.
I take NO CREDIT for its conception. Our thanks go to Pat Hartman for its existence.
 

Attachments

  • UsefulDateFunctions.zip
    189.1 KB · Views: 87

theDBguy

I’m here to help
Staff member
Local time
Today, 12:56
Joined
Oct 29, 2018
Messages
21,474
Here's what I use:
Code:
DateAdd("d", 8-Weekday(Date(),2), Date())
Hope that helps...
 

bob fitz

AWF VIP
Local time
Today, 20:56
Joined
May 23, 2011
Messages
4,727
I think the function you will need is:
Code:
Function fDateDayAfter(dtmDate As Date, bytDaySought As Byte) As Date
'   Function to return the date of the named day after the passed date
'   Accepts:dtmDate - any date
'   bytDaySought (e.g. vbMonday)
    If bytDaySought - WeekDay(dtmDate) >= 0 Then
        fDateDayAfter = dtmDate + bytDaySought - WeekDay(dtmDate)
    Else
        fDateDayAfter = dtmDate + bytDaySought - WeekDay(dtmDate) + 7
    End If
End Function
which can be found in the code module of the form called sfrmFirstWeekday
 

RussPhi

New member
Local time
Today, 12:56
Joined
Jan 3, 2020
Messages
12
1669740059996.png
this is what I'm geting what am i doing wrong.
 

bob fitz

AWF VIP
Local time
Today, 20:56
Joined
May 23, 2011
Messages
4,727
Try:
Me.EndDate = DateAdd("d", 8-Weekday(Date(),2), Date())
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:56
Joined
Feb 19, 2002
Messages
43,280
Thanks @bob fitz ,
Here's a link to the most recent version. You have a very old version. Ten years:) This was one of the first samples I ever made. I also added a change log to the newest version so you can see some of the changes I made in the past couple of years. If you have copies of my other sample db's you might want to see if there is a newer version in the sample-date-functions. I've updated almost all of the samples within the past two years:) Here's all the links



----------
 

bob fitz

AWF VIP
Local time
Today, 20:56
Joined
May 23, 2011
Messages
4,727
Here's a link to the most recent version. You have a very old version. Ten years:) This was one of the first samples I ever made. I also added a change log to the newest version so you can see some of the changes I made in the past couple of years. If you have copies of my other sample db's you might want to see if there is a newer version in the sample-date-functions. I've updated almost all of the samples within the past two years:) Here's all the links
:eek: 10 years. Where did they go? I hadn't realized I'd been a fan for so long. I've used several of your functions over the years and offered them to many posters here and on other forums. Thanks for the link Pat.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:56
Joined
Feb 19, 2002
Messages
43,280
@RussPhi
theDBguy offered an alternate solution. I think it uses an undefined function which will be the next error you get after you take Bob's suggestion on how to fix the current error. Please look at Bob's code again. You are missing the first part of the expression which tells Access where you want the date to be placed.

If you want to bother to look at the example database, you might find the calculation easier to understand. I've excerpted it so you don't even have to take the time to look at the sample. Here is the code to use the function from my database.

Me.EndDate= fDateDayAfter(Date(), vbMonday)

If you look at the sample database, you will see that the two arguments in the sample come from fields on the form. Above, I hardcoded them so that they are always today and always monday which may be easier for you to understand but far less flexible although suitable for this situation..

This is the function from the sample database. Bob posted it already. I repeat it for convenience.
Code:
Function fDateDayAfter(dtmDate As Date, bytDaySought As Byte) As Date
'   Function to return the date of the named day after the passed date
'   Accepts:dtmDate - any date
'   bytDaySought (e.g. vbMonday)
    If Weekday(dtmDate) = bytDaySought Then
        fDateDayAfter = dtmDate + 7
    Else
        If bytDaySought - Weekday(dtmDate) >= 0 Then
            fDateDayAfter = dtmDate + bytDaySought - Weekday(dtmDate)
        Else
            fDateDayAfter = dtmDate + bytDaySought - Weekday(dtmDate) + 7
        End If
    End If
End Function
 
Last edited:

RussPhi

New member
Local time
Today, 12:56
Joined
Jan 3, 2020
Messages
12
I thank all of you for your wounder work and minds of knowledge as well.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:56
Joined
Sep 12, 2006
Messages
15,658
I must say that when I have needed to do something like that, I tend not to try to find an elegant one-line solution and use something I can actually follow!

I would do more

Code:
nextdate=nextdate+1 'in case today actually is a monday
while day(nextdate)<>vbmonday
    nextdate=nextdate+1   
wend
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:56
Joined
Feb 19, 2002
Messages
43,280
@gemma-the-husky The loop is a more compact solution than the one I originally posted but I agree. Especially when dealing with novices, which most of the time we are, it is important to not be too clever with the code.
 

Users who are viewing this thread

Top Bottom