Solved

Redim Preserve troubles.

Posted on 2000-03-27
10
287 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:vbRetard
  • 6
  • 3
10 Comments
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
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.

0
 
LVL 14

Accepted Solution

by:
wsh2 earned 20 total points
Comment Utility
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 control collection whereever it sees fit. As such, it is NOT your Redim statement that is at fault, it is the randomness of the Controls collection.. something that cannot be changed in Visual Basic.

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.

0
 

Expert Comment

by:Khisanth
Comment Utility
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

0
 

Author Comment

by:vbRetard
Comment Utility
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 ?
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
vbRetard.. his code IS the same as yours, all he did was make your arrays 1 based rather than 0 based.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:wsh2
Comment Utility
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>.
0
 

Author Comment

by:vbRetard
Comment Utility
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.
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
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>.
0
 

Author Comment

by:vbRetard
Comment Utility
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.
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
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>
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now