Hi Duane,
here is some code you can put into a standard (general) module:
CODE
'~~~~~~~~~~~~~~~~
'example showing how to add certain fields to a particular table
'~~~~~~~~~~~~~~~~
Sub testaddFieldToTable()
' AddFieldToTable "test", "AutoID", dbLong, , "*AN*"
' AddFieldToTable "test", "SomeID", dbLong, , "*Null*"
' AddFieldToTable "test", "ImportLog", dbText, 255
AddFieldToTable "test", "DateCreated", dbDate, , "*Now*"
End Sub
'~~~~~~~~~~~~~~~~
'example showing how to add the same fields
'to every user table in the database
'~~~~~~~~~~~~~~~~
'~~~~~~~~~~~~~~~~~~~~~~~~~~ RunAddFieldsToTable_Tracking
Sub RunAddFieldsToTable_Tracking()
Dim tdf As DAO.TableDef
Dim mCountDone As Integer _
, mCountChecked As Integer
For Each tdf In CurrentDb.TableDefs
If Left(tdf.Name, 4) <> "Msys" Then
Debug.Print
Debug.Print tdf.Name & "...";
mCountChecked = mCountChecked + 1
If AddFieldToTable(tdf.Name, "EmpIDadd", dbLong _
, , "*Null*", "employee who added record") Then
mCountDone = mCountDone + 1
Debug.Print " EmpIDadd";
End If
If AddFieldToTable(tdf.Name, "EmpIDedit" _
, dbLong, , "*Null*", "employee who last edited record") Then
mCountDone = mCountDone + 1
Debug.Print " EmpIDedit";
End If
If AddFieldToTable(tdf.Name, "datAdd", dbDate _
, , "*Now*", "date record was added") Then
mCountDone = mCountDone + 1
Debug.Print " datAdd";
End If
If AddFieldToTable(tdf.Name, "datEdit", dbDate _
, , , "date record was last edited") Then
mCountDone = mCountDone + 1
Debug.Print " datEdit";
End If
End If
Next tdf
MsgBox mCountChecked & " tables checked" & vbCrLf & vbCrLf _
& "Added " _
& mCountDone & " tracking fields" _
, , "Add Tracking Fields to Table Design"
Proc_Exit:
On Error Resume Next
'close and release object variables
Set tdf = Nothing
Exit Sub
Proc_Err:
Select Case Err.Number
' 'Caption property not found
Case 3270: Resume Next
End Select
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " addTrackingFieldsToTable"
'press F8 to step through code and debug
'remove next line after debugged
Stop: Resume
Resume Proc_Exit
End Sub
'~~~~~~~~~~~~~~~~
'put the following function in a general module:
'~~~~~~~~~~~~~~~~
'~~~~~~~~~~~~~~~~~~~~~~~~~~ AddFieldToTable
Function AddFieldToTable( _
pTablename As String, _
pFldname As String, _
pDataType As Integer, _
Optional pFieldSize As Integer, _
Optional pDefaultValue As String = "", _
Optional pDesc As String = "", _
Optional pSkipMessage As Boolean = True) _
As Boolean
'written by Crystal
'strive4peace2007 at yahoo.com
'modified 8-28-07
'PARAMETERS
'pTablename --> name of table to modify structure of
'pFldname --> name of field to create
'pDataType --> dbText, dbLong, dbDate, etc
'pFieldSize --> length for text fields
'pDefaultValue --> *AN* = autonumber
' --> *Null* --> DefaultValue = Null
' --> *Now* --> DefaultValue = Now()
' --> otherwise whatever is specified
'NEEDS Reference to
'a Microsoft DAO Library
On Error GoTo Proc_Err
AddFieldToTable = False
Dim db As DAO.Database, fld As DAO.Field
'you could make this a passed parameter
' and open another database
Set db = CurrentDb
With db.TableDefs(pTablename)
Select Case pDataType
Case dbText
'Text
Set fld = .CreateField(pFldname, _
pDataType, pFieldSize)
Case Else
'Long Integer, Date, etc
Set fld = .CreateField(pFldname, pDataType)
End Select
If Len(pDefaultValue) > 0 Then
Select Case pDefaultValue
Case "*AN*"
'Autonumber
fld.Attributes = dbAutoIncrField
Case "*Null*"
'Null for DefaultValue
fld.DefaultValue = "Null"
Case "*Now*"
'Now for DefaultValue
fld.DefaultValue = "=Now()"
Case Else
'Now for DefaultValue
fld.DefaultValue = "=" & pDefaultValue
End Select
End If
If pDataType = dbText Then
fld.AllowZeroLength = True
End If
.Fields.Append fld
If Len(pDesc) > 0 Then
On Error Resume Next
fld.Properties("Description") = pDesc
If Err > 0 Then
fld.Properties.Append fld.CreateProperty("Description" _
, dbText, pDesc)
End If
On Error GoTo Proc_Err
End If
End With
db.TableDefs.Refresh
DoEvents
If Not pSkipMessage Then
MsgBox "Added --> " & pFldname _
& " to --> " & pTablename, , "Done"
End If
AddFieldToTable = True
Proc_Exit:
On Error Resume Next
If Not fld Is Nothing Then Set fld = Nothing
'if db is external and you OPENed it,
'you will need to close it too
If Not db Is Nothing Then Set db = Nothing
Exit Function
Proc_Err:
'if the field is already there, ignore error
If Err = 3191 Then Resume Proc_Exit
'linked table
If Err = 3057 Then Resume Proc_Exit
MsgBox Err.Description, , _
"ERROR " & Err.Number & " AddFieldToTable"
'press F8 to step through code and debug
'remove or comment next line after debugged
If IsAdmin Then Stop: Resume 'next line will be the one with the error
Resume Proc_Exit
End Function