In Ms Access (VBA) , How to get all the dates of the month after selecting the certain month and weekdays?

Local time
Today, 09:56
Joined
Oct 7, 2016
Messages
43
In Ms Access (VBA) , How to get all the dates of the month after selecting the certain month and weekdays?
 
Are you creating a calendar? Your explanation is vague.

"Get all the dates of the month" means how many days there are? If so
Code:
Dim FirstDay As Date
FirstDay = DateSerial(Year(YourDate),Month(YourDate),1)

Dim LastDay As Date
'The 0 as day returns the last day of the previous month
LastDay = DateSerial(Year(YourDate),Month(YourDate)+1,0)

Dim DaysOfMonth as long
DaysOfMonth = LastDay - FirstDay + 1

This approach only needs a date. It does the rest of the math for you. You need to provide the year, because there are leap years, so February has a different amount of days in different years.

You could also do something like this to get the amount of days in current month
Day(DateSerial(Year(Now), Month(Now) + 1, 1) - 1) - Day(DateSerial(Year(Now), Month(Now), 1)) + 1

None of these is tested.

If you want to get the weekday, use the built in WeekDay function with a dateserial to get whatever day of the month you need.
 
Dear 561414
How to get all the dates of the month after selecting the certain month and weekdays? VBA-Access:

Selecting the weekdays
"Combo box" Select: Weekdays (Eg: Wednesday)

Input the month
"TextBox" (Input Receive Eg : FEB-2023 (MMM-YYYY))

RESULT
Get all the dates : "List Box" Display : Eg : 1,8,15,22
 
1. Find the first Wednesday (for example)
=>
1a) check weekday of 1. of Month ... x = weekday(FirstDayOfMonthDate)​
1b) calc the next Wednesday ... nextWednesday = dateadd(....)​
2. Loop Until end of month (+7)
 
Last edited:
Oh, that is more interesting. You have to account for the way your data is input. There are many things you should take into account here, such as:
How are the week days considered data wise? numbers or strings?
How about months?
Are you going to always type month-year? like this? FEB-2023, or can it be february-2023? or something else?

Only after you know, can you actually build it.

Take a look at the attached
 

Attachments

see this demo (Form1).
also see the tables and queries.
 

Attachments

1. Find the first Wednesday (for example)
=>
1a) check weekday of 1. of Month ... x = weekday(FirstDayOfMonthDate)​
1b) calc the next Wednesday ... nextWednesday = dateadd(....)​
2. Loop Until end of month (+7)
Requesting you to please provide me VBA Code
 

Note: Another simple method would be to use a calendar table. (But then this is no longer pure VBA.)
 
Last edited:

Users who are viewing this thread

Back
Top Bottom