Array (Visual Basic)

how to add new row in array?
my array type is (0, 5), where 0 is row and 5 is  dimesions to store data.

how to add new row for array with  multi-dimensions.
LVL 1
cyseng10Asked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
>>Redim Preserve myArray(j, 4)
Since then the ubound of 2nd dimension should be 4 not 5, Myarray (0, 5) is not allowed, or did you change it to: Redim Preserve myArray(j, 5)  ?

if the 2 dimension array is confusing you, how about trying a UDT (User-Defined Type)?

Like:

Public Type myType
    Value1 As String
    Value2 As String
    Value3 As String
    Value4 As String
    Value5 As String
End Type

Then:

Dim myArray() As myType
j = 0
  for i = 1 to rst.recordcount '**recound count
       if rst.fields("a") > 400 then  
              Redim Preserve myArray(j)
              myarray(j).Value1 = rst.fields("a")
              myarray(j).Value2 = rst.fields("b")
              myarray(j).Value3 = rst.fields("c")
              myarray(j).Value4 = rst.fields("d")
              myarray(j).Value5 = rst.fields("e")
              j = j + 1
      endif
    rst.movenext
  next i
0
 
Ryan ChongCommented:
Do Not fix the array size for your declaraction, instead use Redim statement to redefine the array size.

Example:

Dim myArray() As String
   
    ReDim myArray(0, 5) As String
    myArray(0, 5) = "original value"
    MsgBox "myArray(0, 5) = " & myArray(0, 5)
   
    ReDim myArray(0, 6) As String
    myArray(0, 5) = "New value"
    myArray(0, 6) = "Added value"
    MsgBox "after myArray(0, 5) = " & myArray(0, 5)
    MsgBox "after myArray(0, 6) = " & myArray(0, 6)

Hope this helps
0
 
cyseng10Author Commented:
is seen different what i wanted. let me explain again of my problem.

i define Myarray(0, 5)

0 is row
5 is data stored in 5 column.

when getting record more than 1 record then i need to add row to stored data for that record.

how i add row into that array as multi-dimension?

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
gvpathi_devCommented:
May I understand your question in this manner.

you require a array which has multiple coloum coutns for each row as a unbalanced arrary..

If so then the solution is ..  you have to use the single dimentional array type as varient and then create new array and add it @ the place where the more then one value requires.

If not explain the requirement with clear example.
0
 
cyseng10Author Commented:
sorry for mis-understand on this question. let me explain clearly

i define MyArray(0,5).

i have problem to add new row into array.

example:-

i get 5 records from database and each record contain of 5 fields.

   
  below is code i assing fields into array:-
 
 j = 0
  for i = 0 to rst.recordcount '**recound count
       
       if rst.fields("a") > 400 then  
              myarray(j,0) = rst.fields("a")
              myarray(j,1) = rst.fields("b")
              myarray(j,2) = rst.fields("c")
              myarray(j,3) = rst.fields("d")
              myarray(j,4) = rst.fields("e")
              j = j + 1
      endif
  next i
 

from the example, i will add fields into array when meet my criteria. the problem i facing is adding new row into array.


0
 
Ryan ChongCommented:
Try use Redim Preserve statement?

like:

Dim myArray() As String
 
j = 0
  for i = 0 to rst.recordcount '**recound count
       if rst.fields("a") > 400 then  
              Redim Preserve myArray(j, 4)
              myarray(j,0) = rst.fields("a")
              myarray(j,1) = rst.fields("b")
              myarray(j,2) = rst.fields("c")
              myarray(j,3) = rst.fields("d")
              myarray(j,4) = rst.fields("e")
              j = j + 1
      endif
  next i
0
 
cyseng10Author Commented:
can't work as error return "subcript out of range" ...
0
 
redfordbCommented:
With Redim, you can only preserve the array if you only change the last dimesion of the array.
I think it would work better if you filtered your source via SQL ie. SELECT * from sourcetable where a > 400
Then just redim your control once ie.

Dim myArray() As String
Redim MyArray(rst.Recordcount,4)
While Not rst.EOF
    ....
    'Move to next record
    rst.Movenext
Wend

0
 
cyseng10Author Commented:
i know redim preserve only change size of last dimension. but my question is add row.

 the if statement only a example.
0
 
gvpathi_devCommented:
Another suggetion from the recordset you can directly load the data into an array.
 arr=   rec.getRows(n)
In the meanwhile, it is better to use Collections and Dictionary in the place where you want to do dynamically add the list.

0
 
rhowellaCommented:
your "subscipt out of range" occurs because of your 'For statement'. the maximum should be recordcount-1 since your lbound starts with 0. Try it out: (also, check if your recordset is using a client side as cursor location since in most applications, using server side cursors returns recordcount as 0)

