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

Why Subscript out of range error

Status
Not open for further replies.

rdeleon

IS-IT--Management
Jun 11, 2002
114
US
I am getting the "subscript out of range error" in the following code. Can someone tell me what I am doing wrong!
Code:
    Dim sfile As String
    Dim strFileNames() As String
    Dim strRowSource As String
    Dim intI As Integer
        
    sfile = Dir("C:\Temp\*.jpg", vbNormal)
    
    intI = 0
    Do While sfile & &quot;&quot; <> &quot;&quot;
        sfile = Dir()
        strFileNames(intI) = sfile
*** error ocurrs here
Code:
        intI = intI + 1
    Loop

Any help is appreciated.
 
Hi!

You set up your array as a dynamic array so you need to initialize it and then redim it each time through the loop, like this:

Dim sfile As String
Dim strFileNames() As String
Dim strRowSource As String
Dim intI As Integer

sfile = Dir(&quot;C:\Temp\*.jpg&quot;, vbNormal)

intI = 0
Do While sfile & &quot;&quot; <> &quot;&quot;
ReDim Preserve strFileNames(0 To intI) As String
sfile = Dir()
strFileNames(intI) = sfile *** error ocurrs here
intI = intI + 1
Loop

This will give you an array that has as many subscripts as necessary.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Does this help ?

intI = 1
ReDim strFileNames(intI)

Do While sfile & &quot;&quot; <> &quot;&quot;
sfile = Dir()
strFileNames(intI) = sfile '*** error ocurrs here
intI = intI + 1
ReDim Preserve strFileNames(intI)
Loop
 
Thanks to both respondents. I appreciate it very much.

Rene'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top