Solved

Access VBA - table relationships

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

What VBA code would I use to find the table relationships in Access?
0
Comment
Question by:Murray Brown
[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 38

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 58

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:Murray Brown
ID: 39707959
Thanks very much
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

635 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