I'm having a problem executing the following subroutine. I would call it from a sub on click routine. I would also like to pass a variable. There will be nothing returned from the subroutine. It is in a module named "Create Relation"
Public Sub CreateRelationship()
DoCmd.SetWarnings False
'Create relationship
Dim db As DAO.Database
Dim rel As DAO.Relation
Dim fld As DAO.Field
Dim RelName As String
RelName = "AWOLRelation"
'Initialize
Set db = CurrentDb()
'Check if relationship already exists
For Each rel In db.Relations
If rel.Name = RelName Then
DoCmd.SetWarnings True
Exit Sub
End If
Next rel
'Create a new relationship
'Set rel = db.CreateRelation("TblAWOLNames, TblAWOLSchedule")
Set rel = db.CreateRelation(RelName)
'Define the properties
With rel
.Table = "TblAWOLNames" 'Specify primary tabl
.ForeignTable = "TblAWOLSchedule" 'Specify related table
.Attributes = dbRelationUpdateCascade + dbRelationDeleteCascade 'Specify attribute for cascading updates and deletes
'Add the field to the relation
Set fld = .CreateField("Badge #") 'Field name in primary table
fld.ForeignName = "Badge #" 'Field name in related table
.Fields.Append fld 'Append the field
End With
'Save the new relationship to the collection
db.Relations.Append rel
'Clean up
Set fld = Nothing
Set rel = Nothing
Set db = Nothing
DoCmd.SetWarnings True
Exit Sub
End Sub