Link to home
Start Free TrialLog in
Avatar of vbRetard
vbRetard

asked on

Redim Preserve troubles.

Hi all, having problem here with ReDim array. After rediming it th values are in reverse order. So if i entered the value 1 as the first value it will be shown to me as the last. I know i can loop backwards though array and get these values back but that looks like an extra step and i want to avoid that. Is there another way so the values will be stored as i enter them after i redim it ? here is the code listed below. Thanks , I welcome all suggestions.

Dim i As Byte, j As Byte, AEmpty() As String, AFull() As String
For Each Con In frm.Controls
    If TypeOf Con Is TextBox Or TypeOf Con Is ComboBox Then
        If Con.Text = "" Then
            Con.BackColor = vbYellow
            ReDim Preserve AEmpty(i)
            AEmpty(i) = Con.Name
            i = i + 1
        Else
            ReDim Preserve AFull(j)
            AFull(j) = Con.Text
            j = j + 1
            Con.BackColor = vbWhite
        End If
    End If
Next
Avatar of wsh2
wsh2

The Form Controls collection is not in any prescribed order.. and that is why your arrays appear to be going all over the place.. <sigh>. As Controls are Loaded / Unloaded VB just places them into the collection whereever it sees fit.. so it is NOT your Redim statement that is at fault.

If indeed you need to maintain some kind of order.. in your arrays then consider using a listbox.. or a dictionary.. or a listview.. and avail yourself of their sorting features. By storing the control name along with its Index you can later address the control (assuming it is still loaded) by using a CallByName function. If sorting is not important.. use the control name / index as a collection key and you can acheive addressibility that way.

ASKER CERTIFIED SOLUTION
Avatar of wsh2
wsh2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try This

Dim i As Byte, j As Byte
ReDim AEmpty(1 to 1) As String, AFull(1 to 1) As String
For Each Con In frm.Controls
    If TypeOf Con Is TextBox Or TypeOf Con Is ComboBox Then
        If Con.Text = "" Then
            Con.BackColor = vbYellow
            ReDim Preserve AEmpty(1 to i)
            AEmpty(i) = Con.Name
            i = i + 1
        Else
            ReDim Preserve AFull(1 to j)
            AFull(j) = Con.Text
            j = j + 1
            Con.BackColor = vbWhite
        End If
    End If
Next

Avatar of vbRetard

ASKER

Do you want to explain this to me because i just see it as the same my code. Just you placed Cempty(1to1) isn't this the same if i would have done it to cempty(i) ? You seem to ReDim array in declaration is this allowed ? Why don't we redim then the AFull() one as well ?
vbRetard.. his code IS the same as yours, all he did was make your arrays 1 based rather than 0 based.
In reading your code again.. there is nothing wrong with your preserves.. everything is going into your arrays.. in the sequence that the For Each Loop is giving them to you.

I'm curious, how can you say that they are going in backwards? Please be more specific about the problem.. <smile>.
wsh2 yes i understand that the array gets the values. For example this values go there from textboxes. Say the first textbox contains value "Hello" textbox2 has "BYE" and so on... So "Hello" will be on the last place of the array "BYE" will be before "HELLO". Thats why i though i can loop back and get the values from ubound(array) to its lbound. I understood from your earlier comment how vb loads controls. Thanks.Also i never delete my posts. I just sighned up with this site.
Ok.. getting closer now. First of all with your Textboxes, did you set them up as a control array? (ie.. First text box has an index propertry of 0.. the next textbox has an index property of 1).. If not.. you want to do that, because the index properties along with the control name will give you a precise place to put things in. Rather than loop through the controls collection.. you can just loop through the Textbox Control array from indexes 0 to n.. getting only the values you want.. <smile>.

Now.. from the sounds of your Bye / Hello comment it sounds like you are trying to Sort the Textbox items after they are entered.. and then place them in ascending order back into the Textboxes, right?.. Let me know.. and we will go from there.. <smile>.
Mr. Wsh2 Thanks for your comment again. I will set my textboxes as control array now. I wanted to do that after i didn't get the results i wanted but i will do that now. Thanks for your comments i appriciate them. I will post another question if i won't get this right. Thanks.
vbRetard.. a control array of Textboxes is going to work GREAT!!! You are now only going to need one textbox event procedure to handle all n Textboxes.. as the textbox event procedures will now all have an Index parameter of the Textbox effected by the event in them.

After the Control array is established.. to go through it.. you now access each of the textboxes via index.. like this..

Dim intIndex as Integer
For intIndex = 0 to UBound(Text1)
  MsgBox Text1(intIndex).Text
Next intIndex

Pretty kewl, huh? Best part is.. that that the Textbox index never changes (unless you dynamically load / unload Textboxes to the array.. which I doubt you are doing) so that you can use the indexes over and over.

One last thing I GOTTA know.. are you trying to sort the values placed into the Textboxes after the user enters them?

Keep me posted.. <smile>