By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

Date Funtions for Previous & Next Buttons On Form

P: 1
I have the start and end dates on my report forms set at the first and last days of the current month.
I also have three buttons for these dates:
One that advances forward one month at a time
One that goes back one month at a time
One that returns to the current month (same code as I use in "OnLoad")
Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandBack_Click()
  2. Me.DateStart = DateSerial(Year(Me.DateStart), Month(Me.DateStart) - 1, 1)
  3. Me.DateEnd = DateAdd("m", 1, (DateAdd("d", -1, DateStart)))
  4. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandCurrent_Click()
  2. Me.DateStart = DatePart("m", Now()) & "/01/" & DatePart("yyyy", Now())
  3. Me.DateEnd = DateSerial(Year(Date), Month(Date) + 1, 1) - 1
  4. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandForward_Click()
  2. Me.DateStart = DateSerial(Year(Me.DateStart), Month(Me.DateStart) + 1, 1)
  3. Me.DateEnd = DateSerial(Year(DateStart), Month(DateStart) + 1, 1) - 1
  4. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Me.DateStart = DatePart("m", Now()) & "/01/" & DatePart("yyyy", Now())
  3. Me.DateEnd = DateSerial(Year(Date), Month(Date) + 1, 1) - 1
  4. End Sub
3 Weeks Ago #1
Share this Question
Share on Google+
3 Replies


twinnyfo
Expert Mod 2.5K+
P: 3,313
JEBritt,

Welcome to Bytes!

Is this a question or an attempt at an informative article?
3 Weeks Ago #2

NeoPa
Expert Mod 15k+
P: 31,561
Indeed. It's not clear where you're going with this thread.
3 Weeks Ago #3

P: 30
By all means, avoid string handling of date and time.
Thus, your code can be reduced to:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandBack_Click()
  2.     Me.DateStart = DateSerial(Year(Me.DateStart), Month(Me.DateStart) - 1, 1)
  3.     Me.DateEnd = DateSerial(Year(Me.DateStart), Month(Me.DateStart), 0)
  4. End Sub
  5.  
  6. Private Sub CommandCurrent_Click()
  7.     Me.DateStart = DateSerial(Year(Date), Month(Date), 1)
  8.     Me.DateEnd = DateSerial(Year(Date), Month(Date) + 1, 0)
  9. End Sub
  10.  
  11. Private Sub CommandForward_Click()
  12.     Me.DateStart = DateSerial(Year(Me.DateStart), Month(Me.DateStart) + 1, 1)
  13.     Me.DateEnd = DateSerial(Year(DateStart), Month(DateStart) + 2, 0)
  14. End Sub
  15.  
  16. Private Sub Form_Load()
  17.     Me.DateStart = DateSerial(Year(Date), Month(Date), 1)
  18.     Me.DateEnd = DateSerial(Year(Date), Month(Date) + 1, 0)
  19. End Sub
3 Weeks Ago #4

Post your reply

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