Solved

Check for existence of a field in a Microsoft Access table

Posted on 2012-04-04
6
396 Views
Last Modified: 2012-04-04
I have a vba program that alters a linked table - a new field is added.  Before I create the field, I need to make sure that it doesn't already exist.  Here is the code I have to do the alter:

Public Function Alter_Table()
    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Dim strSQL As String
   
    On Error GoTo Alter_Table_Exit
   
    Set db = DBEngine.OpenDatabase("C:\Quote Templates\Data Files\Quote Data.mdb")
    Set tdf = db.TableDefs("tbl_Quotes")
   
    Set fld = tdf.CreateField("Freight_PO", dbBoolean)
    fld.DefaultValue = 0
    tdf.Fields.Append fld
    Set fld = Nothing
   
    Set fld = tdf.CreateField("Freight_Amt", dbCurrency)
    fld.DefaultValue = 0
    tdf.Fields.Append fld
    Set fld = Nothing
    Set tdf = Nothing
   
    db.Close
    Set db = Nothing

    Set db = CurrentDb()
    Set tdf = db.TableDefs("tbl_Quotes")
    tdf.RefreshLink

Alter_Table_Exit:
   
    DoCmd.OpenQuery ("qupd_Freight_Amt_0")
     
    Set tdf = Nothing
    db.Close
    Set db = Nothing
    Exit Function
   
End Function
0
Comment
Question by:Marilync1266
  • 3
  • 3
6 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37807717
Set db = DBEngine.OpenDatabase("C:\Quote Templates\Data Files\Quote Data.mdb")
    Set tdf = db.TableDefs("tbl_Quotes")

'add this codes

For Each fld In tdf.Fields
    If fld.Name = "Freight_PO" Then
        MsgBox "Field exists"
        Exit Function
    End If
Next

'end of codes    to add

Set fld = tdf.CreateField("Freight_PO", dbBoolean)
    fld.DefaultValue = 0
    tdf.Fields.Append fld
    Set fld = Nothing
0
 

Author Comment

by:Marilync1266
ID: 37807794
If it exists - I don't want to display a message, I need to continue in the code to check the next field - Freight_Amt
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37807811
ok, how many fields are you going to check?
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:Marilync1266
ID: 37807815
Just 2 - freight_po and freight_amt
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 37807856
Public Function Alter_Table()
    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Dim strSQL As String
   
    On Error GoTo Alter_Table_Exit
   
    Set db = DBEngine.OpenDatabase("C:\Quote Templates\Data Files\Quote Data.mdb")
    Set tdf = db.TableDefs("tbl_Quotes")
   
   Dim blnFld1 As Boolean, blnFld2 As Boolean
   For Each fld In tdf.Fields
        If fld.Name = "Freight_PO" Then
            blnFld1 = True
        End If
        If fld.Name = "freight_amt" Then
            blnFld2 = True
        End If
   
    Next

   If blnFld1 = False Then
            Set fld = tdf.CreateField("Freight_PO", dbBoolean)
            fld.DefaultValue = 0
            tdf.Fields.Append fld
            Set fld = Nothing
   End If
   
      If blnFld2 = False Then

            Set fld = tdf.CreateField("Freight_Amt", dbCurrency)
            fld.DefaultValue = 0
            tdf.Fields.Append fld
            Set fld = Nothing
            Set tdf = Nothing
   End If
    db.Close
    Set db = Nothing

    Set db = CurrentDb()
    Set tdf = db.TableDefs("tbl_Quotes")
    tdf.RefreshLink

Alter_Table_Exit:
   
    DoCmd.OpenQuery ("qupd_Freight_Amt_0")
     
    Set tdf = Nothing
    db.Close
    Set db = Nothing
    Exit Function
   
End Function
0
 

Author Comment

by:Marilync1266
ID: 37807906
Excellent!  Thanks!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question