check if field exists (1 Viewer)

laailalalaa

Registered User.
Local time
Today, 04:55
Joined
Jan 5, 2010
Messages
10
hello,

before adding a column to a table i want to check if that column doesn't already exist in the table. is there a way to do this in access?

the only way i can think of is through vb.net (populate datatable and check the columns), which is pretty straightforward, but i'd like to know whether it is possible to perform this task using plain sql.

thank you
 

namliam

The Mailman - AWF VIP
Local time
Today, 13:55
Joined
Aug 11, 2003
Messages
11,695
if executing a query with a column name fails... you know the column doesnt exist

VB = VBA though.... what ever is easy in VB is easy in VBA
 

laailalalaa

Registered User.
Local time
Today, 04:55
Joined
Jan 5, 2010
Messages
10
i know the query would fail, i failed to specify i don't wanna take this approach.

i thought there is some SqlServer-like table schema data. but i don't know of any access "system views" that make available this kind of data.
 

DCrake

Remembered
Local time
Today, 12:55
Joined
Jun 8, 2005
Messages
8,632
A sloppy way to do this is to have a hidden listbox on your form that has the query as the rowsource. Set the rowsource type to be field list. Then if you use vba to enumerate throught the lists contents to see if the named field exists.

David
 

namliam

The Mailman - AWF VIP
Local time
Today, 13:55
Joined
Aug 11, 2003
Messages
11,695
In the menu while in the database screen: Tools > Options
The check the "System objects" to be visible, you can then see and browse the tables.
NOTE: The use of these tables is "not supported by M$"
 

laailalalaa

Registered User.
Local time
Today, 04:55
Joined
Jan 5, 2010
Messages
10
In the menu while in the database screen: Tools > Options
The check the "System objects" to be visible, you can then see and browse the tables.
NOTE: The use of these tables is "not supported by M$"

not quite what i'm looking for, but thanks for your answer
 

ghudson

Registered User.
Local time
Today, 07:55
Joined
Jun 8, 2002
Messages
6,195
Verify field in table exists

Not SQL but this works...

Code:
Public Function VerifyFieldExists(ByVal sfieldName As String, ByVal stableName As String) As Boolean

    Dim db As Database
    Dim tbl As TableDef
    Dim fld As Field
    Dim sName As String
    
    Set db = CurrentDb
    Set tbl = db.TableDefs(stableName)
    
    For Each fld In tbl.Fields
        If fld.Name = sfieldName Then
            VerifyFieldExists = True
            Exit For
        End If
    Next

     If VerifyFieldExists Then
         MsgBox "Field Name " + sfieldName + " Exists in " + stableName
     Else
        MsgBox "Field Name Does Not Exist"
     End If

End Function
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:55
Joined
Sep 12, 2006
Messages
15,651
this should work


Code:
function fieldexists(tablename as string, fieldname as string) as boolean
dim exists as boolean

   exists=false
   on error resume next
   exists = currentdb.tabledefs(tablename).fields(fieldname).name = fieldname

   fieldexists = exists
end sub
 

Users who are viewing this thread

Top Bottom