Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with array 2

Status
Not open for further replies.

AFK1

IS-IT--Management
Aug 26, 2005
38
US
I am trying to populate my combo box and text boxes from array, that I already populated. I chcek if a value in an array is same as my gird Bookmark. then populate the combo box.


i=0
'check if I have rows in my array
If mlarrayMaxRows<> 0 Then
'Loop through rows
Do While i <= mlarrayMaxRows
If i = 0 Then
ReDim Array(mlMaxCols, i + 1)
Else
ReDim Preserve Array(mlMaxCols, i + 1)
End If
If Array(ITEM, i) = sItemID
---something here
If Not Array(QTY, i) Then
txtQty.Text = Array(QTY, i)
Else
txtQty.Text = ""
End If
cbo.Text = Array(LINE, i) + Space(5) + Array(ITEM, i)
Endif
i =i +1
Looop


The problem is if I don't use ReDim Preserve Array(mlMaxCols, i + 1) or ReDim Array(mlMaxCols, i + 1) then I get error "Subscript out of range (Error 9)".

The second problem is if I use Redim then My array comes to be blank.
Any views..
 
How about a For Next Loop.

For x = 1 to UBound(<arrayname>,<index value>)
if array(1,x)=something then
do something
end if
next x

 
I tried
For iCounter = 0 To UBound(Array, mlarrayMaxRows)
and got the same error.

Why do you start x = 1, arrays start wiht 0.

please help.
 
Arrays don't have to start with 0 or 1, they can start anywhere you want them to.

There is an option statement you can add to the top of your source code file to specify where arrays begin (as a default). Look up 'option base' for more information.

When working with arrays, it is always safest to use the LBound function. Ex.

For i = LBound(ArrayName) to UBound(ArrayName)
' something
Next



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks for explaing it gmmastros.
I think I am not clear in what I am trying to achieve.
1. I load my array from Info.
2. Then I let the user click on grid and I capture the bookmark and match it with first Array's value and match that to second array and if the value matches I load up my text boxes and combos.
I do get values from array but if I use redim then i don't get any result. Its empty.


vBookmark = (CInt(grd.SelBookmarks(iRowIndex)))
sItem= Array1(ITEM, vBookmark)
i=0
'check if I have rows in my array
If mlarrayMaxRows<> 0 Then
'Loop through rows
Do While i <= mlarrayMaxRows
ReDim Preserve Array(mlMaxCols, i + 1)
If Array2(ITEM, i) = sItem
---something here
If Not Array2(QTY, i) Then
txtQty.Text = Array2(QTY, i)
Else
txtQty.Text = ""
End If
cbo.Text = Array2(LINE, i) + Space(5) + Array2(ITEM, i)
Endif
i =i +1
Looop
 
Yep, it's official. I'm confused.

Generally speaking, you use redim when you want to change the size of an array. You use redim preserve when you want to change the size of an array and preserve the data within the array.

Removing some of the code you posted...

Code:
i=0
If mlarrayMaxRows<> 0 Then
   'Loop through rows
    Do While i <= mlarrayMaxRows
         ReDim Preserve Array(mlMaxCols, i + 1)
         i =i +1
    Looop

First time though the loop, you are redim preserving the array to 1, second time through, you redim preserve to 2, etc...

Array data is only preserved if you make the array larger. If you redim preserve smaller, then you will lose data.

ex.

(Not vb code)
Array = "1,2,3,4,5,6,7"

Array(0) = 1
Array(1) = 2
etc...

If you redim preserve the array to 4, then your array looks like...

Array = "1,2,3,4,5"

You will have lost the 5th and 6th elements.

I'm confused about something. If the array is already loaded, why do you want to change it's size? You should be able to loop through the array and get the values you need to show the user without modifying the size of the array.

Also

I noticed that you have Loop spelled wrong. You should get in to the habit of running with full compile instead of just running. Running with full compile with catch syntax errors for you.


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
The reason why I am using Redim again after loading my data is that I was getting that error(same one) and I findout in details of error to use redim. Thats why I added the redim code to my programe.
 
Maybe a sample code will help. I am looping thru aryParts to see if the value of cboPartsEdit matches anything in the array (Which contains a list of known good part numbers.) If I find a match then I update the FlexGrid with the values from the array.

