Link to home
Start Free TrialLog in
Avatar of dsoderstrom
dsoderstrom

asked on

Problem with code for listing field names

I am trying to find the field names in a table in an Access database other than the current database and write these  field names into a  table in the current database.
I am using the following code:

       Dim fld As Field
       Dim db As DAO.Database
       Dim rs2 As DAO.Recordset

       Set db = OpenDatabase(txtdatabasename)    
       Set rs2 = Currentdb.OpenRecordset("TableFieldNames")
       Currentdb.Execute ("DELETE TableFieldNames.* FROM TableFieldNames")

       For Each fld In db.TableDefs(cmbTable).Fields
             rs2.AddNew
             rs2!NamofTable = cmbTable
             rs2!NameOfField = fld.Name
             rs2.Update
       Next fld
       rs2.close
     
Variable txtdatabasename contains the full path for the database that the table is in.
Variable cmbTable contains the name of the table.

I am getting the error message "Run-time error '13':   Type mismatch"  on the following line:
       For Each fld In db.TableDefs(cmbTable).Fields

What am I doing wrong here?
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dsoderstrom
dsoderstrom

ASKER

That was the problem.
Thanks for the quick and accurate solution.
You are welcome!

/gustav