accessNator
Registered User.
- Local time
- Today, 13:54
- Joined
- Oct 17, 2008
- Messages
- 132
I have a button when clicked, it creates a relationship between two tables. It works fine but if I have a situation where a relationship already exists, I want it to stop and exit. My current code sends a message box that the relationship exist but it does not stop and proceeds to the next line.
I am sure its a simple fix or maybe I should write it a different way. I am giving an example of the same relationship that already exists to test the error, but I need it to stop in its tracks.
Button:
THoughts?
I am sure its a simple fix or maybe I should write it a different way. I am giving an example of the same relationship that already exists to test the error, but I need it to stop in its tracks.
Button:
Code:
Private Sub cmdCreateRelationships_Click()
On Error GoTo Err_cmdCreateRelationships_Click
Me.Repaint
Me.txtStatus.ForeColor = vbBlue
Me.txtStatus.Value = "Starting"
On Error Resume Next
' Create Relationships
Call CreateRelationships
' Use the following line if you want to customize a relationship
'Call CreateRelationships1
DoCmd.Beep
Me.Repaint
Me.txtStatus.ForeColor = vbBlue
Me.txtStatus.Value = "Finished creating relationships"
Exit_cmdCreateRelationships_Click:
Exit Sub
Err_cmdCreateRelationships_Click:
MsgBox Err.description
Resume Exit_cmdCreateRelationships_Click
End Sub
Code:
Private Sub CreateRelationships()
' tblCompanyInformation => tblContactAgentInformation
CreateRelationDAO "DaotblCompanyInformation_DaotblContactAgentInformation", "tblCompanyInformation", "tblContactAgentInformation", "company_cid", "agent_cid"
' tblCompanyInformation => tblContactAgentInformation
CreateRelationDAO "DaotblCompanyInformation_DaotblContactAgentInformation", "tblCompanyInformation", "tblContactAgentInformation", "company_cid", "agent_cid"
' tblCompanyInformation => tblMiscTransactions
CreateRelationDAO "DaotblCompanyInformation_DaotblMiscTransactions", "tblCompanyInformation", "tblMiscTransactions", "company_cid", "cid"
End Sub
Code:
Private Sub CreateRelationDAO(passDAORelation As String, passTablePrimary As String, passTableForeign As String, passTableFldPrimary As String, passTableFldForeign)
On Error GoTo Err_CreateRelationshipsDAO
Dim db As DAO.Database
Dim rel As DAO.Relation
Dim fld As DAO.Field
'Initialize
Set db = CurrentDb()
'Create a new relation.
Set rel = db.CreateRelation(passDAORelation)
'Define its properties.
With rel
'Specify the primary table.
.Table = passTablePrimary
'Specify the related table.
.ForeignTable = passTableForeign
'Specify attributes for cascading updates and deletes.
.Attributes = dbRelationUpdateCascade + dbRelationDeleteCascade
'Add the fields to the relation.
'Field name in primary table.
Set fld = .CreateField(passTableFldPrimary)
'Field name in related table.
fld.ForeignName = passTableFldForeign
'Append the field.
.Fields.Append fld
'Repeat for other fields if a multi-field relation.
End With
'Save the newly defined relation to the Relations collection.
db.Relations.Append rel
'Clean up
Set fld = Nothing
Set rel = Nothing
Set db = Nothing
Exit_CreateRelationshipsDAO:
Exit Sub
Err_CreateRelationshipsDAO:
MsgBox Err.description
Resume Exit_CreateRelationshipsDAO
End Sub