This VBA adds a modified date/time column to my tables, which is correct. It does not set the auto value, anyway i can do this with a code instead of going through all my tables and manually do it?
Code:
Sub AddModifiedDateToAllUserTables() Dim db As Object
Dim tdf As Object
Dim fld As Object
Dim fieldExists As Boolean
Dim tableName As String
Set db = CurrentDb
For Each tdf In db.TableDefs
tableName = tdf.Name
' ✅ Skip system and temporary tables
If Not (Left(tableName, 4) = "MSys" Or Left(tableName, 1) = "~") Then
fieldExists = False
' ✅ Check if ModifiedDate column already exists
For Each fld In tdf.Fields
If fld.Name = "ModifiedDate" Then
fieldExists = True
Exit For
End If
Next fld
' ✅ Add column only if not already present
If Not fieldExists Then
' Add field with DEFAULT Now()
db.Execute "ALTER TABLE [" & tableName & "] ADD COLUMN ModifiedDate DATETIME DEFAULT Now();", 128
Debug.Print "✅ Added ModifiedDate with DEFAULT Now() to: " & tableName
Else
Debug.Print "ℹ️ Already has ModifiedDate: " & tableName
End If
Else
Debug.Print "⛔ Skipped system/temporary table: " & tableName
End If
Next tdf
MsgBox "ModifiedDate update complete.", vbInformation
End Sub