Register  |  Login

Question Details    

   Question

Time: 06:05 - May 29, 2007     Asked by: dustPuppy      Status: Answered      Points: 75   

how do I check if a field exists in a table with sql?

I need to update a database (access, jet sql) automatically and in that process i need to check first if a table contains a specific field, before I add that field to the table.
What is the syntax to do that?

Ask a New Question

Become a Quomon Expert

Current Categories

 

Other Questions Needing Answers


   

Answer Discussion
Answer Discussion
Answer Summaries
Answer Summary
 
Try

CurrentDB.TableDefs("Table").Fields("Field").Name

this will return the field name if the column exists. Otherwise it will throw an error.

Cheers

Peter

Expert:

PeterNZ

Date:

May 30, 2007

Time:

00:09

 

Votes: Good (0) | Bad (0)
Login to rate this answer

There is nothing u can do in SQL, it has to be done via VBA.


quick way is as Peter suggests. But if u trap for errors then u need to make sure u get the right error number.


eg
On Error Resume Next

Debug.Print CurrentDb.TableDefs("MyTbl").Fields("MyField").Name
If Err.Number = 0 Then
Debug.Print "Field Exists"
ElseIf Err.Number = 3265 Then
Debug.Print "Field Does Not Exist"
Else
Debug.Print Err.Number, Err.Description
End If


The other way if u dont want to catch certain errors is to loop thru the field collection in the table then add it that way. U can add fields in vba as well, just in case u didnt want to bother with sql
eg

'This bit loops thru field collection and sets a flag
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim i As Integer
Dim bFound As Boolean

bFound = False
Set db = CurrentDb
Set tbl = db.TableDefs("MyTbl")
For i = 0 To tbl.Fields.Count - 1

'I do a lowercase comparison just in case
If LCase$(tbl.Fields(i).Name) = LCase$("MyField") Then bFound = True
Next i
Debug.Print "Found", bFound

'This bit adds that field. In this case, I am adding it as text size 20
Dim fld As DAO.Field
If bFound = False Then
Set fld = tbl.CreateField("MyField", DB_TEXT, 20)
tbl.Fields.Append fld
End If


Expert:

nidhi

Date:

May 30, 2007

Time:

05:31

 

Votes: Good (0) | Bad (0)
Login to rate this answer

thanks guys.
I read that in the case of SQL Server it is actually possible, if you query the sys-tables. Anyway that option doesn't work in the access-case, so I guess I'll have to do it by code as you suggested.

Expert:

dustPuppy

Date:

Jun 04, 2007

Time:

01:29

 

Votes: Good (0) | Bad (0)
Login to rate this answer

what is CurrentDb ?
i dont know how to declear this.

Expert:

smuenchaisit

Date:

Nov 28, 2007

Time:

03:19

 

Votes: Good (0) | Bad (0)
Login to rate this answer

Question Answered

This question has been answered, and points have been rewarded to the following experts:

PeterNZ: 35
nidhi: 40

You're welcome however to comment or give additional information or if you wish, you have the ability to write an Answer Summary for this question by clicking on the "Answer Summaries" Tab.

 
No summaries have been submitted yet. Want to be the first?



Respond to this question:

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password

 

New User

  Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:


Forgotten Password

   

"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it


All Questions


Language Options

English:

www.quomon.com

Español:

www.quomon.es

Sponsors

Questions and Answers Software
Real Estate Postcards
Marketing Fulfillment