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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating an array from a colunm in a database

Status
Not open for further replies.

cwalshe

Programmer
May 28, 2001
84
0
0
IE
HI there,

I am wondering how to do the following:

I have a database (say 17 cols in total, no of rows varies). I would like to read the contents of the last column into an array. How do I open the database and take the values into my array.

Thx again,

Cormac.
 
Hi,

You dont' mention which databse you use or which datatype the column has. Anyway the code could look something like:

-----------------------------------------------------------
Dim MyArr() As String, i as long
Set conn = New Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\tmp\test.mdb"

Set Rst = New Recordset
Rst.Open "SELECT MyTextField FROM MyTable1;", conn, adOpenStatic, adLockOptimistic

i = -1
If Not Rst.EOF And Not Rst.BOF Then
Rst.MoveFirst
While Not Rst.EOF
i = i + 1
ReDim Preserve MyArr(i)
MyArr(i) = Rst.Fields("MyTextField")
Rst.MoveNext
Wend
Else
'no records
End If

'clean up
Rst.Close
Set Rst = Nothing
conn.Close
Set conn = Nothing

If i > 0 Then
'do array stuff here...
End If

-----------------------------------------------------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top