Checking to look if field is in a table

Leesha
BronzeLounger
Posts: 1487
Joined: 05 Feb 2010, 22:25

Checking to look if field is in a table

Post by Leesha »

Hi,
I have users that "should have" installed an update to their database tables. I need to be sure they have done this prior to giving them a new update. I would like to be able to use the onload function to see if the table has the new field. In this instance it would be tblReceipts and the field is [privateuse]. What would the code look like for something like this?

Thanks!
Leesha

User avatar
HansV
Administrator
Posts: 78451
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Checking to look if field is in a table

Post by HansV »

Here is a general function to check if a field is missing:

Code: Select all

Function MissingField(TableName As String, FieldName As String) As Boolean
    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Set dbs = CurrentDb
    On Error Resume Next
    Set tdf = dbs.TableDefs(TableName)
    Set fld = tdf.Fields(FieldName)
    If fld Is Nothing Then
        MissingField = True
    End If
End Function
Use like this:

Code: Select all

Sub Test()
    If MissingField("tblReceipts", "privateuse") Then
        MsgBox "Please install update", vbCritical
    End If
End Sub
Best wishes,
Hans

Leesha
BronzeLounger
Posts: 1487
Joined: 05 Feb 2010, 22:25

Re: Checking to look if field is in a table

Post by Leesha »

Thanks Hans! Worked like a charm!
Have a great day,
Leesha