Hey guys thanks for viewing this post.

I have created an array to store usernames and passwords. Im tryning to get the array ReDim-ed mid program.

Here is the array:

Public Sub Usernames_Passwords()
    
'Assigns names to array space 1
        arrNames(0, 0) = "bnsjon"
        arrNames(1, 0) = "bnsjack"
        arrNames(2, 0) = "bnsjane"
        arrNames(3, 0) = "bnsjill"
        
'Assigns Passwords to array space 2
        arrNames(0, 1) = "jon"
        arrNames(1, 1) = "jack"
        arrNames(2, 1) = "jane"
        arrNames(3, 1) = "jill"
End Sub

This is the code I tried using to get the array ReDim-ed :

Private Sub Form_Load()
    Dim u As Integer
'Redims the array arrNames with new holding amount
'while preserving previous values
    u = 3
    u = u + 1
    ReDim Preserve arrNames(u, 1) As String
End Sub
Private Sub btnAdd_Click()
'Assigns new name and password to array
    arrNames(u, 0) = txtName.Text
    arrNames(u, 1) = txtPassword.Text
End Sub

But I get this error.

Compile Error:
Array already defined

And it highlights this line

Private Sub Form_Load()
    Dim u As Integer
'Redims the array arrNames with new holding amount
'while preserving previous values
    u = 3
    u = u + 1
    ReDim Preserve arrNames(u, 1) As StringEnd Sub

Any takers?
Thanks in advance

-Russell aka Rabbit

Unfortunately in 2D arrays redim is not working..
I recommend you to use two (one-dimensional) arrays one for usernames and the other for passwords.The arrays will be parraller.

You can use the redim - function on 1D array without error...
For exampe..
----------------------------------------------
dim usernames() as string
dim passwords() as string
dim rows as integer

rows=5

redim usernames(rows)
redim passwords(rows)

''Values assignment....
rows = rows +1

redim preserve usernames(rows)
redim preserve passwords(rows)
----------------------------------------------
I hope that i helped you...

Thanks for replying KSS

Thats a pitty though. Is it that it CANT be done? Or neither of us know how to? lol

And thanks for the advice. I was hoping I wouldnt have had to do so, but I'll have to impliment that.

Thanks again

-Russell aka Rabbit

Well.... I put them in two different arrays. Debugged the code so that the login screen works fine (Y)

And im still getting the same error.
This is what I have now :

Private Sub Form_Load()
    Dim u As Integer
'Redims the array arrNames with new holding amount
'while preserving previous values
    u = 3
    u = u + 1
    ReDim Preserve arrNames(u)
    ReDim Preserve arrNamesP(u)
End Sub

So im guessing its not that it cant be done. But we or I am just doing it wrong.
Any takers?

thanks in advance

-Russell aka Rabbit

Try the attachment file...
I just write it and it works fine...

Is it working?

Yes it is. Your solution worked perfectly

-Russell aka Rabbit

Oh woah woah. Wrong thread.
I'll try your ReDim zip now

-Russell aka Rabbit

KSS again. Thank you. I didnt know about list box till now O_O

Now that i've rummaged through your code, I understand how to use it. Thank you. I do beleive I can consider this solved as well.
2for you man lol. Your on a role

-Russell aka Rabbit

Hi Russel

Since the thread is already solved, I feel you need to also know why this happens... This is just for your FYI...

When using Preserve keyword, you can resize only the last array dimension. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.

For Example

Private Sub Command1_Click()
    Dim MyArray() As String
    ReDim Preserve MyArray(1, 1)        '<~~ YOU CAN DO THIS
        MyArray(1, 1) = "1. Blah Blah"
    ReDim Preserve MyArray(1, 2)        '<~~ YOU CAN DO THIS
        MyArray(1, 2) = "2. Blah Blah"
    ReDim Preserve MyArray(1, 3)        '<~~ YOU CAN DO THIS
        MyArray(1, 3) = "3. Blah Blah"

    u = 2
    ReDim Preserve MyArray(u, 3)        '<~~ YOU CANNOT DO THIS
End Sub

An Alternative is

