Home All Groups Group Topic Archive Search About

Newbie: FOR loop on dates

Author
19 Sep 2005 7:24 PM
Newbie
Hello, i was wondering if there is a way to loop through dates.
e.g.. I want to loop through all dates between March 5th,1990  to April
20th,1996.

I've tried a couple of things with no success and Google gives nothing.

Thanx in advance!
-Steve

Author
19 Sep 2005 7:35 PM
Ken Halter
"Newbie" <st***@here.com> wrote in message news:CfEXe.797$hW.529@tor-nn1...
> Hello, i was wondering if there is a way to loop through dates.
> e.g.. I want to loop through all dates between March 5th,1990  to April
> 20th,1996.
>
> I've tried a couple of things with no success and Google gives nothing.
>
> Thanx in advance!
> -Steve

If you're looking for days, this'll add all of those dates to a combobox.
'========
Private Sub Command1_Click()
   Dim dt1 As Date
   Dim dt2 As Date
   Dim dtTmp As Date

   dt1 = #3/5/1990#
   dt2 = #4/20/1996#

   Combo1.Clear

   For dtTmp = dt1 To dt2
      Combo1.AddItem Format$(dtTmp, "General Date")
   Next
End Sub
'========

You can do it by month/second/etc too.... easiest with a Do/Loop
'========
Private Sub Command1_Click()
   'Months - Change the "m" to "s" for seconds, or "d" for days
   Const DT_INTERVAL = "m"
   Dim dt1 As Date
   Dim dt2 As Date
   Dim dtTmp As Date

   dt1 = #3/5/1990#
   dt2 = #4/20/1996#

   Combo1.Clear

   dtTmp = dt1

   Do While DateDiff(DT_INTERVAL, dtTmp, dt2) >= 0
      Combo1.AddItem Format$(dtTmp, "General Date")
      dtTmp = DateAdd(DT_INTERVAL, 1, dtTmp)
   Loop

End Sub
'========

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
19 Sep 2005 7:39 PM
Bob Butler
"Newbie" <st***@here.com> wrote in message news:CfEXe.797$hW.529@tor-nn1
> Hello, i was wondering if there is a way to loop through dates.
> e.g.. I want to loop through all dates between March 5th,1990  to
> April 20th,1996.
>
> I've tried a couple of things with no success and Google gives
> nothing.

Offhand, you can use the fact that VB stores dates as Double internally

