Originally Posted by
skydivetom
And, btw, to make it easier, maybe the 2nd field does NOT necessarily have to match the data type of the existing field. After some more thinking, just because the source data = TEXT, does NOT mean the target must = TEXT... in fact, there's a good chance that it may be a NUM. To get started though, it may be ok to make all fields = TEXT.
Tom,
It is easier to convert a number or a date to text rather that the opposite.
Put this subroutine in a module and run it:
Code:
Sub AddLKFields()
Dim rsLK As DAO.Recordset
Dim db As DAO.Database
Dim fld As DAO.Field
Set db = CurrentDb
Set rsLK = db.OpenRecordset("SELECT [Name] AS table_name " _
& "FROM MSysObjects " _
& "WHERE [Name] Like 'LK_*';" _
, dbOpenForwardOnly)
With rsLK
If Not (rsLK.BOF And rsLK.EOF) Then
On Error Resume Next
While Not rsLK.EOF
With db.TableDefs(rsLK![table_name]).Fields
'Add [Target_Table] (Text)
Set fld = New DAO.Field
fld.Name = "Target_Table"
fld.Type = dbText
.Append fld
'Add [Target_Field] (Same Type as the first field)
Set fld = New DAO.Field
fld.Name = "Target_Field"
fld.Type = .Item(0).Type
.Append fld
'Add [Target_PK_FK] (Long)
Set fld = New DAO.Field
fld.Name = "Target_PK_FK"
fld.Type = dbLong
.Append fld
'Add [Comment] (Text)
Set fld = New DAO.Field
fld.Name = "Comment"
fld.Type = dbText
.Append fld
'Refresh fields collection.
.Refresh
End With
rsLK.MoveNext
Wend
End If
End With
rsLK.Close
Set rsLK = Nothing
Set fld = Nothing
Set db = Nothing
On Error GoTo 0
End Sub