View Full Version : Counting fieldnames of a table


raindrop3
12-17-2001, 12:15 AM
Hello,

Does someone know the SQL-Statement to count howmany fields are in a table? (count the colomnames)

Thanks,

Albert

raskew
12-17-2001, 01:36 AM
Try this:

Function CountFields(MyTable As String) As Integer
'*******************************************
'Name: CountFields (Function)
'Purpose: Returns the number of fields in table
'Inputs: From debug window:
' ? countfields("tblProducts")
'Returns: 6
'*******************************************

Dim db As DATABASE, td As TableDef, tName As String

Set db = CurrentDb
tName = MyTable
On Error Resume Next
Set td = db.TableDefs(tName)
If Not Err = 3265 Then 'table not found
CountFields = td.Fields.Count
End If
db.Close
Set db = Nothing
End Function

raindrop3
12-17-2001, 01:40 AM
Raskew,

Thanks a lot for your quick response. It works great!

Albert

raindrop3
12-17-2001, 01:57 AM
Uhhh, I thought I could figure it out myself, but no luck. How do I retrieve the names of the columns?

Thanks.

Albert

raskew
12-17-2001, 04:19 AM
Adding to the previous code, immediately following the line reading CountFields = ...

Dim FieldName as string, fld as field
For Each fld In td.Fields
FieldName = fld.Name
debug.print FieldName
next fld

[This message has been edited by raskew (edited 12-17-2001).]

raindrop3
12-17-2001, 06:15 AM
Raskew,

Your the man!

THANKS!

Albert