Declaring a variable size array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frankleggett
    New Member
    • Oct 2006
    • 22

    #1

    Declaring a variable size array

    Hi

    Please excuse my ignorance. I am trying to declare an array with a variable amount but I'm having no luck.
    this is what I've tried

    Dim Number as string
    X = Val(Number)
    dim Array(X) as string

    I've also tried

    dim Array(val(Numbe r)) as string

    but with both I get a compile error "constant expression required"

    Can someone help me. I have tried looking up all my reference books and on the Internet.

    Frank
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by frankleggett
    Please excuse my ignorance.
    If it weren't for ignorance, we'd have nothing to do here, would we? :)

    Originally posted by frankleggett
    I am trying to declare an array with a variable amount but I'm having no luck. this is what I've tried
    Code:
    Dim Number as string
    X = Val(Number)
    dim Array(X) as string
    I've also tried
    Code:
    dim Array(val(Number)) as string
    but with both I get a compile error "constant expression required"

    Can someone help me. I have tried looking up all my reference books and on the Internet.
    For starters, I think it depends on where you define the array. If you are defining it inside a Sub or Function, then I think (not completely certain) that it has to be a specific size. For a variable-sized array, try defining it at the module level (as Public if desired).

    Also, I think (once again, not 100% certain) that you have to define it without any size, then use the ReDim statement in your code to set the size. For example:
    Code:
    ' At the module level...
    Private MyArray() As String
    
    ' In a Sub or Function...
    ReDim MyArray(1 To X)
    One important thing to remember - you can change the size of the array later by using ReDim again, but by default it will wipe out all values in the array at the time. Use Redim Preserve to keep existing contents while changing the size.

    Comment

    • frankleggett
      New Member
      • Oct 2006
      • 22

      #3
      Thanks for your reply I will try it in a module. I have tried to set up variables in a module before but every time I've tried when the variable is called the error comes back variable unknown this maybe because I'm not tying the module to the form in some way or I'm declaring the variable in the module properly, I'm not sure but I will keep trying until I run out of ideas.

      Thanks again.
      Frank

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by frankleggett
        Thanks for your reply I will try it in a module. I have tried to set up variables in a module before but every time I've tried when the variable is called the error comes back variable unknown this maybe because I'm not tying the module to the form in some way or I'm declaring the variable in the module properly, I'm not sure but I will keep trying until I run out of ideas.
        That's the spirit.

        Note though, I'm pretty sure you can do it in a form. You would declare it at the form level. In other words, at the top of the code before any Sub or Function declarations. Then in your code (perhaps the Form_Load event?) you would set it to the size you want using ReDim.

        Anyway, as you said, have a play with it and see where it goes.

        Comment

        • frankleggett
          New Member
          • Oct 2006
          • 22

          #5
          Thanks alot

          I tried as you said to declare the array at the top of the form under Option Explicit and then reDim inside the sub I needed it for and it worked.

          Frank

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by frankleggett
            I tried as you said to declare the array at the top of the form under Option Explicit and then reDim inside the sub I needed it for and it worked.
            That's great news! Have fun...

            Comment

            Working...