I was looking for a way to update tables remotely, one issue I had was I had to invent a new column in a table. Which was straight forwards, using the "ALTER" SQL command.
Obviously, I only want this to run if the column didn't exist already.
I searched for "How to tell if column exists in table" and was greeted with a whole new system of TableDef, which looked messy and I wasn't prepared to learn:
Which does work - but looks messy.
I had a think and thought of a work around:
It runs a DLookup, and if there is nothing, it throws error 2471 (which says I've spelt it wrong or it can't find it)
What is the main difference? why could none of the forums think of my solution? - I feel like I've done something wrong??
I'm sure if I spent time on it, I could turn it into a generic function...
Obviously, I only want this to run if the column didn't exist already.
I searched for "How to tell if column exists in table" and was greeted with a whole new system of TableDef, which looked messy and I wasn't prepared to learn:
Code:
Dim db As Database
Dim tbl As TableDef
Dim fld As Field
Dim strName As String
Set db = CurrentDb
Set tbl = db.TableDefs(tableName)
For Each fld In tbl.Fields
If fld.Name = fieldName Then
FieldExists = True
Exit For
End If
Next
Which does work - but looks messy.
I had a think and thought of a work around:
Code:
Dim Bacs As String
On Error GoTo NoField
Bacs = DLookup("BacsInfo", "tblBranch", "ID = 1") 'Looks for the first line in the new column
NoField:
If Err.Number = 2471 Then 'Error in finding the field
Err.Clear
'Run Code
End If
It runs a DLookup, and if there is nothing, it throws error 2471 (which says I've spelt it wrong or it can't find it)
What is the main difference? why could none of the forums think of my solution? - I feel like I've done something wrong??
I'm sure if I spent time on it, I could turn it into a generic function...