Solved

How to check if an Access column exists

Posted on 1998-06-17
1
126 Views
Last Modified: 2008-03-04
I need to find out how to check if an Access column exists.

If an Access column does not exist my VB app. must add it.
0
Comment
Question by:jfl
1 Comment
 

Accepted Solution

by:
cerik earned 20 total points
ID: 1463531
try this, just stick it in a sub or function

Dim ws As Workspace
Dim db As Database
Dim FieldX As Field
Dim Searchstr As String
Dim index As Integer
Dim tdef As TableDef
'connect up to database
Set ws = Workspaces(0)
Set db = ws.OpenDatabase("D:/mydb") 'or whereever yours is
'get a tabledef to work with
Set tdef = db.TableDefs!myTable
'set column search name
Searchstr = "unknown"
index = -1
'scan thru column(fields) for match
For Each FieldX In tdef.Fields
    If FieldX.Name = Searchstr Then index = FieldX.OrdinalPosition
Next
'if no ordinal position found then add
If index = -1 Then
    'check that you can update
    If tdef.Updatable Then
        tdef.Fields.Append tdef.CreateField(Searchstr, dbText, 50)
    'you can add as different fields and field size but both are optional
    End If
End If


0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

832 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