Solved

Check for existence of a field in a Microsoft Access table

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

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
ID: 37807811
ok, how many fields are you going to check?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

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

Accepted Solution

by:
Rey Obrero 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
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.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now