Click to See Complete Forum and Search --> : Field Names
J_BEYTIA
Oct 31st, 2000, 03:10 PM
How do you retrieve the field names from a recordset?
barrk
Oct 31st, 2000, 03:43 PM
for n = 0 to adodc.recordset.recordcount - 1
myarray(n) = adodc.recordset.fields(n).name
next
Coder Guy
Oct 31st, 2000, 04:08 PM
I'm not sure about the above code because I don't use ADO much - I still use DAO mostly... But to me it looks as if the above code is looping through the entire recordset when you only want to loop through a fields collection...
Here's a good way to do it using DAO...
--------------------------------------------------------
Dim db as Database
Dim dbRS As Recordset
Dim NumOfFields As Integer
Dim X As Integer
Dim arrayFieldNames() As String
Set db = OpenDatabase(YourDatabasesPath)
Set dbRS = dbProbe.OpenRecordset("YourRecordsetsName", _ dbOpenDynaset)
NumOfFields = dbRS.Fields.Count
ReDim arrayFieldNames(0 To NumOfFields - 1)
For X = 0 To NumOfFields - 1
arrayFieldNames(X) = dbRS.Fields(X).Name
Next X
----------------------------------------------------------
If your reading the names into a control or something you could just add them during the loop instead of using an array to store them....
Hope this helps....
Coder Guy
Oct 31st, 2000, 04:11 PM
the line that read Set dbRS = dbProbe.OpenRecordset etc...
Should read Set dbRS = db.OpenRecordset ---- etc.. etc...
The probe part was because I was writing a recent
database where the name of the database object was dbProbe... It was probably obvious to you anyways :)
Just thought I'd post the correction anyway.
The corrected version is below.
Dim db as Database
Dim dbRS As Recordset
Dim NumOfFields As Integer
Dim X As Integer
Dim arrayFieldNames() As String
Set db = OpenDatabase(YourDatabasesPath)
Set dbRS = db.OpenRecordset("RecordsetName", _ dbOpenDynaset)
NumOfFields = dbRS.Fields.Count
ReDim arrayFieldNames(0 To NumOfFields - 1)
For X = 0 To NumOfFields - 1
arrayFieldNames(X) = dbRS.Fields(X).Name
Next X
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.