Computer Learning Zone CLZ Access Excel Word Windows

If you can't take it with you, don't go.

-98th Rule of Acquisition
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Visitor Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
creating relationships with vba
Fred Zwalm 

3 years ago
I need to set relationships using vba. However in Access 365 the DAO lib is not working in reference. So I comment out the DIM and then it seems to work. However, the line ".Attributes = dbRelationUpdateCascade + dbRelationDeleteCascade" is NOT working. No error, but no cascading. How can I put then on as well?
And if you have a better option by NOT using DAO of some other trick, that would be great. Hier is the code:

Sub CreateRelationDAO()
Dim db As DAO.Database
Dim rel As DAO.Relation
Dim fld As DAO.Field

'Initialize
Set db = CurrentDb()

'Create a new relation.
Set rel = db.CreateRelation("MyMainTableMyRelatedTable")

'Define its properties.
With rel
'Specify the primary table.
.Table = "MyMainTable"
'Specify the related table.
.ForeignTable = "MyRelatedTable"
'Specify attributes for cascading updates and deletes.
.Attributes = dbRelationUpdateCascade + dbRelationDeleteCascade

'Add the fields to the relation.
'Field name in primary table.
Set fld = .CreateField("MyMainTableID")
'Field name in related table.
fld.ForeignName = "MyForeignKeyID"
'Append the field.
.Fields.Append fld

'Repeat for other fields if a multi-field relation.

End With

'Save the newly defined relation to the Relations collection.
db.Relations.Append rel

'Clean up
Set fld = Nothing
Set rel = Nothing
Set db = Nothing
Debug.Print "Relation created."
End Sub
Kevin Robertson  @Reply  
          
3 years ago
Sounds like you may be missing a reference.

Go to Tools--References and make sure this reference is selected:
Microsoft Access 16.0 Access database engine Object Library

I have tested you code and it worked for me.

A few things to consider
I made some changes so that nothing was hard coded in the VBA (Everything was sent from form fields).
I also added some error handling.
Kevin Robertson  @Reply  
          
3 years ago

Alex Hedley  @Reply  
            
3 years ago
Kevin You should add all these code examples to gists
Kevin Robertson  @Reply  
          
3 years ago
Thanks Alex I'll look in to that.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Visitor Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 6/16/2026 12:25:12 PM. PLT: 0s