Problem with Array vb6

Dim mTaskArray() Form level variable

 File = Dir$(strPath & Extention)
    Do While Len(File)
         ReDim mTaskArray(iCounter)
        
        mTaskArray(iCounter) = gTasksFolder & File
         Debug.Print "test " & mTaskArray(iCounter) ' typical  C:\Users\Public\Documents\Personal\Tasks\Yr8-3-2016.rtf
         iCounter = iCounter + 1
       
        File = Dir$
    Loop

Open in new window

output by debug:
test C:\Users\Public\Documents\Personal\Tasks\01-03-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\02-20-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\03-10-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\07-26-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\1-3-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\10-14-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\11-18-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\11-29-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\12-25-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\3-10-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\9-25-2015.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr01-10-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr06-23-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr08-03-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr09-09-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr7-4-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr8-22-2016.rtf
test C:\Users\Public\Documents\Personal\Tasks\Yr8-3-2016.rtf

this seems to work correct

but this does not, the array seems to be empty except for the last dimintion

Private Sub Command_Click()
Dim i As Integer
For i = LBound(mTaskArray) To UBound(mTaskArray) '0 to 17
Debug.Print "cmd " & mTaskArray(i)
Next
End Sub

Open in new window


the debug output
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd
cmd C:\Users\Public\Documents\Personal\Tasks\Yr8-3-2016.rtf

What is wrong ?
isnoend2001Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
Remove the ReDim mTaskArray(iCounter) line.
It resets the array so only the last text you put in is left.
If you want to preserve the arrays contents, you would use redim preserve, but in this case you don't want to do that.
isnoend2001Author Commented:
i commented that line then the next line gives "Subscript out range" error
 mTaskArray(iCounter) = gTasksFolder & File 'error here
MacroShadowCommented:
Before line 1 add
Dim mTaskArray() as String

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

isnoend2001Author Commented:
already declared it
Option Explicit
Dim mTaskArray() Form level variable
i need it to have more scope
MacroShadowCommented:
Please provide full code.
isnoend2001Author Commented:
Project is too huge and many ocx's.
This is  all the code that pertains to the array
GrahamSkanRetiredCommented:
Try changing
ReDim mTaskArray(iCounter)

Open in new window

to
ReDim Preserver mTaskArray(iCounter)

Open in new window

GrahamSkanRetiredCommented:
Sorry, that should read
ReDim Preserve mTaskArray(iCounter)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
You can do this instead (untested)
Dim MyCollection As New Collection
File = Dir$(strPath & Extention)
    Do While Len(File)
        MyCollection.Add gTasksFolder & File
        File = Dir$
    Loop

' Then to use
Dim lngIndex as Long
For lngIndex = 1 to MyCollection.Count
    Debug.Print MyCollection(lngIndex)
Next

Open in new window

MacroShadowCommented:
This is  all the code that pertains to the array
Where is iCounter defined? What value does it hold the first time on line mTaskArray(iCounter) = gTasksFolder & File? Does the array have an element equal to iCounter?

Without seeing your code, this is the best I can do. Most likely this is where the issue lies.
isnoend2001Author Commented:
thanks that works
isnoend2001Author Commented:
Where is iCounter defined?
 in the sub that creates the array
Dim iCounter As Integer
using Option Explicit would have caught it
using ReDim Preserve ( mTaskArray(iCounter) fixed the issue
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.