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

See if Field exists

Status
Not open for further replies.

MikeKohler

Technical User
Jun 22, 2001
115
0
0
CA
I have a database that brings in a linked table through an odbc connection, when I bring it in I have a module that creates a field. What I am asking is if there is a way to see if that field exists such that it would look something like this

If field does not exist
then create field
Else

Thanks, Mike Kohler
mkohler@telusplanet.net
 
You could try and add the field and if it is there then the following code will not add it as Access will only let you add a field to a table once.

Dim MyWorkspace As Workspace
Dim MyDatabase As DATABASE
Dim MyField As Field
Dim MyTableDef As TableDef

Set MyWorkspace = DBEngine.Workspaces(0) ' Get current workspace.
Set MyDatabase = MyWorkspace.OpenDatabase(YourPathannDatabaseNameHere) ' Open database.
Set MyTableDef = MyDatabase![YourTableNameHere]

Set MyField = MyTableDef.CreateField("YourFieldNameHere", dbBoolean)

MyTableDef.Fields.Append MyField ' Append field to collection.

Hope this helps,
 
Thanks for the reply, but I already know how to create a field. My problem was that I am still adjusting the code that I have written, it would be convenient that I didn't have to delete the fields that I have created every time I run the code. I am adding things like creating indexes etc.
Thanks, Mike Kohler
mkohler@telusplanet.net
 
Maybe something like this will work for you -
assuming you have a recordset variable called rst:
Code:
DIM fld       AS field
DIM AddField  AS Boolean
    AddField = True

FOR EACH fld in rst.fields
   IF fld.name = "MyFieldName" THEN
      AddField = False
      EXIT FOR
   END IF
NEXT

IF AddField = True THEN
  'Add the Field...
--------------
A little knowledge is a dangerous thing.
 
Hey, thanks for the answer, that was exactly what I was looking for.
Michael Kohler
mkohler@telusplanet.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top