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

What is the code to see if a field exists?

Status
Not open for further replies.

emumaster

Programmer
Aug 30, 2001
31
US
What is the VBA Code to check and see if a field exists in a table??
 
Try this..


Dim db As DATABASE
Dim x
Dim Fname, TableOfInterest
Dim tdfNew As TableDef
Dim tdfLoop As TableDef

Set db = CurrentDb()

Fname = "name"
TableOfInterest = "CustomersX"

For Each tdfLoop In db.TableDefs
If tdfLoop.NAME = TableOfInterest Then
For x = 0 To tdfLoop.Fields.Count - 1
If tdfLoop.Fields(x).NAME = Fname Then
MsgBox "Found Field of interest"
'Can do whatever you want here
End If
Next x
End If

Next tdfLoop


Btw - I would post these sorts of questions in this group : forum181 will get a better response.

Opp.
 
The above code produced an error at the :

For Each td in d.TableDefs

Run-Time error 91
Object variable or With lock variable not set
 
* NOTE: All of the following assumes you copied the earlier example code verbatim.

You just lost a "b" in "db". :) (The other day, I lost a whole assignment statement; for fifteen minutes, I couldn't figure out why my control's value stopped updating.)

Code:
For Each td in d.TableDefs
should be
Code:
For Each td in db.TableDefs

You did not have any object called "d" so VBA flagged the reference to *a variable that had not been set* when you tried to reference the Tabledefs property of d (
Code:
d.Tabledefs
doesn't exist, but
Code:
db.TableDefs
does).

Have a good one!
-- C Vigil =)
(Before becoming a member, I also signed on several posts as
"JustPassingThru" and "QuickieBoy" -- as in "Giving Quick Answers")
 
Actually I just mistyped in the reply message.
I have it correctly type in my code and it doesn't work.
 
I would check your implementation of my earlier code then - as I can cut and paste it onto a form - and it works!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top