why do I start the loop at 1? If I use 0 then the subscript is out of range.
Code:
    'Updates the grid
        MSFlexParts.TextMatrix(R, C) = cboPartsEdit
          For a = 1 To UBound(aryParts, 2)
                If cboPartsEdit = aryParts(1, a) Then
                    MSFlexParts.TextMatrix(R, 2) = aryParts(2, a)
                    MSFlexParts.TextMatrix(R, 3) = FormatCurrency(MSFlexParts.TextMatrix(R, 1) * MSFlexParts.TextMatrix(R, 2), 2)
                    MSFlexParts.TextMatrix(R, 5) = aryParts(3, a) 'part id
                    Exit For
                End If
         Next a

HTH
 
I removed the redim part from my code and start the rowcounter at 1, and I didn't get any error.

vBookmark = (CInt(grd.SelBookmarks(iRowIndex)))
sItem= Array1(ITEM, vBookmark)
i=1
'check if I have rows in my array
If mlarrayMaxRows<> 0 Then
'Loop through rows
Do While i <= mlarrayMaxRows
If Array2(ITEM, i) = sItem
---something here
If Not Array2(QTY, i) Then
txtQty.Text = Array2(QTY, i)
Else
txtQty.Text = ""
End If
cbo.Text = Array2(LINE, i) + Space(5) + Array2(ITEM, i)
Endif
i =i +1
Loop

Thanks for Everyones Help in this regard. I really appreciate theis.

I only have one question. As you try to explain that I can start my array from 0 or 1, if I start from 1, I am not going to lose some records..right. I just want to understand that concept. lets say I have 15 rows in my array and I say

For a = 1 To maxRows
-----
Next a

My loop will go through rows 15 time right.


 
As I stated before, the safest/best method is to use the LBound and the UBound functions to control your loops.

For a = LBound(Array) To UBound(Array)
' some code here
Next

With this method, you will not get an Index Array Out Of bound error.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I try to use....

For iRowCounter = LBound(Array) To UBound(Array)

Next iRowCounter

I have total of 6 rows in my array. When it get to array 6, it gives error of subscreipt out of range. I really like it, it is lees number of code and you don't need to remember how many line numbers and from where to start. I am learning new things..
 
George- I have/will change my code to reflect the LBound(Array) to UBound(Array). I to am learning from you as well and didn't take the time to update the code before I posted it... :). I never actually thought of using UBound but it makes perfect sense.

AFK1- I'm going to update my code to Georges recomendation and I will post back tomorrow as I am not at work now, but it should work the same as my for a = 1 to ubound line...I would think.
 
George-I updated my code and used LBound instead of 1 to ubound and got the subscript out of range error because LBound is 0. Can you explain as to why using
Code:
for x = LBound(array) to UBound(array)
is prefered over...
Code:
for x = 1 to UBound(array)
 
>why ... prefered

It has already been explained. Arrays are not compelled to start at any specific number e.g.

Dim wombat(19 to 23,5 To 7) As Long

so the use of LBound ensures that you start at the right place and that you don't get a subscript out of range error

 
Hey strongm, thanks for the reply. Your example helped me understand the logic behind using LBound.

I guess the part that confuses me is that in my situation I have DIM aryParts() which doesn't specify a starting number. When I loop through that array if I use
Code:
For x = LBound(aryParts) to UBound(aryParts,2)
If cboPartsEdit = aryParts(1, a) Then
.
.
.
end if
I get a subscript out of range error. I think because LBound is 0 and aryParts(1,0) is not valid. If I replace LBound with 1 it works. How do I handle this?
Thanks to you and everyone else for helping me understand this.
 
For x = LBound(aryParts[red],2[/red]) to UBound(aryParts,2)

The second parameter for the ubound and lbound function is the dimension. You were already specifying the dimension in the ubound, but not the lbound.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
To illustrate....

Code:
    Dim wombat(19 To 23, 5 To 7) As Long
    
    Debug.Print LBound(wombat, 1) & "," & UBound(wombat, 1)
    Debug.Print LBound(wombat, 2) & "," & UBound(wombat, 2)

The first debug.print line displays 19,23
and the second displays 5,7


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George,
The light finally came on fully in my dark head [sunshine] Many thanks to you and strongm for clearing that up for me. I also hope that AFK1 found this as helpful as I did. Both of you enjoy the five pointed pink thingy as you both are deserving! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top