how to redimension array

Posted on 2009-02-24
Last Modified: 2013-11-28
I have the following:

ReDim Preserve jobCodes(0 To jbMax) As String

and when I compile I get the compile error: Array Already Dimensioned

The message is true: my array *was* previously dimensioned. This is my frist shot at using dynamic arrays, so I'm sure I'm doing something wrong, but I thought one was supposed to use the "Preserve" when the arrary was previously dimensioned. What am I doing wrong?
Question by:jmarkfoley
    LVL 84
    How was your array dimensioned originally? Was it also an array of Strings?
    LVL 58

    Accepted Solution

    The initial declaration must be made without dimensions. You use 'Preserve' to enlarge a dynamic array which has already been dimensioned once dynamically, if you want to keep the values already entered.

        Dim jobCodes() As String   ' initial declaration for dynamic arrays
        ReDim jobCodes(0 to 3)   ' run-time dimensioning of array
        jobCodes(2) = "foobar"   ' using  the array
        ReDim Preserve jobCodes(0 To jbMax)   ' preserving the first 4

    Open in new window

    LVL 28

    Assisted Solution

    The Preserve keyword is used when you want to "Preserve" the values already assigned to your Array elements and only works if you are increasing the elements.

    You do not need to DIM an array, the ReDim does the job for you.

    The following example will print A to F in the Immediate window, if you take out the PRESERVE then you will get 5 blank lines followed by E and F.

    Are you changing the defined type, say to string from variant?

    Finally, what version of Access are you using my tests were in Access 2007 but I am fairly sure the previous versions were the same.

    Cheers, Andrew

    Dim cnt As Long
    ReDim arya(0 To 4) As String
    Dim jbmax As Long
        For cnt = 0 To 4
            arya(cnt) = Chr(65 + cnt)
        Next cnt
        jbmax = 6
        ReDim Preserve arya(0 To jbmax) As String
        For cnt = 5 To jbmax
            arya(cnt) = Chr(65 + cnt)
        Next cnt
        For cnt = 0 To jbmax
            Debug.Print arya(cnt)
        Next cnt

    Open in new window

    LVL 58

    Expert Comment

    > You do not need to DIM an array

    That is true. Option Explicit will not catch the problem. Formally (from the VB help file), you should not use it that way:

    The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts).

    The problem becomes apparent in the snippet. The typo isn't identified, and the code breaks only at run-time. I don't know why the VB compiler was made to imply Dim when reading ReDim, these are quite different operations, and no other language makes that particular confusion. Anyway, you are right:

        ReDim Something(12) As String

    Is interpreted as

        Dim Something() As String
        ReDim Something(12)

    Stupid, but there you go...

    Sub Manipulate(pvarMyArray() As Variant)
        ReDim pvarMyAray(12)
        pvarMyArray(12) = "something"
    End Sub
    Sub ShowProblem()
        Dim Values() As Variant
        Manipulate Values
    End Sub

    Open in new window

    LVL 28

    Expert Comment

    harfang, I always use Option Explicit and would always recoment it's use for trapping the logic error that are caused by a stupid typo. Everyone should have this option turned ON in your VBA Options (goodnes knows why the default is OFF)
    Cheers, Andrew
    LVL 1

    Author Closing Comment

    didn't implement any solutions

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    Access - LDB lock file 4 22
    SQL statement for latest record 15 48
    Printing Problem 13 21
    ODBC--call failed error Access 2016/SQL 6 15
    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now