I have this typical Access function to add new fields + default values to Access tables:. The field is added if it does not already exists in the table.
Public Function addNewField( _
m_tableName as string, _
m_fieldName As String, _
m_fieldType As Long, _ 'check syntax of .createField method for values'
m_fieldLength As Long, _ 'check syntax of .createField method for values'
Optional m_defaultValue As Variant = Null)
Dim myTable As DAO.TableDef
Dim myField As DAO.Field
On Error GoTo addNewField_error
Set myTable = currentDb.TableDefs(m_tableName)
Set myField = myTable.CreateField(m_fieldName, m_fieldType, m_fieldLength)
If Not IsNull(m_defaultValue) Then
myField.DefaultValue = m_defaultValue
End If
myTable.Fields.Append myField
Set myTable = Nothing
Set myField = Nothing
Exit Function
addNewField_error:
If Err.Number = 3191 Or Err.Number = 3211 Then
'The field already exists or the table is opened'
'nothing to do but exit the function'
Else
debug.print Err.Number & " - " & Error$
End If
End Function