check if column exists in table

G

Guest

I want to check and see if a column exists in a table before trying to
dynamically create a query that uses that column name.

Any suggestions?

Thank you,
Judy
 
G

Guest

Copy this function to a module

Function CheckIfFieldExist(TableName As String, FieldName As String) As
Boolean
' Function that check if a field exist in the table
' Return True if exist , and False if doesn't exist
On Error GoTo CheckIfFieldExist_Err
Dim I As String
CheckIfFieldExist = True
I = Application.CurrentDb.TableDefs(TableName).Fields(FieldName).Name
Exit Function
CheckIfFieldExist_Err:
If Err = 3265 Then
CheckIfFieldExist = False
Else
MsgBox Error
End If
End Function

=====================
Use this function in your code

If CheckIfFieldExist("TableName","FieldName") Then
' Exist
Else
' Doesn't Exist
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top