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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create an array of dates problem, subscript out of range - run time 9 2

Status
Not open for further replies.

knifey

Technical User
Nov 14, 2006
180
GB
Hi,
I'm trying to create an array of dates. But am getting a run time error 9, subscript out of range error on the arrDates(i1) = iDate line. It looks like this should be an easy fix, but I can't find the cause of the problem (don't have the brainpower).
Can anyone advise where I may be going wrong?
Thanks,
K
Code:
startDateVar = Forms![fdlg_Event/Task]![StartDate].Value
endDateVar = Forms![fdlg_Event/Task]![EndDate].Value
daysBetweenDates = endDateVar - startDateVar
'create an array of dates
iDate = startDateVar
i1 = 0
For i1 = 0 To daysBetweenDates - 1
    arrDates(i1) = iDate
    iDate = DateAdd("d", 1, iDate)
Next
 
Where is defined arrDates ?
What about this ?
Code:
startDateVar = Forms![fdlg_Event/Task]![StartDate].Value
endDateVar = Forms![fdlg_Event/Task]![EndDate].Value
daysBetweenDates = endDateVar - startDateVar
'create an array of dates
Dim arrDates() As Date
ReDim arrDates(daysBetweenDates)
For i1 = 0 To daysBetweenDates - 1
    arrDates(i1) = startDateVar + i1
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
since you don't know how big the array needs to be you need to redim it as PHV has suggested otherwise you get an error when the array exceeds the originally dimmed size

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
How are ya knifey . . .

[blue]Redim[/blue] is one method to perform this. However it can cause a performance hit for large day counts (the array is re-dimensioned on each iration of the loop. The following methods requires no [blue]ReDim[/blue] ... give it a shot:
Code:
[blue]   Dim begDate As Date, endDate As Date, cnt As Long
   Dim Pack, Ary, x As Integer
   
   begDate = Forms![fdlg_Event/Task]![StartDate]
   endDate = Forms![fdlg_Event/Task]![endDate]
   cnt = DateDiff("d", begDate, endDate) + 1
   
   [green]'Concatenate the dates into string Pack (with delimiter)[/green]
   For x = 1 To cnt
      If Not IsEmpty(Pack) Then
         Pack = Pack & ";" & begDate
      Else
         Pack = begDate
      End If
      
      begDate = begDate + 1
   Next
   
   [green]'Split the string into an array[/green]
   Ary = Split(Pack, ";")
   
   [green]'Read the array[/green]
   For x = LBound(Ary) To UBound(Ary)
      Debug.Print Ary(x)
   Next[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks PHV/Aceman,
Both solutions work great but I've gone with Aceman's version in case I'm faced with a huge date range (e.g. maternity leave, etc.)
Cheers,
K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top