?
Solved

Array (Visual Basic)

Posted on 2004-04-18
20
Medium Priority
?
587 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:cyseng10
[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
  • 4
  • 2
  • +5
20 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 10856533
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
 
LVL 1

Author Comment

by:cyseng10
ID: 10856867
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
 
LVL 2

Expert Comment

by:gvpathi_dev
ID: 10856897
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:cyseng10
ID: 10856972
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
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 10857004
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
 
LVL 1

Author Comment

by:cyseng10
ID: 10857062
can't work as error return "subcript out of range" ...
0
 
LVL 3

Expert Comment

by:redfordb
ID: 10857485
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
 
LVL 1

Author Comment

by:cyseng10
ID: 10858064
i know redim preserve only change size of last dimension. but my question is add row.

 the if statement only a example.
0
 
LVL 2

Expert Comment

by:gvpathi_dev
ID: 10858123
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
 

Expert Comment

by:rhowella
ID: 10858489
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
 

Expert Comment

by:rhowella
ID: 10858499
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
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 10860594
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
 
LVL 1

Author Comment

by:cyseng10
ID: 10864792
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
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 150 total points
ID: 10865924
>>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
 
LVL 1

Author Comment

by:cyseng10
ID: 10865969
let me working on ur suggestion and get back to you later
0
 
LVL 1

Expert Comment

by:dapcom
ID: 10867855
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
 
LVL 1

Expert Comment

by:dapcom
ID: 10867876
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
 

Expert Comment

by:drfunjohn
ID: 10876718
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
 
LVL 1

Expert Comment

by:issamtaher
ID: 10878076
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
 

Expert Comment

by:drfunjohn
ID: 10886107
>>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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 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