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 see if a field exists in a table??
 
Here is the code I have used in the past:

Function FieldExists(strField as string, varIn as Variant) as Boolean

on error resume next
Dim fld as DAO.Field

FieldExists=False

for each fld in varIn.Fields
if fld.name=strfield then
FieldExists=True
end if
next fld

End Function.

I'm writing it freehand, so excuse any mistakes please! The function takes a field name and a variant as parameters. The variant could be a tabledef, a recordset or a querydef.

Dim td as tabledef
set td=currentdb.tabledefs("tblData")

msgbox Fieldexists("Field1",td)

HTH

Ben ----------------------------------
Ben O'Hara
bo104@westyorkshire.police.uk
----------------------------------
 
I took your code out of the function and it doesn't seem to work. I get the "Object Invalid or no longer set" error.
Here's how I used your code.

Dim td As TableDef
dim fld As DAO.field

FieldExits = False
Set td = CurrentDb,TableDefs("actsrec") 'It stops here!!
For Each fld in td.fields
If fld.Name = "balance then
FieldExists = True
End if
Next fld
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top