Private Sub Command1_Click()
    Dim MyArray() As String, MyTempArray() As String
    ReDim Preserve MyArray(1, 1)        '<~~ YOU CAN DO THIS
        MyArray(1, 1) = "1. Blah Blah"
    ReDim Preserve MyArray(1, 2)        '<~~ YOU CAN DO THIS
        MyArray(1, 2) = "2. Blah Blah"
    ReDim Preserve MyArray(1, 3)        '<~~ YOU CAN DO THIS
        MyArray(1, 3) = "3. Blah Blah"
    
    u = 2
    ReDim Preserve MyArray(u, 3)        '<~~ YOU CANNOT DO THIS
    
    ReDim MyTempArray(u, 1)
    'Transfer data from MyArray to this array
End Sub

Woah, guess not. =S

Still getting the same issue as before.

With your code, your array hasnt been dimensioned. Its empty.

So the ReDim seems to work there. But with mine that seems to be the problem. The fact that I have already included data in the array.

So your solution didnt work this time KSS. Thanks for posting though, I appreciate it.

Anybody else that is capable to lend some input, all help is appreciated. Thanks in advance.

-Russell aka Rabbit

Did you go thru my post?

This is what I know have, and the error is still the same. Thanks for that info Sidz.

Private Sub Form_Load()
'~~> Remove the Close system menu item and the menu separator.
    RemoveMenus Me, False, False, False, False, False, True, True
    
    Dim u As Integer
'Redims the array arrNames with new holding amount
'while preserving previous values
    u = 3
    u = u + 1
    ReDim Preserve arrNames(0 To u)
    ReDim Preserve arrNamesP(0 To u)
End Sub

yeah I read it Sidz.

And the common thing I notice is this :

Dim MyArray () as string

Is it that you can only ReDim an empty array?

ReDim Preserve arrNames(0 To u)

Before you do that, you need to dim it as follows

Dim arrNames() as string
and then
ReDim arrNames(0 To u) as string

For example

Private Sub Command1_Click()
    Dim arrNames() As String
    u = 3
    u = u + 1
    ReDim Preserve arrNames(0 To u) As String
End Sub

Is it that you can only ReDim an empty array?

No. You can only redim the last element of an array...

No. You can only redim the last element of an array...

Technically speaking...

Similarly, when you use Preserve, you can change the size of the array only by changing the upper bound; changing the lower bound causes an error.

which means

Dim MyString() As String
Redim MyString(0 To 1) As Integer
Redim MyString(1 To 2) As String

would work... but...

Dim MyString() As String
Redim MyString(0 To 1) As Integer
Redim [b]Preserve[/b] MyString(1 To 2) As String

would thrown error #9 subscript out of range

Good Luck

Hi vb5prgrmr

No you cannot do that :)

Seems like you missed my previous post....

Check post 10

Yes you can! Made a program and all. As long as you are not trying to preserved the data within the array you can dimension it to any lbound and ubound you want. However, if you are talking about the string then integer then you are correct. That is my typo.

Preserve Optional. Keyword used to preserve the data in an existing array when you change the size of the last dimension. (MSDN)

I think the key word is preserve. The whole purpose of the Preserve key word is to preserve data.

It's intended use is when increasing the size of the last dimension of a multimensional array. Notice that the MSDN documentation does not say changing the first dimension: it says, "the last dimension."

If you want to use the Redim Preserve features, then follow the guidelines:

If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. (MSDN)

Is it that you can only ReDim an empty array?

The ReDimstatement 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). (MSDN)

Microsoft says the Redim Statement is to be used with arrays declared with empty parentheses. I guess you could try otherwise; but why?

But I think I see your problem. Forgive me, if you've already solved this.

But look at how you started:

'Assigns names to array space 1
        arrNames(0, 0) = "bnsjon"
        arrNames(1, 0) = "bnsjack"
        arrNames(2, 0) = "bnsjane"
        arrNames(3, 0) = "bnsjill"
        
'Assigns Passwords to array space 2
        arrNames(0, 1) = "jon"
        arrNames(1, 1) = "jack"
        arrNames(2, 1) = "jane"
        arrNames(3, 1) = "jill"

The area that has the most values is the first dimension. Since the Redim Preserve feature is designed to preserve the data only when you expand the second dimension. Make the second dimension the dimension you want to increase.

Dim intArrayCount as integer

Private Sub IncreaseArray(CurrentArrayCount as Integer)
     Redim Preserve strNames (1, intArrayCount + 1)
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.