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

Finding Field Names

Status
Not open for further replies.

TidyTrax

Programmer
Jul 11, 2001
263
AU
Is there a piece of code where i can get a list of each of the field names in a table???
 
You could create a query in the following form to list the fields of a table:
Code:
describe tablename ' works for MySQL server
Code:
sp_help tablename  ' works for MS SQL server

Hope this helps!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
WHat i was actually hoping to be able to do was, to take an output of a list of the field names and read them into an array along the lines

For x = 1 to last field_name
FieldNames(x) = currentFieldName
next x

If you see where im coming from!
 
What are you using for your database access at the moment? Are you using the Database and Recordset objects (DAO)?

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
If you're using Excel then here is the code:
Code:
  ...  
  Dim db as Database
  Dim rs as RecordSet
  Dim counter as Integer
  Dim FieldNames() As String
  ...
  Set db = OpenDatabase(...)
  Set rs = db.OpenRecordset(...)
  ReDim FieldNames(rs.Fields.Count)
  ...
  For counter = 0 To rs.Fields.Count - 1
    FieldNames(counter) = rs.Fields(counter).Name
  Next counter
  Set rs = Nothing
  Set db = Nothing

Hope this helps!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Im actually using access and not excel!!!
 
I'm sorry but I haven't played with Access VBA - it should still be the same code perhaps with some minor changes.

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Hi TidyTrax,

This ought to do it for you ...

Code:
Dim tjDb As DAO.Database
Dim tjTab As DAO.TableDef
Dim tjFld As DAO.Field

Set tjDb = CurrentDb
Set tjTab = tjDb.TableDefs!Table5

For Each tjFld In tjTab.Fields
    MsgBox tjFld.Name
Code:
' Do what you want with the name(s) here
Code:
Next

tjDb.Close

Set tjFld = Nothing
Set tjTab = Nothing
Set tjDb = Nothing

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top