| | |
ReDim error!
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
This is the code I tried using to get the array ReDim-ed :
But I get this error.
And it highlights this line
Any takers?
Thanks in advance
-Russell aka Rabbit
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 SubThis 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 SubPrivate Sub btnAdd_Click()
'Assigns new name and password to array
arrNames(u, 0) = txtName.Text
arrNames(u, 1) = txtPassword.Text
End SubBut I get this error.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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 SubAny 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...
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...
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 :
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
And im still getting the same error.
This is what I have now :
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
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
An Alternative is
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
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
Last edited by koolsid; Mar 29th, 2009 at 7:32 pm.
A good excercise for the Heart is to bend down and help another up...
Please Mark your Thread "Solved", if the query is solved...
==>If a post has helped you then Please Rate it!
Please Mark your Thread "Solved", if the query is solved...
==>If a post has helped you then Please Rate it!
![]() |
Similar Threads
- Problem linking (LNK2019 Error) (C++)
- Runtime Error 5 - Need help please (Visual Basic 4 / 5 / 6)
- Quickpak error when porting vb6.0 app from 98 to xp (Visual Basic 4 / 5 / 6)
- How do I get the default action of a specific file type? (Visual Basic 4 / 5 / 6)
- Newbie Help (Visual Basic 4 / 5 / 6)
- Info in Database converted to Chart (Visual Basic 4 / 5 / 6)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Array Help (VB.NET)
- Code not working for deleting recordfrom a file (Visual Basic 4 / 5 / 6)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Text box Validation
- Next Thread: Connect to mysql in another computer error(pls help)
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





