Results 1 to 11 of 11
  1. #1
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65

    VBA to delete all relationships

    Hello all!

    I need to automatically delete all the relationships in my database and I've found on internet a code that returns:
    Code:
    Run-time error '3211' 
    The database engine could not lock the table 'X' because 
    it is already in use by another user or process.
    The code is:


    Code:
    Private Sub Command39_Click()
    Dim db As Database
    Dim rex As Relations
    Set db = CurrentDb()
    Set rex = db.Relations
    Do While rex.Count > 0
    Debug.Print rex(0).Name
    rex.Delete rex(0).Name
    Loop
    End Sub
    I have no table in use. The problem could be the multiple relationships that I have from table 'X' to other tables.
    Is there any way to improve this code?

    Thx!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,728
    Please give the link for where you found the code.

    See this for info

  3. #3
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,728
    Please show us ALL of the code you are using.

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Try code something like this:

    Code:
    Dim r As Relation
     For Each r In CurrentDb.Relations
       If Left(r.Name, 4) <> "MSys" Then
         Debug.Print r.Name
         CurrentDb.Relations.Delete (r.Name)
       End If
     Next
    I gave it a try in my sandbox db and it seemed to be OK.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,728
    bob,

    I agree with JohnG's code, and the full code sample shown by Allen Browne. But, can you tell us why you want to delete all existing relations? Just curious.

  7. #7
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    Thank you John!

    I've tried your code, but I get exactly the same error.
    Don't know why

    Thank you Orange for all the info.
    Quite hard to explain the reason

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Where are you running this code? If you are running it from a form, and the form has a recordsource (table or query) defined, then you do have a table open, as part of the form operations.

    Try putting the code in a standard module (i.e. not in a form), then run it from the immediate pane (Ctrl_G) with no forms open.

  9. #9
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    I'm running it from a button placed on the main menu form, which doesn't have any recordsource defined.

  10. #10
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    You're right John. It works placed in a module.
    And if I want it in a button, I have to close the form first.

    So, your code will be:

    Code:
    Private Sub Command38_Click()
    DoCmd.Close acForm, Me.Name
    Dim r As Relation
     For Each r In CurrentDb.Relations
       If Left(r.Name, 4) <> "MSys" Then
         Debug.Print r.Name
         CurrentDb.Relations.Delete (r.Name)
       End If
    Next
    End Sub

  11. #11
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I was able to replicate your 3211 error by running the code with a form open (but with the code in a standard module), and the error referenced the table I expected it to.

    Is there any code in your Main Menu form that might open a recordset, or otherwise work with a table or query? If there is a lot of code in the form, you could do a text search for the name of the table listed in the error message. Does your main form have a combo box or list box on it? Those have rowsources wich usually reference tables or queries.

    I am almost certain the problem is in your form somewhere, because I was able to successfully run my code from a form button.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Delete does not delete records in evey table
    By LaughingBull in forum Access
    Replies: 5
    Last Post: 09-01-2015, 04:05 PM
  2. Replies: 13
    Last Post: 05-06-2014, 12:42 PM
  3. Replies: 5
    Last Post: 02-19-2014, 11:50 AM
  4. Explicit Relationships and Implicit Relationships
    By Dazza666 in forum Database Design
    Replies: 2
    Last Post: 07-17-2013, 02:11 AM
  5. Replies: 2
    Last Post: 01-24-2012, 02:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums