Re: Error on UBound with Dynamic Array



Adding 50 at a time is a reasonable approach. When possible, I try to
determine the number of elements needed prior to any dimensioning. Sometimes
you can't. In this situation, however, I would count the number of delimiter
characters first and that would be the number of elements needed.

"Douglas J. Steele" wrote:

Either that, or you can define a flag that you set to True once it's been
dimensioned, and check that flag:

If booDimensioned Then
ReDim Preserve arrSplit(UBound(arrSplit) + 1)
Else
ReDim arrSplit(0)
booDimensioned = True
End If

BTW, ReDim is an "expensive" operation in terms of resource requirements.
What I typically do is increase the size by, say, 50 elements at a time, and
then do a final ReDim once I know the actual size. Yes, it means you have to
keep track of how many elements you've added, and then ReDim when you've
used up those 50 slots.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Klatuu" <Klatuu@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:49F4CAA9-BDCE-4A77-AD94-A6C902F239F3@xxxxxxxxxxxxxxxx
The reason you are getting this error:
ReDim Preserve arrSplit(UBound(arrSplit) + 1) <-- ERROR occurs
here

is that arrSplit has not yet been dimmed as having any dimension. If you
need to use a dynamic array and may be checking the boundries, then you
can
ReDim varArray(0) at the beginning of the procedure and you will not have
the error problem.

"Jay" wrote:

Talk about taking the wind out of my sails!

I'd still like to know if there's a way to deal with a UBound on a
dynamic
array. But thanks for the clue about Split, it'll save me some time.

Jay

"Brendan Reynolds" wrote:

There are two different implementations of Access 97-compatible Split
functions at the following URL ...

http://groups.google.com/group/microsoft.public.access/browse_frm/thread/9afd2d3659e99192

--
Brendan Reynolds
Access MVP


"Jay" <Jay@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F31635B6-67E1-4E19-AE22-CD61F0C657A5@xxxxxxxxxxxxxxxx
Hello all,

I'm having a problem using UBound with a dynamic array. Basically, I
want
to check the upper bound (size) of an array. But the array has no
dimension,
so I keep getting an error. But I can't find a way to catch the
error,
and
set the dimension of the arry to 1.

The reason I'm trying this is I'm writing a Split function to take a
comma-separated string of values and splitting the values into a
dynamic
array. I'm using Access 97, so I don't have a predefined Split
function
to
use, so I figured this was the easiest way to do it.

*** If anyone can suggest better ways to do it, please let me know.
I'd
love to get some feedback on the function itself, if you're so
inclined.
***

CODE:

Public Function Split(csvString As String) As Variant
Dim arrSplit() As Variant ' array holding string (field) values
Dim locStart As Long ' start location of string
Dim locEnd As Long ' end location of string

' itialize variables
locStart = 1 ' start at beginning of string

Do
' find location of comma
locEnd = InStr(locStart, csvString, ",")

' resize the array to add the next value
ReDim Preserve arrSplit(UBound(arrSplit) + 1) <-- ERROR
occurs
here

' if no comma is found
If locEnd = 0 Then

' extract the remainder of the string
arrSplit(UBound(arrSplit)) = Trim(Mid(csvString,
locStart))

Else

' extract the section of the string between the commas
arrSplit(UBound(arrSplit)) = Trim(Mid(csvString, locStart,
locEnd - locStart))

' set start to next space after comma
locStart = locEnd + 1

End If

' loop while commas delimiters are found in the string
Loop While locEnd <> 0

Split = arrSplit

End Function ' == Split ==


I appreciate any pointers on this one. I might try to replicate the
Split
function (because I like the idea of choosing the delimiter) but
that's
future improvements.

Thanks!
Jay






.



Relevant Pages

  • Re: Help Please - Searching a large text file
    ... an array with about 500 elements to start with, and only Redim it when the ... Dim LensDataArrayAs String, FileNumber As Long ...
    (comp.lang.basic.visual.misc)
  • Re: Help Please - Searching a large text file
    ... Actually you can speed that part of it up about three or four times by writing it in such a way that it does not Redim the array every single time through the loop. ... Dim LensDataArrayAs String, FileNumber As Long ...
    (comp.lang.basic.visual.misc)
  • Re: ReDim an array AFTER its loaded into another array
    ... '.RteInfo2 as string 'etc depending on how many pieces of data you need ... Redim vRteFactsAy ... ' RFA is the route facts array ... Once a "master" array is formed, it looks like the component arrays ...
    (microsoft.public.excel.programming)
  • Re: Error on UBound with Dynamic Array
    ... determine the difference in length of the string. ... Doug Steele, Microsoft Access MVP ... ReDim Preserve arrSplit+ 1) ... need to use a dynamic array and may be checking the boundries, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Error on UBound with Dynamic Array
    ... Hadn't realized ReDim was 'expensive'. ... determine the difference in length of the string. ... need to use a dynamic array and may be checking the boundries, ... If locEnd = 0 Then ...
    (microsoft.public.access.modulesdaovba)