Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Redim Preserve troubles.

Posted on 2000-03-27
10
Medium Priority
?
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 14

Expert Comment

by:wsh2
ID: 2661608
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 80 total points
ID: 2661611
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
ID: 2662441
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:vbRetard
ID: 2662608
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
ID: 2662807
vbRetard.. his code IS the same as yours, all he did was make your arrays 1 based rather than 0 based.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2662830
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
ID: 2663814
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
ID: 2664036
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
ID: 2664197
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
ID: 2664335
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

647 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