CreateRelation method

I know that access doesn't support the CreateRelation method on linked tables but I need to dynamically link and set relations from a table currently in the database to the linked table.  I've got the table linked so is there any other way to programatically set the relationship between these two tables?
pvegaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peter57rCommented:
Hello pvega,

> I know that access doesn't support the CreateRelation method on linked tables
?Who told you that then?(:-)

You can't enforce referential integrity with linked tables but that's all


This is based on code I pinched from the net sometime ago - I don't know the original author.

Sub CreateRelationDAO()
    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("EmployeePO")

    'Define its properties.
    With rel
        'Specify the primary table.
        .Table = "Employees"  '******* in this database
        'Specify the related table.
        .ForeignTable = "Purchase Orders" ' ********linked table
        'Specify attributes for cascading updates and deletes.
        .Attributes = dbRelationDontEnforce

        'Add the fields to the relation.
        'Field name in primary table.
        Set fld = .CreateField("EmployeeID")
        'Field name in related table.
        fld.ForeignName = "employeeID"
        '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
    msgbox "Relation created."
End Sub


Pete
0
pvegaAuthor Commented:
Hey Pete,

Modified and ran the code with no errors but I don't see a relationship between the tables.  I'm looking in relationships window.  Thanks.

Pam
0
peter57rCommented:
The relationship window only shows what you put there.
Right-click and Show All.

Pete
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.