Guys, I need something specific and I just can't make it happen. I've got a date field in my Access form and I want the default value to be the first Sunday of september of the current year. Is this even possible?
1 1472
That is possible: - FirstSundaySeptember = DateWeekdayInMonth(DateSerial(Year(Date()), 9, 1), 1, vbSunday)
using this generic function: - ' Calculates the date of the occurrence of Weekday in the month of DateInMonth.
-
'
-
' If Occurrence is 0 or negative, the first occurrence of Weekday in the month is assumed.
-
' If Occurrence is 5 or larger, the last occurrence of Weekday in the month is assumed.
-
'
-
' If Weekday is invalid or not specified, the weekday of DateInMonth is used.
-
'
-
' 2019-12-08. Gustav Brock, Cactus Data ApS, CPH.
-
'
-
Public Function DateWeekdayInMonth( _
-
ByVal DateInMonth As Date, _
-
Optional ByVal Occurrence As Integer, _
-
Optional ByVal Weekday As VbDayOfWeek = vbUseSystemDayOfWeek) _
-
As Date
-
-
Const DaysPerWeek As Integer = 7
-
Const MaxWeekdayCountInMonth As Integer = 5
-
-
Dim Offset As Integer
-
Dim Month As Integer
-
Dim Year As Integer
-
Dim ResultDate As Date
-
-
' Validate Weekday.
-
Select Case Weekday
-
Case _
-
vbMonday, _
-
vbTuesday, _
-
vbWednesday, _
-
vbThursday, _
-
vbFriday, _
-
vbSaturday, _
-
vbSunday
-
Case Else
-
' vbUseSystemDayOfWeek, zero, none or invalid value for VbDayOfWeek.
-
Weekday = VBA.Weekday(DateInMonth)
-
End Select
-
-
' Validate Occurence.
-
If Occurrence < 1 Then
-
' Find first occurrence.
-
Occurrence = 1
-
ElseIf Occurrence > MaxWeekdayCountInMonth Then
-
' Find last occurrence.
-
Occurrence = MaxWeekdayCountInMonth
-
End If
-
-
' Start date.
-
Month = VBA.Month(DateInMonth)
-
Year = VBA.Year(DateInMonth)
-
ResultDate = DateSerial(Year, Month, 1)
-
-
' Find offset of Weekday from first day of month.
-
Offset = DaysPerWeek * (Occurrence - 1) + (Weekday - VBA.Weekday(ResultDate) + DaysPerWeek) Mod DaysPerWeek
-
' Calculate result date.
-
ResultDate = DateAdd("d", Offset, ResultDate)
-
-
If Occurrence = MaxWeekdayCountInMonth Then
-
' The latest occurrency of Weekday is requested.
-
' Check if there really is a fifth occurrence of Weekday in this month.
-
If VBA.Month(ResultDate) <> Month Then
-
' There are only four occurrencies of Weekday in this month.
-
' Return the fourth as the latest.
-
ResultDate = DateAdd("d", -DaysPerWeek, ResultDate)
-
End If
-
End If
-
-
DateWeekdayInMonth = ResultDate
-
-
End Function
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dave |
last post by:
Hi,
Can anyone tell me a simple way of inserting some sort of code into a
Dreamweaver template (it goes at the bottom of the page in Copyright
info) that will cause the current year to be...
|
by: Parintas Themis STE Kardias |
last post by:
Hi How can i have in a textbox the first sunday of a month
|
by: Dixie |
last post by:
How do I parse out the 20 from the current year (2005). I can get the last
2 numbers on their own, but have been unable to get the first two on their
own.
TIA
dixie
|
by: Jimmy |
last post by:
How do I set the default value of a text box (date/time) to 1/1/Current
Year... i.e 1/1/2006 this year, 1/1/2007 next year?
|
by: Trinopus |
last post by:
I can set a date to a given fixed value e.g. date1 = #5/31/1993#
But how do I set a date to, say, January 1 of the current year, whatever the year happens to be? Obviously, date1 =...
|
by: john |
last post by:
I have an unbound field in which I would like to show january 1 of the
current year like this 01-01-2007. I tried this code but it doesn't work:
=dateserial(Year(date()),1,1)
Can someone help me...
|
by: prakashwadhwani |
last post by:
I'd like the default date in a text box to be 31(dd) + 12(mm) +
2006(current year-1).
The YEAR part of the date should sense that the current year is 2007
and so deduct 1 & set the year as 2006...
|
by: tbeers |
last post by:
Good morning.
I am running a timecard program in Access 2007 and it seems that the users have trouble entering the correct year when entering the date in the date field. Date field entry format...
|
by: SANDEEP PREMCHANDANI |
last post by:
How do we add 15 days to the current Date which will not cross the month
and resultant Date will never be Sunday? If it is Sunday make it Saturday?
Example > If Current Date is 17/08/2010...
|
by: dfrost |
last post by:
In MS Access 2007, I am having difficulty with the syntax for a field validation using a date range for current year.
For example, >= #1/1/2013# And <#1/1/2014# will prevent users from entering a...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| | |