Solved

How to check if an Access column exists

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

920 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

17 Experts available now in Live!

Get 1:1 Help Now