Solved

Access VBA - table relationships

Posted on 2013-12-09
3
843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 36

Assisted Solution

by:PatHartman
PatHartman earned 200 total points
ID: 39706348
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
ID: 39706389
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
ID: 39707959
Thanks very much
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

733 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