j = 0
  for i = 0 to rst.recordcount-1            '**recound count
       if rst.fields("a") > 400 then  
              myarray(j,0) = rst.fields("a")
              myarray(j,1) = rst.fields("b")
              myarray(j,2) = rst.fields("c")
              myarray(j,3) = rst.fields("d")
              myarray(j,4) = rst.fields("e")
              j = j + 1
      endif
 next i
0
 
rhowellaCommented:
oooops! forgot the redim statement... here, try this out again:

Dim myArray() As String
 
j = 0
  for i = 0 to rst.recordcount-1                      '**recound count
       if rst.fields("a") > 400 then  
              Redim Preserve myArray(j, 4)
              myarray(j,0) = rst.fields("a")
              myarray(j,1) = rst.fields("b")
              myarray(j,2) = rst.fields("c")
              myarray(j,3) = rst.fields("d")
              myarray(j,4) = rst.fields("e")
              j = j + 1
      endif
  next i
0
 
Ryan ChongCommented:
oops. we should put a MoveNext there too..

so:

Dim myArray() As String
j = 0
  for i = 1 to rst.recordcount '**recound count
       if rst.fields("a") > 400 then  
              Redim Preserve myArray(j, 4)
              myarray(j,0) = rst.fields("a")
              myarray(j,1) = rst.fields("b")
              myarray(j,2) = rst.fields("c")
              myarray(j,3) = rst.fields("d")
              myarray(j,4) = rst.fields("e")
              j = j + 1
      endif
    rst.movenext
  next i

And i dont see a reason why the loops should be rst.recordcount-1, as the value of i is Not affect the size of array myArray. And i think the i should also start with 1, but not 0.

Let us know if the problem haven't resolved.

regards
0
 
cyseng10Author Commented:
the problem occured when next record where i need to assign the record field into array. i can't using redim preserve as return error 'subcript out of range'.

bear in mind that i using multi-dimension which Myarray (0, 5).

0 - row
5 - contains 5 columns fields

0
 
cyseng10Author Commented:
let me working on ur suggestion and get back to you later
0
 
dapcomCommented:
Hello,
I think the above solution should help you,

but just for your info:

your array is a 2 dimensional array:
  dimension 1 = rows: initialised with 1 element (0)
  dimension 2 = data with 6 elements (0 to 5)

You  just can't change the number of elements of the 1st dimension because as you know, you can only redim the last dimension.

So if you still wanted for any reason to use a multi-dimensional array, rather thyen the user-defined type solution,
your solution woyld be to invert the dimensions order.

That is:


0
 
dapcomCommented:
Oops, here it continues:

dim myarray(5,0)

then later:
  redim preserve myArray(5,1)
  ...

so to access the second value of row 20 would be:
a=myArray(19,1)

(Just trying to add some light...)
Dan

0
 
drfunjohnCommented:
Hey, there are no problem define fist dimension like column and secomd like row.
Just use:
'--------------------------------------------------
Dim myArray() As String
j = 0
  for i = 1 to rst.recordcount '**recound count
       if rst.fields("a") > 400 then  
              Redim Preserve myArray(4.j)
              myarray(0,j) = rst.fields("a")
              myarray(1,j) = rst.fields("b")
              myarray(2,j) = rst.fields("c")
              myarray(3,j) = rst.fields("d")
              myarray(4,j) = rst.fields("e")
              j = j + 1
      endif
    rst.movenext
  next i
'--------------------------------------------------
that is All!
Thanx
0
 
issamtaherCommented:
hi cyseng10

did you try this statment

Dim RsCount as integer

RsCount = Rs.RecordCount;
ReDim myArray(RsCount ,5) As String

Fro i=1 to  RsCount -1
  myArray(i ,1) = Rs.Fields("Value1").Value
  myArray(i ,2) = Rs.Fields("Value1").Value
  myArray(i ,3) = Rs.Fields("Value1").Value
  myArray(i ,4) = Rs.Fields("Value1").Value
  myArray(i ,5) = Rs.Fields("Value1").Value
  Rs.movenext
Next i

it will be work .!

0
 
drfunjohnCommented:
>>Redim Preserve myArray(j, 4)
Ok! I create mistake but this is not principial. But this is answer!!!:

Row or Column ? Any Array does not has row and does not has column!
Only first dimension and second!
Try this:
Dim arr(2,2) as String
arr(1,1) = "first"
arr(1,2) = "first1"
arr(2,1) = "second"
arr(2,2) = "second1"
Or:
Dim arr(2,2) as String
arr(1,1) = "first"
arr(2,1) = "first1"
arr(1,2) = "second"
arr(2,2) = "second1"
is what metter?

So  ryancys was right with some little changes:
use right Upper bound 5 instead 4:
Redim Preserve myArray(j, 5)
and replace first dimension with second like this:
Redim Preserve myArray(5, j)
Good luck
0
All Courses

From novice to tech pro — start learning today.