Results 1 to 2 of 2
  1. #1
    ysrini is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    10

    Add columns if not exists


    Hi for a given access table, i want to add few columns if they don't exist.
    psedo code:
    If col1 not exists then create col1 ...
    If col2 not exists then create col2 ...
    If col3 not exists then create col3 ...

    And i want to run all the above if statements in one query
    Thanks
    -srinivas

  2. #2
    carlmdobbs is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2010
    Location
    Maryland
    Posts
    26

    I would use VBA.

    Quote Originally Posted by ysrini View Post
    Hi for a given access table, i want to add few columns if they don't exist.
    psedo code:
    If col1 not exists then create col1 ...
    If col2 not exists then create col2 ...
    If col3 not exists then create col3 ...

    And i want to run all the above if statements in one query
    Thanks
    -srinivas
    This gives you part of the solution. You can write me for more if needed.
    If you wish to know more and I can help you more please write
    Sub subAppendField()
    Dim tdfTemp as tabledef, strTblname as string

    Dim strN as string, varitype, varSize
    strTableName = "tblYourtablename"

    set tdfTemp = db.tabledefs(strTableName)
    strN = “NewField”
    If fnFieldNameExistsn (strTableName, strN) = true then GoTo
    Vartype = vbLong
    With tdfTemp
    .Fields.Append .CreateField(strN, _
    varType)
    End With
    End If
    End Sub
    You can iterate through the table field names like this
    Function fnFieldNameExists(strTableName As String, strFieldName As String) As Boolean
    Dim tblDef As TableDef
    Dim db As Database
    Set db = CurrentDb
    Dim fld As Field

    Set tblDef = db.TableDefs(strTableName)
    For Each fld In tblDef.Fields
    If fld.Name = strFieldName Then
    fnFieldNameExists = True
    End If
    Next fld

    db.close
    set db = nothing

    End Function
    Write me for more info if this isn't what you want.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. if exists UPDATE else INSERT
    By lloyddobler in forum Programming
    Replies: 18
    Last Post: 01-04-2012, 11:35 PM
  2. Checking if files exists
    By geoffishere in forum Access
    Replies: 1
    Last Post: 02-09-2010, 01:32 PM
  3. SQL - find out whether a table exists
    By yurako in forum Programming
    Replies: 2
    Last Post: 01-20-2010, 09:27 AM
  4. How to use if not exists in access?
    By halifaxdalRaymondXie in forum Queries
    Replies: 1
    Last Post: 01-14-2010, 03:18 PM
  5. Create Table If Not Exists
    By jp2access in forum Queries
    Replies: 7
    Last Post: 07-14-2009, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums