Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check for existence of a field in a Microsoft Access table

Posted on 2012-04-04
6
Medium Priority
?
400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

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

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

886 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