Solved

Problem with code for listing field names

Posted on 2014-10-20
3
188 Views
Last Modified: 2014-11-22
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?
0
Comment
Question by:dsoderstrom
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40392173
You may need to modify to:

Dim fld As DAO.Field

/gustav
0
 

Author Closing Comment

by:dsoderstrom
ID: 40392222
That was the problem.
Thanks for the quick and accurate solution.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40392227
You are welcome!

/gustav
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question