Solved

Access VBA - table relationships

Posted on 2013-12-09
3
829 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 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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 the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

829 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