Convert weeks to first day of month (1 Viewer)

dark11984

Registered User.
Local time
Today, 15:41
Joined
Mar 3, 2008
Messages
129
Hi I am trying to convert a week which is entered in a text box on a form to the first day of the month in which it falls.

My text box (txtdw) is in the format yyww and I want it to be converted to 01/mm/yyyy.

example: 1401 would convert to 01/01/2014. 1406 would convert to 01/02/2014.

I am pretty close, I can get to the correct month but I can't work out how to change to the first day of the month.

Code:
DateAdd("D", Right(Me.txtDW, 2) * 7, DateSerial("20" & Left(Me.txtDW, 2), 1, 1))
 

Isskint

Slowly Developing
Local time
Today, 06:41
Joined
Apr 25, 2012
Messages
1,302
You pretty much have it:eek:. Just use another DateSerial() with the result you have gained.

Declare a Long variable to hold the result of your DateAdd() function. Declare another Long variable to hold the result of a second DateSerial() along with Year() and Month() EG DateSerial(Hour(),Month(),1)

Code:
Dim lngRawDate, lngTgtDate As Long

lngRawDate = DateAdd("D", Right(Me.txtDW, 2) * 7, DateSerial("20" & Left(Me.txtDW, 2), 1, 1))
lngTgtDate = DateSerial(Year(lngRawDate), Month(lngRawDate), 1)
 

Mihail

Registered User.
Local time
Today, 08:41
Joined
Jan 22, 2011
Messages
2,373
The first day is always 01. Nothing to calculate here.
Is something that I don't understand ?
 

spikepl

Eledittingent Beliped
Local time
Today, 07:41
Joined
Nov 3, 2010
Messages
6,142
If this is something for yourself then it is one thing. If it is for business, then your method has shortcomings. Week 1 of 2014, according to ISO, begins on Dec 30, 2013 so the correct first day is 1 Dec 2013!
 

Users who are viewing this thread

Top Bottom