?
Solved

Incrementing dates

Posted on 2005-03-14
3
Medium Priority
?
191 Views
Last Modified: 2010-05-02
I'm just trying to create a loop that will increment a date by a day each time:

TestDate = "3/27/2005"
For x = 1 To 7
    Debug.Print TestDate + Day(x)
Next x

Seems pretty simple... Here's the results:

4/27/2005
3/28/2005
3/29/2005
3/30/2005
3/31/2005
4/1/2005
4/2/2005

See the first date?  What's up with that?  Why would +day(1) increment the MONTH?  Why does the month then decrease by one?  I don't get this...

So I try this:

TestDate = "3/27/2005"
For x = 1 To 7
    TestDate = TestDate + Day(x)
    Debug.Print TestDate
Next x

Results:

4/27/2005
4/28/2005
4/30/2005
5/3/2005
5/7/2005
5/12/2005
5/18/2005

???

Why does this make no sense whatsoever?
Thanks!
0
Comment
Question by:MIKEV
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:MIKEV
ID: 13536219
Oh, I can see my error in the second example above, it's adding 1 days, then 2 days, etc.  This is better:

TestDate = "3/27/2005"
For x = 1 To 7
    TestDate = TestDate + Day(1)
    Debug.Print TestDate
Next x

4/27/2005
5/28/2005
6/28/2005
7/29/2005
8/29/2005
9/29/2005
10/30/2005

Now it's inreasing the month?
0
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 800 total points
ID: 13536404
try:

TestDate = CDate("3/27/2005")
    For x = 1 To 7
        TestDate = TestDate + 1
        Debug.Print TestDate
    Next x
0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 13536430
or this if you using DateSerial, DateAdd functions:

TestDate = DateSerial(2005, 3, 27)
    For x = 1 To 7
        TestDate = DateAdd("d", 1, TestDate)
        Debug.Print TestDate
    Next x
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question