Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Intriguing problem with Variables, Dates, etc 4

Status
Not open for further replies.

jasonatsmtc

Technical User
Joined
Dec 1, 2002
Messages
6
Location
US
Hello,

I am trying to create a function that will take two inputted dates, from 2 MS Date Time Pickers, dtArrival and dtDepart, and create veriables for those dates and every date in between. Here is the code that i am trying to use.

[vb]
Public varDate() As Date
Public varIndex As Integer

Public Sub InitDates()
varIndex = 1
ReDim varDate(0) As Date
varDate(0) = frmMakeReso.dtArrival.Value
Do Until varDate(varIndex) = frmMakeReso.dtDepart.Value
varIndex = varIndex + 1
ReDim varDate(varIndex) As Date
varDate(varIndex) = DateAdd("d", 1, varDate(varIndex - 1))
Loop
End Sub
[/vb]
This gives me run-time error 9, "Subscript out of range"
Please let me know if you see anything i can change in this code to make it work, or if you know a better way to go about it.

Thanks in advance!
Jason C.
 
Hi Jason:

I see a few problems in your code.

1. The first Redim statement should be "Redim varDate(x)" where x is some integer value indicating the appropriate size.

2. The "varDate(0) = . . ." will give a runtime error 9 in your existing code since the existing Redim statement sizes the array to no elements. Thus, there is no zero element.

3. The "Do Until varDate(varIndex) = . . . " will also cause error 9 since on the first time it is executed varDate has no elements.

4. The Redim statement in the loop should not be there.

I suggest something like the following:

Code:
Public varDate() As Date
Public varIndex As Integer

Public Sub InitDates()

ReDim varDate(0 To (frmMakeReso.dtDepart.Value - frmMakeReso.dtArrival.Value))

varIndex = 1
varDate(0) = frmMakeReso.dtArrival.Value
Do Until varDate(varIndex) = frmMakeReso.dtDepart.Value
    varDate(varIndex) = DateAdd("d", 1, varDate(varIndex - 1))
    varIndex = varIndex + 1
Loop
End Sub

I have not tried this code, but hope it helps you find the answer.

Cassie
 
When i try your code i still get the run-time error 9 on the following line:

Do Until varDate(varIndex) = frmMakeReso.dtDepart.Value

Any other ideas would be awesome!
Jason
 
It may be easier if you use a For..Next loop and look at DateDiff function!
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Try this....
'========================================================
Public Sub InitDates()
Dim dateStart As Date
Dim dateEnd As Date
Dim DatesInBetween() As Date


dateStart = frmMakeReso.dtArrival.Value
dateEnd = frmMakeReso.dtDepart.Value

ReDim DatesInBetween(DateDiff("d", dateStart, dateEnd))

For i = 0 To DateDiff("d", dateStart, dateEnd)
DatesInBetween(i) = DateAdd("d", i, dateStart)
Next
End Sub
'======================================================== All the Best
Praveen Menon
pcmin@rediffmail.com
 
Thanks a lot everyone!
I can now move on... :)
I have been stuck on this detail for days!!

Jason
 
Hi Jasonat..
I tried Ur problem and found the solution as below.When U declare dimension of array as 0 actually U r starting first element at 0 index. if U want to save the contents of the array then U have to use keyword preserve.

[vb]
Public varDate() As Date
Public varIndex As Integer

Public Sub InitDates()
varIndex = 0
ReDim varDate(0) As Date
varDate(0) = frmMakeReso.dtArrival.Value
Do Until varDate(varIndex) = frmMakeReso.dtDepart.Value
varIndex = varIndex + 1
ReDim preserve varDate(varIndex) As Date
varDate(varIndex) = DateAdd("d", 1, varDate(varIndex - 1))
Loop
End Sub
[/vb]

All the best
Abdul Rafi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top