Free Lessons
Fast Tips
Topic Index
Home   Courses   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
Home > Forums > Access
Access Forum

Back to Access Forum

DateAdd returns bogus value Upload Images   Link  
Gary Becker 
3 months ago
I have a simple loop. I want to add 1 month to a date and each time it runs through the loop. Here is my code:
Private Sub MonthAddBtn_Click()

Dim CurMonth As Date
Dim X As Long

    StatusBox = ""
    CurMonth = #1/1/2023#
    X = X + 1
    Status "Current Month is " & CurMonth
    CurMonth = CurMonth + DateAdd("m", 1, CurMonth)
    Loop Until X = 5
End Sub

And it returns funny values.
Gary Becker
3 months ago

Adam Schwanz
3 months ago
It can be helpful to write out what you are actually telling the code to do instead of just using variables sometimes.

This line here
CurMonth = CurMonth + DateAdd("m", 1, CurMonth)

CurMonth is 1/1/2023 so we have
1/1/2023 + DateAdd("m", 1, 1/1/2023)
which turns into
1/1/2023 + 2/1/2023

See the problem? ;).

In this case just do CurMonth = DateAdd("m",1,CurMonth)
Brent Davis
3 months ago
I have watched the missing months video a million times and i cannot figure out how to get my status box to show only the end dates for each month. I am trying to do this with VBA:

Private Sub MonthListBtn_Click()

    Dim StartDate As Date, X As Long
    StartDate = DateSerial(Year(DATE), Month(DATE), 0)
    StatusBox = ""
    Status "Current Month is " & StartDate
    StartDate = DateAdd("m", 1, StartDate)
    X = X + 1
    Loop Until X = 12

End Sub

It turns out like this:

Current Month is 7/29/2024
Current Month is 6/29/2024
Current Month is 5/29/2024
Current Month is 4/29/2024
Current Month is 3/29/2024
Current Month is 2/29/2024
Current Month is 1/30/2024
Current Month is 12/30/2023
Current Month is 11/30/2023
Current Month is 10/30/2023
Current Month is 9/30/2023
Current Month is 8/31/2023

I want each date to be the last day of each month; 1-31-2023, 2-28-2023, 3-31-2023, etc.

Any assistance is appreciated. Thanks.
Kevin Robertson
3 months ago
Try this:

Private Sub MonthListBtn_Click()

    Dim X As Long

    StatusBox = ""
    For X = 1 To 12
        Status "Current Month is: " & DateSerial(Year(Date), Month(Date) + X, 0)

End Sub
Gary Becker
3 months ago
Richard's EOMonth TechHelp video from about 10 days ago might also give you some ideas.
Kevin Robertson
3 months ago
Here's a link to the video Gary mentioned: Access EOMONTH
Brent Davis
3 months ago
Gentlemen, thanks for all the assistance!  You guys pointed me in the right direction and after a couple of hours working through it, I  was able to get it closer to what I Invision. A couple more steps and it should be complete. Having to work through it and finally seeing the outcome you Invision is really cool!!! Thanks

This thread is now closed. If you wish to comment, start a NEW discussion, below.

Back to Access Forum Comments

Start a NEW Conversation
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
Subscribe to Access Forum
Get notifications when this page is updated

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.

Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
My Account
My Courses
Lost Password
Student Databases
Change Email
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Customer Support
Web Site Tour
Consulting Services
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Video Tutorials
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Terms of Sale
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

Copyright 2023 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 12/1/2023 5:56:27 AM.