Dim n As Double
Dim d As Date
For n = CDbl(#3/5/1990#) To CDbl(#5/1/1990#)
  d = CDate(n)
  Debug.Print d
Next

or you can work with the number of days to be processed

Dim n As Long
Dim k As Long
Dim d As Date
n = DateDiff("d", #3/1/1990#, #5/1/1990#)
For k = 0 To n
  d = DateAdd("d", k, #3/1/1990#)
  Debug.Print d
Next

and there are probably other options

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
19 Sep 2005 7:47 PM
Mike Williams
"Newbie" <st***@here.com> wrote in message news:CfEXe.797$hW.529@tor-nn1...

> Hello, i was wondering if there is a way to loop through
> dates. e.g.. I want to loop through all dates between
> March 5th,1990  to April 20th,1996.

Fortunately Visual Basic is very forgiving on all sorts of stuff. Is this
what you want? Paste it into a button click event or something on a Form
that contains a ListBox:

Dim d1 As Date, d2 As Date, n As Date
d1 = "March 5 1990"
d2 = "April 20 1996"
For n = d1 To d2 Step 1
  List1.AddItem n
Next n

Mike
Author
19 Sep 2005 11:54 PM
Someone
> d1 = "March 5 1990"
> d2 = "April 20 1996"

I think VB uses CDate implicitly in this case, so it may generate an error
in other countries, I don't know. Imagine someone using:

d1 = "5/13/2005" ' May 13 2005

This would generate a runtime error in non-US OS versions. Try "13/5/2005"
in your computer and see what happens.

Date constants in VB6 are used with ##, for example:

d1 = #5/13/05#
d1 = #5/13/05 5:00:00 PM#





Show quoteHide quote
"Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
news:dgn4ki$7q7$1@news8.svr.pol.co.uk...
> "Newbie" <st***@here.com> wrote in message
> news:CfEXe.797$hW.529@tor-nn1...
>
>> Hello, i was wondering if there is a way to loop through
>> dates. e.g.. I want to loop through all dates between
>> March 5th,1990  to April 20th,1996.
>
> Fortunately Visual Basic is very forgiving on all sorts of stuff. Is this
> what you want? Paste it into a button click event or something on a Form
> that contains a ListBox:
>
> Dim d1 As Date, d2 As Date, n As Date
> d1 = "March 5 1990"
> d2 = "April 20 1996"
> For n = d1 To d2 Step 1
>  List1.AddItem n
> Next n
>
> Mike
>
>
>
Author
20 Sep 2005 12:20 AM
Someone
I just tested what I said, apparently if VB sees a number more than 12, it
considers it a month. The default maybe a US date, but it's not clear. I
would rather recommend and use ##. Sample code:

Private Sub Form_Load()
    Dim d As Date

    d = "5/13/2005"
    Debug.Print d
    d = "13/5/2005"
    Debug.Print d
End Sub

This prints:

5/13/2005
5/13/2005

I am using a US date in the Control Panel. Apparently this could work, but
it's not good if you have team members outside the US, or if you post sample
code on the web.

"Someone" <nob***@cox.net> wrote in message
news:IcIXe.43359$ct5.16749@fed1read04...
Show quoteHide quote
>> d1 = "March 5 1990"
>> d2 = "April 20 1996"
>
> I think VB uses CDate implicitly in this case, so it may generate an error
> in other countries, I don't know. Imagine someone using:
>
> d1 = "5/13/2005" ' May 13 2005
>
> This would generate a runtime error in non-US OS versions. Try "13/5/2005"
> in your computer and see what happens.
>
> Date constants in VB6 are used with ##, for example:
>
> d1 = #5/13/05#
> d1 = #5/13/05 5:00:00 PM#
>
>
>
>
>
> "Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
> news:dgn4ki$7q7$1@news8.svr.pol.co.uk...
>> "Newbie" <st***@here.com> wrote in message
>> news:CfEXe.797$hW.529@tor-nn1...
>>
>>> Hello, i was wondering if there is a way to loop through
>>> dates. e.g.. I want to loop through all dates between
>>> March 5th,1990  to April 20th,1996.
>>
>> Fortunately Visual Basic is very forgiving on all sorts of stuff. Is this
>> what you want? Paste it into a button click event or something on a Form
>> that contains a ListBox:
>>
>> Dim d1 As Date, d2 As Date, n As Date
>> d1 = "March 5 1990"
>> d2 = "April 20 1996"
>> For n = d1 To d2 Step 1
>>  List1.AddItem n
>> Next n
>>
>> Mike
>>
>>
>>
>
>
Author
20 Sep 2005 12:50 AM
Bob Butler
"Someone" <nob***@cox.net> wrote in message
news:RAIXe.43387$ct5.8315@fed1read04
Show quoteHide quote
> I just tested what I said, apparently if VB sees a number more than
> 12, it considers it a month. The default maybe a US date, but it's
> not clear. I would rather recommend and use ##. Sample code:
>
> Private Sub Form_Load()
>     Dim d As Date
>
>     d = "5/13/2005"
>     Debug.Print d
>     d = "13/5/2005"
>     Debug.Print d
> End Sub
>
> This prints:
>
> 5/13/2005
> 5/13/2005
>
> I am using a US date in the Control Panel. Apparently this could
> work, but it's not good if you have team members outside the US, or
> if you post sample code on the web.

Correct; VB uses the local settings as a guide when the string to convert is
ambiguous.  "5/13/2005" will be handled consistently because only "m/d/y"
makes sense.  If you used
  d="5/1/2005"
then it may be Jan 5 or May 1 depending on where the code runs.  Use #m/d/y#
format for constants or use DateSerial to build a date to avoid potential
problems.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."