Dynamic Arrays and Subscript Out Of Range

vShaneCurtis

New member
Local time
Today, 11:52
Joined
Apr 17, 2017
Messages
9
Hi Guys,

I'm having a bit of a problem and could use some help. I am using an empty array in conjunction with a for each loop to store files names in a directory. The names in the array will then be used for further processing. As expected I am getting error 9 subscript out of range. I know it is possible to trap this error and correct it. I had a function to handle it but I have lost the code because my database was corrupted and I am unable to open it. Does anyone have a code snippet that will solve this problem. Any help you can provide is appreciated.
 
In my experience, error 9 occurs when array variables are not defined.
 
Thanks for the reply. When a variable is not defined Access does not give you an error number. It simply tells you that it's not defined. I get error 9 subscript out of range. In this case Access shows the error number and the message. I have temporarily fixed the problem with a redim FileNames(0) just before entering the for each loop and then a Redim Preserve FileNames(UBound(FileNames) + 1) after I add the file name to the array. I would like to use a cleaner method if possible
 
Shane, best thing if you need help with code is post the code. If there is an error, indicate the line. Without seeing the code we are just guessing.
 
Mark,

Thanks for the reply. I understand what you are saying, but in this case it will not solve anything as the code I am using is a simple stop gap measure. I had found a function that works but lost it when my DB got corrupted. I am looking for a code snippet that will basically do what I had before. The code checked for error 9 and redim the array to 0 otherwise if there was no error it added one element to the array. I found the function on the internet and can't seem to locate the page again.
 
To repeat my previous post, I got error 9 with array code only yesterday after losing code from a program crash. The cause was a variable that hadn't been identified within the array code. Once I 'defined' it in that context, the error was fixed & the code worked again

So I suggest you do that check & if its not obvious then post the code as MarkK suggested
 
Not sure how to interpret your last post!
All I can say is I tried to help....
 
The problem with my code is not because of an undeclared variable, it is because the array is unallocated and I'm attempting to add an element to the array which you can't do. I was hoping that someone here had a code snippet that could determine if an array was unallocated. Instead I got posts telling me to check for every other thing. You can't have an undeclared variable and expect the code to execute with option explicit turned on, Access catches the error at compile time. So as I said before it's NOT and undeclared variable.

Cheers
 
Everybody's doing the best they know how... :)
 
Minty,
Your the only Padawan I see. I asked if anyone had a code snippet that handles an unallocated array subscript our of range error. Instead you blocks want me to check variables and every other thing. I know what the problem with the code is and I asked for assistance in solving that particular issue. Go play with your light sabre
 
If you post the code you are having trouble with, it provides the best description of what you are trying to do. Then people might see a problem, or suggest a work-around.

This is your thread. The less information we can see and understand, the less help we can be. If we don't understand, you can blame us, but it doesn't further anything.

Cheers,
 
I don't think you can check if an array has been given an element.
I wrote this earlier for another post, if it helps...

Code:
Private Function addtoarr(ByRef ary() As Variant, v As Variant) As Boolean
On Error Resume Next
    Dim l As Long
    l = UBound(ary)
    Select Case Err.Number
    Case 0
        ReDim Preserve ary(l + 1)
    Case Else
        ReDim ary(l)
    End Select
    ary(UBound(ary)) = v
End Function

You probably want to check the actual error numbers...
 
I suspect they have, but with only 7 posts the great unwashed helpers on here would possibly like to see your code to ensure you aren't making some possible beginner error.
Until your additional posts it was unclear of your level of experience.

Your netiquette levels are obvious though.
 
ReDim Preserve

Also, it's nice not being the most obnoxious person in a thread for once. :D
 
I stated my issue VERY clearly from the start Gents. I needed a code snippet/function to deal with a subscript out of range error caused by an unallocated array. I don't know how I could be more clear. I am not a newbie I have three college degrees in the field and I've been doing this stuff for 30 years Just having a bit of a brain cramp right now, So sorry for my blunt manner but I don't deal well with people who don't address the question I asked but instead want to try and tell me what they think the problem is.
 
I posted a code snippet/function. Maybe try reading your own thread or provide feedback to the people trying to help you?

Also somebody as knowledgeable as yourself should know one of the most widely used methods of getting files from a folder returns an array. Maybe that's adding to the confusion.
 
My final contribution to this thread

Perhaps with your vast experience, you could give up some of your own time for free to try & assist other users of this forum. Much better than alienating others who have offered suggestions even if they aren't correct for your situation
 
When I come into a forum looking for help I also spend some time looking around at other people's posts and offer assistance where I can.
 

Users who are viewing this thread

Back
Top Bottom