• Manipulating Table Relationships (Access 97)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Manipulating Table Relationships (Access 97)

    Author
    Topic
    #364345

    Good evening loungers

    I am attempting to delete and then recreate a realtionship.
    This code pretty much is flogged from Access help.

    Code is falling over at appending the newly recreated relationship.

    Error msg states that object cannot be found.

    Any assistance greatly appreciated.

    Merry christmas

    Geof

    >>>>>>>>>>>>
    Sub relationsTest()
    Dim db As Database
    Dim rel As Relation
    Dim fld As Field
    Set db = CurrentDb
    For Each rel In db.Relations
    If rel.Table = “tblSiteAssessments” And rel.ForeignTable = “tblFaunaFlora” Then
    db.Relations.Delete rel.Name
    Exit For
    End If
    Next rel
    ‘ Create new relationship and set its properties.
    Set rel = db.CreateRelation(“tblSiteAssessments”, “tblFaunaFlora”)
    ‘ Set Relation object attributes to enforce referential integrity.
    rel.Attributes = dbRelationDeleteCascade

    ‘ Create field in Fields collection of Relation object.

    Set fld = rel.CreateField(“SurveyNumber”)
    ‘ Provide name of foreign key field.
    fld.ForeignName = “SurveyNumber”
    ‘ Append field to Relation object and Relation object to database.
    rel.Fields.Append fld
    db.Relations.Append rel
    MsgBox “Relation ‘” & rel.Name & “‘ created.”

    Debug.Print “Attributes of relations in ” & db.Name & “:”
    For Each rel In db.Relations
    Debug.Print ” ” & rel.Name & ” = ” & rel.Attributes

    Next rel
    db.Close
    Set db = Nothing

    End Sub

    >>>>>>

    Viewing 0 reply threads
    Author
    Replies
    • #559030

      Seems to me that you forget the name of the relationship:

      Set rel = db.CreateRelation(“NameOfRelationship”,”tblSiteAssessments”, “tblFaunaFlora”)

      but I haven’t test it.

      • #559154

        Good morning Francois
        Thank you
        Dohl!!!
        It was that simple. Working fine now.
        The perils of working when tired.

        Merry christmas

        Geof

    Viewing 0 reply threads
    Reply To: Manipulating Table Relationships (Access 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: