Query to retrieve field names of table

  • Thread starter Thread starter Snowy
  • Start date Start date
S

Snowy

Hello all,

I need just a query to get the field names from a table using just the
table
name.

Only field names, not type, etc..


something like


SELECT fields(*) from thistable
do while...


loop

(Visual basic 6.0 application using Mysql)


Thanks a lot,
Snowy
 
That will not return field names. It will return the first record in the
table.
Snowy will need to use the fields collection of the table. Here is some
sample code that does that. You can modifiy it to suit your needs:

Sub ListFieldNames(strTblName)
'Lists the fields in a table
Dim dbf As Database
Dim tdfs As TableDefs
Dim tdf As TableDef
Dim fld As Field
Dim blnFoundIt As Boolean

Set dbf = CurrentDb
Set tdfs = dbf.TableDefs

For Each tdf In tdfs
If tdf.Name = strTblName Then
Debug.Print "Table " & strTblName & " contains " &
tdf.Fields.Count _
& " Fields"
For Each fld In tdf.Fields
Debug.Print fld.Name
Next fld
blnFoundIt = True
Exit For
End If
Next tdf
If Not blnFoundIt Then
MsgBox "Table " & strTblName & " Not Found in Database", _
vbExclamation, "ListFieldNames"
End If
Set fld = Nothing
Set tdfs = Nothing
Set dbf = Nothing
Set tdf = Nothing
End Sub
 
That will not return field names. It will return the first record in the
table.
Snowy will need to use the fields collection of the table.

Alternatively, one can use the Fields collection of an empty recordset,
e.g.

Dim R As DAO.Recordset
Dim j as Long

Set R = CurrentDB.OpenRecordset( _
"SELECT * FROM TheTable WHERE FALSE;", dbOpenSnapshot)
For j = 0 to R.Fields.Count
Debug.Print j, R.Fields(j).Name
Next
R.Close

This is handy if there isn't TableDef (e.g. when importing from an
external source); otherwise I don't think there's anything to choose
between this and using TableDef.Fields.
 

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

Back
Top