Solved

Access VBA - table relationships

Posted on 2013-12-09
3
802 Views
Last Modified: 2013-12-10
Hi

What VBA code would I use to find the table relationships in Access?
0
Comment
Question by:murbro
3 Comments
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 200 total points
Comment Utility
You could create a query that reads the MSysRelationships table.

SELECT MSysRelationships.*
FROM MSysRelationships;
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 300 total points
Comment Utility
You'd want to access the relations object, which belongs to the database object.  Sample DAO code from the on-line help below.

Jim.

Sub RelationX()

      Dim dbsNorthwind As Database
      Dim rstProducts As Recordset
      Dim prpLoop As Property
      Dim fldLoop As Field
      Dim errLoop As Error

      Set dbsNorthwind = OpenDatabase("Northwind.mdb")
      Set rstProducts = dbsNorthwind.OpenRecordset("Products")

      ' Print a report showing all the different parts of
      ' the relation and where each part is stored.
      With dbsNorthwind.Relations!CategoriesProducts
            Debug.Print "Properties of " & .Name & " Relation"

Debug.Print "    Table = " & .Table
            Debug.Print "    ForeignTable = " & .ForeignTable
            Debug.Print "Fields of " & .Name & " Relation"
            With .Fields!CategoryID
                  Debug.Print "    " & .Name
                  Debug.Print "        Name = " & .Name
                  Debug.Print "        ForeignName = " & .ForeignName
            End With
      End With

      ' Attempt to add a record that violates the relation.
      With rstProducts
            .AddNew
            !ProductName = "Trygve's Lutefisk"

!CategoryID = 10
            On Error GoTo Err_Relation
            .Update
            On Error GoTo 0
            .Close
      End With

      dbsNorthwind.Close
      
      Exit Sub
      
Err_Relation:

      ' Notify user of any errors that result from
      ' the invalid data.
      If DBEngine.Errors.Count > 0 Then
            For Each errLoop In DBEngine.Errors
                  MsgBox "Error number: " & errLoop.Number & _
                        vbCr & errLoop.Description
            Next errLoop
      End If
      
      Resume Next

End Sub
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now