954,559 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ReDim error!

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

rabbithaveit
Light Poster
32 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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...

KSS
Newbie Poster
21 posts since Mar 2009
Reputation Points: 11
Solved Threads: 3
 

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

rabbithaveit
Light Poster
32 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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

rabbithaveit
Light Poster
32 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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

Attachments Redim.zip (1.54KB)
KSS
Newbie Poster
21 posts since Mar 2009
Reputation Points: 11
Solved Threads: 3
 

Is it working?

KSS
Newbie Poster
21 posts since Mar 2009
Reputation Points: 11
Solved Threads: 3
 

Yes it is. Your solution worked perfectly

-Russell aka Rabbit

rabbithaveit
Light Poster
32 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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

-Russell aka Rabbit

rabbithaveit
Light Poster
32 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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

rabbithaveit
Light Poster
32 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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
koolsid
Light Poster
35 posts since Feb 2005
Reputation Points: 11
Solved Threads: 6
 

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

rabbithaveit
Light Poster
32 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Did you go thru my post?

koolsid
Light Poster
35 posts since Feb 2005
Reputation Points: 11
Solved Threads: 6
 

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
rabbithaveit
Light Poster
32 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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?

rabbithaveit
Light Poster
32 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 
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
koolsid
Light Poster
35 posts since Feb 2005
Reputation Points: 11
Solved Threads: 6
 
Is it that you can only ReDim an empty array?

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

koolsid
Light Poster
35 posts since Feb 2005
Reputation Points: 11
Solved Threads: 6
 
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 <strong>Preserve</strong> MyString(1 To 2) As String

would thrown error #9 subscript out of range

Good Luck

vb5prgrmr
Posting Virtuoso
1,912 posts since Mar 2009
Reputation Points: 156
Solved Threads: 296
 

Hi vb5prgrmr

No you cannot do that :)

Seems like you missed my previous post....

Check post 10

koolsid
Light Poster
35 posts since Feb 2005
Reputation Points: 11
Solved Threads: 6
 

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.

vb5prgrmr
Posting Virtuoso
1,912 posts since Mar 2009
Reputation Points: 156
Solved Threads: 296
 
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 ispreserve. 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)

hkdani
Posting Pro in Training
435 posts since Nov 2007
Reputation Points: 49
Solved Threads: 47
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You