INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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

RE: Calculate the First date of the previous month & Last date of previous month.

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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Calculate the First date of the previous month & Last date of previous month.

"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.... smile


---- Andy

There is a great need for a sarcasm font.

RE: Calculate the First date of the previous month & Last date of previous month.

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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Calculate the First date of the previous month & Last date of previous month.

(OP)
Back on track. Thank you very much. Mark 🤗

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close