Solved

Check for existence of a field in a Microsoft Access table

Posted on 2012-04-04
6
395 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

773 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