check to see if field exists in table prior to linking
Access 2013 Desktop database using VBA
I have a front end and linked back end database.
I have logic for the user to selectively change the linked backend database to another accdb file.
When the user indicates to link to a new backend database,
I want to first check to see if a particular field exists in the new database - if not, add it to the new database,
then proceed with the linking.
how do I determine the presence of a field in a database I haven't linked to yet?
Microsoft Access
Last Comment
Fredd
8/22/2022 - Mon
bfuchs
Function ifFieldExists(FieldName As String, TableName As String) As BooleanDim rs As Recordset, db As Database ' DAO Vars'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object Library'Set references by Clicking Tools and Then References in the Code View window'Checks if Field exists.'USAGE: ifFieldExists "FIELDNAME", "TABLENAME"On Error GoTo NoField 'If there is no Field capture the error.Set db = CurrentDb()'If Field is there open itSet rs = db.OpenRecordset("Select " & FieldName & " from " & TableName & ";") ifFieldExists = True rs.Close db.CloseExit FunctionNoField:'If field is not there close out and set function to false Set rs = Nothing db.Close Set db = Nothing ifFieldExists = FalseExit FunctionEnd Function
Open in new window