Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate the First date of the previous month & Last date of previous month. 2

Status
Not open for further replies.

Moss100

Technical User
Aug 10, 2004
586
GB
Hello I have a form which has a textbox for a start date: txtDateStart and an end date: txtDateEnd

I have been able to set the default values as follows:

txtDateStart = DateSerial(Year([Date]),Month([Date])-1,1)

txtDateEnd = DateSerial(Year(Date()),Month(Date())+1,0)

This works fine in that it gives me the start and end dates of the CURRENT month.

Can someone help me tweak the code so I am able to set it to display the start and end dates of the PREVIOUS month.

Many thanks Mark
 
Hi,

Previous month..
Code:
txtDateStart = DateSerial(Year([Date]),Month([Date])-1-1,1)

txtDateEnd = DateSerial(Year(Date()),Month(Date())+1-1,0)

I just produced the code for current month and subtracted 1 from the month parameter of DateSerial.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
This works fine in that it gives me the start and end dates of the CURRENT month." - not really.

Your code gives you the Start date of the Previous month (December) and the End of the Current month (January):

Code:
Debug.Print "Start " & DateSerial(Year(Date), Month(Date) - 1, 1)
Start 12/1/2017

Debug.Print "End   " & DateSerial(Year(Date), Month(Date) + 1, 0)
End   1/31/2018

So you are either half wrong, or half way to your solution.... :)


---- Andy

There is a great need for a sarcasm font.
 
Andy, I du
I didn't think to check the validity of the start and end.

Whatever current month formula, just subtract 1 from the month parameter in DateSerial.
Code:
CurStart: DateSerial(Year(Date), Month(Date), 1)
CurEnd: DateSerial(Year(Date), Month(Date) +1, 0)

PrvStart: DateSerial(Year(Date), Month(Date)-1, 1)
PrvEnd: DateSerial(Year(Date), Month(Date) +1-1, 0)


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Back on track. Thank you very much. Mark 🤗
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top