473,467 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Access default date, specifically the first Sunday of september of the current year

1 New Member
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?
Dec 9 '19 #1
1 1472
cactusdata
214 Recognized Expert New Member
That is possible:

Expand|Select|Wrap|Line Numbers
  1. FirstSundaySeptember = DateWeekdayInMonth(DateSerial(Year(Date()), 9, 1), 1, vbSunday)
using this generic function:

Expand|Select|Wrap|Line Numbers
  1. ' Calculates the date of the occurrence of Weekday in the month of DateInMonth.
  2. '
  3. ' If Occurrence is 0 or negative, the first occurrence of Weekday in the month is assumed.
  4. ' If Occurrence is 5 or larger, the last occurrence of Weekday in the month is assumed.
  5. '
  6. ' If Weekday is invalid or not specified, the weekday of DateInMonth is used.
  7. '
  8. ' 2019-12-08. Gustav Brock, Cactus Data ApS, CPH.
  9. '
  10. Public Function DateWeekdayInMonth( _
  11.     ByVal DateInMonth As Date, _
  12.     Optional ByVal Occurrence As Integer, _
  13.     Optional ByVal Weekday As VbDayOfWeek = vbUseSystemDayOfWeek) _
  14.     As Date
  15.  
  16.     Const DaysPerWeek            As Integer = 7
  17.     Const MaxWeekdayCountInMonth As Integer = 5
  18.  
  19.     Dim Offset          As Integer
  20.     Dim Month           As Integer
  21.     Dim Year            As Integer
  22.     Dim ResultDate      As Date
  23.  
  24.     ' Validate Weekday.
  25.     Select Case Weekday
  26.         Case _
  27.             vbMonday, _
  28.             vbTuesday, _
  29.             vbWednesday, _
  30.             vbThursday, _
  31.             vbFriday, _
  32.             vbSaturday, _
  33.             vbSunday
  34.         Case Else
  35.             ' vbUseSystemDayOfWeek, zero, none or invalid value for VbDayOfWeek.
  36.             Weekday = VBA.Weekday(DateInMonth)
  37.     End Select
  38.  
  39.     ' Validate Occurence.
  40.     If Occurrence < 1 Then
  41.         ' Find first occurrence.
  42.         Occurrence = 1
  43.     ElseIf Occurrence > MaxWeekdayCountInMonth Then
  44.         ' Find last occurrence.
  45.         Occurrence = MaxWeekdayCountInMonth
  46.     End If
  47.  
  48.     ' Start date.
  49.     Month = VBA.Month(DateInMonth)
  50.     Year = VBA.Year(DateInMonth)
  51.     ResultDate = DateSerial(Year, Month, 1)
  52.  
  53.     ' Find offset of Weekday from first day of month.
  54.     Offset = DaysPerWeek * (Occurrence - 1) + (Weekday - VBA.Weekday(ResultDate) + DaysPerWeek) Mod DaysPerWeek
  55.     ' Calculate result date.
  56.     ResultDate = DateAdd("d", Offset, ResultDate)
  57.  
  58.     If Occurrence = MaxWeekdayCountInMonth Then
  59.         ' The latest occurrency of Weekday is requested.
  60.         ' Check if there really is a fifth occurrence of Weekday in this month.
  61.         If VBA.Month(ResultDate) <> Month Then
  62.             ' There are only four occurrencies of Weekday in this month.
  63.             ' Return the fourth as the latest.
  64.             ResultDate = DateAdd("d", -DaysPerWeek, ResultDate)
  65.         End If
  66.     End If
  67.  
  68.     DateWeekdayInMonth = ResultDate
  69.  
  70. End Function
Dec 9 '19 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
5
by: Parintas Themis STE Kardias | last post by:
Hi How can i have in a textbox the first sunday of a month
3
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
5
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?
1
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 =...
4
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...
6
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...
5
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...
2
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...
13
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...
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.