Solved

Array (Visual Basic)

Posted on 2004-04-18
20
557 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
  • 6
  • 4
  • 2
  • +5
20 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

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

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
can't work as error return "subcript out of range" ...
0
 
LVL 3

Expert Comment

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

 

Expert Comment

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

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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 49

Accepted Solution

by:
Ryan Chong earned 50 total points
Comment Utility
>>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
Comment Utility
let me working on ur suggestion and get back to you later
0
 
LVL 1

Expert Comment

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
for loop with Set 4 45
bunnyEars challenge 6 62
mapShare challenge 13 66
wordlen challenge 3 46
A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

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

6 Experts available now in Live!

Get 1:1 Help Now