Getting the fields names

M

Maracay

Hi guys

How can I know 1.- The number of columns in a table and 2.- The field names
of a query or table. I want to do a cycle until I have all the filed names.

Thanks
 
D

DStegon via AccessMonster.com

routine to display fields. Using ADO and printing to screen, but you can
have the routine do what ever you need. We put in a prefix so if you want to
get a list with say "rst!" and then the name of the fields.

Public Sub ListFields(tablename As String, Optional Prefix As String)
Dim rst As New ADODB.Recordset
Dim fld As ADODB.Field
On Error GoTo HandleErr

rst.Open tablename, CurrentProject.Connection
For Each fld In rst.Fields
Debug.Print Prefix & "!" & fld.NAME
Next
rst.Close
Set rst = Nothing

exithere:
Exit Sub

HandleErr:

Resume

End Sub
 
M

Maracay

Hi,

Can I do this in a DAO database, I did your example and I got rst! plus the
field's name, not the value in the field, is there something missing.

Thanks
 
D

DStegon via AccessMonster.com

Your original post

How can I know 1.- The number of columns in a table and 2.- The field names
of a query or table. I want to do a cycle until I have all the filed names.

#1 - put a variable in the routine "x" dim it as long and in the for each
loop make it increment by 1
x=x+1

#2 What I gave you gives you exactly what you asked for.
Hi,

Can I do this in a DAO database, I did your example and I got rst! plus the
field's name, not the value in the field, is there something missing.

Thanks
routine to display fields. Using ADO and printing to screen, but you can
have the routine do what ever you need. We put in a prefix so if you want to
[quoted text clipped - 27 lines]
 
G

Graham Mandeno

The code I posted for you uses DAO. The code DStegon posted for you uses
ADO. Both achieve exactly what you asked for, i.e.:
How can I know 1.- The number of columns in a table and
2.- The field names of a query or table.

If you also want to see the field values, you need to open a Recordset and,
for each record, cycle through the fields displaying its name and value.

For a table with a million records, this could produce a lot of pointless
output!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top