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

Access fieldnames from VBA - without new object

Status
Not open for further replies.

DoDo1975

Technical User
Jun 4, 2002
18
CA
I would like to get a listing of the fieldnames in an access table from within vb code included on a database form. Because I am already connected to the database, I shouldn't have to make an odbc, dao etc connection to it should I?

Can anyone tell me how to retrieve these table fieldnames??
 
You don't need to make a connection - everything you need is in the CurrentDb object e.g.

'==================
Dim Fld as Field
Dim Tbl as TableDef

Set Tbl = CurrentDb.TableDefs("TableName")

For Each Fld in Tbl.Fields

'Do something with the fields

Next
'====================

Hope this helps


 
Thanks, this is great. Only problem is that my tabledef object seems to become expired even before it can be used 2 lines down. It seems OK if I define a database object, but not without??

eg

Set myDB = CurrentDb()
Set Tbl = myDB.TableDefs("tablename")

works, but

Set Tbl = CurrentDb.TableDefs("tablename")

does not work????
 
Sorry about that - I normally create a DB variable myself for the very same reason. I'll check the code next time before sending it.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top