Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2003
    Posts
    8

    Red face Unanswered: Create Relationship with Module Code

    Hi there

    Is it possible to create and delete relationships using module code in Access 2002/XP?

    I have to regularly set up some code that will import information into a table in a database. If the table exists, I need to delete any relationships and delete the table.

    Then import the new data into a table with the same name as the deleted one and recreate the relationships

    Any thoughts how this could be done?????

    Nevermind I just worked out that it would be much easier to do with an Update Query *bangs head on desk*
    Last edited by RascalBird; 11-29-04 at 22:49. Reason: Having A Blonde Moment

  2. #2
    Join Date
    Dec 2003
    Posts
    268

    DAO Example

    Here is something I was able to find
    http://msdn.microsoft.com/library/de...eltoAccess.asp
    look at figures 5 and 6



    Dim dbsBuild As DAO.Database
    Dim relatNew
    As DAO.Relation
    Set dbsBuild =
    CurrentDb
    Set relatNew =
    dbsBuild.CreateRelation("MyRelationship", _
    "tlkp_ContactType", "contacts",
    dbRelationUpdateCascade)

    ' Create a field in
    relation object before
    ' defining the name of
    the external field.
    With
    relatNew
    .Fields.Append
    .CreateField("ContactType")
    .Fields!ContactType.ForeignName
    = "ContactType"
    dbsBuild.Relations.Append
    relatNew
    End With

    On Error Resume Next
    ' Delete the relationship if it exists.
    Set dbsBuild = CurrentDb With dbsBuild
    .Relations.Delete "MyRelationship"
    .Close
    End With
    ' Delete the table in case it exists.
    DoCmd.RunSQL "Drop table tlkp_ContactType"

    On Error GoTo 0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •