Click to See Complete Forum and Search --> : Arrays in VB


kjoter
October 22nd, 2001, 08:33 AM
Hello!

I am trying to print some values from a recordset, and the way i do it is to read them into a 2D array first, and then trying to print variables from the array in a loop.
I have pasted som of my code below, my question is: Why do "print #1, sData" print values, and why do not "Print #1, DataArray(0, C)" print anything but blank lines? Have i done anything wrong?

Dim rsAddresses As ADODB.Recordset
Dim fld As ADODB.Field
Dim sData As String
Dim DataArray() As String
ReDim DataArray(1, 0) As String
Do Until rsAddresses.EOF
sData = ""
For Each fld In rsAddresses.Fields
sData = sData & fld.Value & ", "
DataArray(0, C) = fld.Name
DataArray(1, C) = fld.Value

ReDim Preserve DataArray(1, C + 1) As String
C = C + 1
Print #1, DataArray(0, C)
Print #1, DataArray(1, C)
Next
rsAddresses.MoveNext
Print #1, sData; 'prints address
Loop

Thankful for all suggestions.

Andrew_Fryer
October 22nd, 2001, 08:54 AM
Hello,

I'm not 100% certain, but I think redim might destroy the current contents of the array...

Andrew

Iouri
October 22nd, 2001, 09:03 AM
First of all as Andrew_Fryer said Redim will destroy the value.

Secondly:
ReDim DataArray(1, 0) As String

here you redim array 1 element in the first dimension an 0 elemenbts in the second dimension


Do Until rsAddresses.EOF
sData = ""
For Each fld In rsAddresses.Fields
sData = sData & fld.Value & ", "

what the C element in the second dimension
I think your data will not go anywhere

DataArray(0, C) = fld.Name
DataArray(1, C) = fld.Value


Why do you want to throw rs data to the array and then to string and then print it? I think it is reasonable to print the recordset value directly

Do while not ts.EOF
Print #1, rs!Name & " " & rs!Value
Loop

Also be careful with the filed named 'Value', because value is reserved word in VB. Try it several different ways
rs!Value
rs.("Value").Value
rs.(Field(2)).Value



Iouri Boutchkine
iouri@hotsheet.com

srinika
October 22nd, 2001, 09:44 PM
Note 1: Redim will destroy the values
Note 2: Redim Preserve will not destroy the values
Note 3: As Iouri says, using the recordest's fields directly, rather than putting to fields is more efficient
Note 1: U r not getting correct results not because of these but
u put value to C'th element, increment C & the view C'th element

Eg: consider when, C = 0

DataArray(1, C) = fld.Value
' This makes DataArray(1, 0) = Value1

C = C + 1
' This makes C =1

'Try to Print DataArray(1, 1)
Print #1, DataArray(0, C)
' result is nothing getting printed
' if this is the case use the following
Print #1, DataArray(0, C - 1)