Solved

How to check if an Access column exists

Posted on 1998-06-17
1
127 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

829 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