473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to create relationships with DAO?

As part of an upgrade routine, I need to delete all relations, drop and
import some tables, then reapply the relationships.

The error I'm getting is:

Error Number 3366: Cannot append a relation with no fields defined.

How do I save, retrieve and apply the fields that belong to each
relationship?

Here's how I save the relations:

Public Function SaveRelations()
Dim db As DAO.Database
Dim rstRel As DAO.Recordset
Dim rstTbl As DAO.Recordset
Dim tdf As DAO.TableDef
Dim rel As DAO.Relation
Dim strTbl As String
Dim strSql As String
Set db = CurrentDb()
Set rstRel = db.OpenRecordset("tblRelations")
strSql = "DELETE * FROM tblRelations;" 'clear previous list
db.Execute strSql, dbFailOnError
db.Relations.Refresh
For Each rel In db.Relations
Debug.Print "Saving Relationship " & rel.Name
With rel
rstRel.AddNew
rstRel!rel = .Name
rstRel!tbl = .Table
rstRel!FrnTbl = .ForeignTable
rstRel!Att = .Attributes
rstRel.Update
End With
Next
End Function

Here's how I apply the relations:

Public Function ApplyRelations()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim rel As DAO.Relation
Dim fld As DAO.Field
Dim strRel As String
Dim strFrnTbl As String
Dim strFld As String
Dim strFrnFld As String
Set db = CurrentDb
Set rst = db.OpenRecordset("tblRelations")
Do While Not rst.EOF
'create relations
Set rel = db.CreateRelation(rst!rel, rst!tbl, _
rst!FrnTbl, rst!Att)
'set field attributes
For Each fld In rel.Fields
strFld = fld.Name
strFrnFld = fld.ForeignName
rel.Fields.Append rel.CreateField(strFld)
rel.Fields(strFld).ForeignName = strFrnFld
Next
db.Relations.Append rel '<<=== error here * * *
Loop
db.Relations.Refresh
End Function

Should I use a second table to store the fields that belong to each
relation? How would I retrieve and apply the field properties in my
ApplyRelations function?

Thanks in advance.
Nov 13 '05 #1
2 3163
On Mon, 15 Aug 2005 05:43:48 -0700, "deko" <de**@nospam.com> wrote:

In the FOR loop just above the error: you should loop over your saved
fields, not the fields in rel (which there are none).
Yes, there is a 1:M between Relationships and Fields, so you would be
wise to use two tables.

-Tom.
As part of an upgrade routine, I need to delete all relations, drop and
import some tables, then reapply the relationships.

The error I'm getting is:

Error Number 3366: Cannot append a relation with no fields defined.

How do I save, retrieve and apply the fields that belong to each
relationship?

Here's how I save the relations:

Public Function SaveRelations()
Dim db As DAO.Database
Dim rstRel As DAO.Recordset
Dim rstTbl As DAO.Recordset
Dim tdf As DAO.TableDef
Dim rel As DAO.Relation
Dim strTbl As String
Dim strSql As String
Set db = CurrentDb()
Set rstRel = db.OpenRecordset("tblRelations")
strSql = "DELETE * FROM tblRelations;" 'clear previous list
db.Execute strSql, dbFailOnError
db.Relations.Refresh
For Each rel In db.Relations
Debug.Print "Saving Relationship " & rel.Name
With rel
rstRel.AddNew
rstRel!rel = .Name
rstRel!tbl = .Table
rstRel!FrnTbl = .ForeignTable
rstRel!Att = .Attributes
rstRel.Update
End With
Next
End Function

Here's how I apply the relations:

Public Function ApplyRelations()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim rel As DAO.Relation
Dim fld As DAO.Field
Dim strRel As String
Dim strFrnTbl As String
Dim strFld As String
Dim strFrnFld As String
Set db = CurrentDb
Set rst = db.OpenRecordset("tblRelations")
Do While Not rst.EOF
'create relations
Set rel = db.CreateRelation(rst!rel, rst!tbl, _
rst!FrnTbl, rst!Att)
'set field attributes
For Each fld In rel.Fields
strFld = fld.Name
strFrnFld = fld.ForeignName
rel.Fields.Append rel.CreateField(strFld)
rel.Fields(strFld).ForeignName = strFrnFld
Next
db.Relations.Append rel '<<=== error here * * *
Loop
db.Relations.Refresh
End Function

Should I use a second table to store the fields that belong to each
relation? How would I retrieve and apply the field properties in my
ApplyRelations function?

Thanks in advance.


Nov 13 '05 #2
> In the FOR loop just above the error: you should loop over your saved
fields, not the fields in rel (which there are none).
Yes, there is a 1:M between Relationships and Fields, so you would be
wise to use two tables.


This seems to do the trick - the foreign fields are always the same name as
the PK

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim rel As DAO.Relation
Set db = CurrentDb
Set rst = db.OpenRecordset("tblRelations")
Do While Not rst.EOF
Set rel = db.CreateRelation(rst!RelName, rst!RelTbl, _
rst!FrnTbl, rst!RelAtt)
rel.Fields.Append rel.CreateField(rst!RelFld)
rel.Fields(rst!RelFld).ForeignName = rst!RelFld
db.Relations.Append rel
db.Relations.Refresh 'not sure if I need this
rst.MoveNext
Loop

Thanks for the help.
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brian S. Smith | last post by:
Hi gang, Please help. I've been through the Access help, searched the Web, and I can't seem to get a straight answer. As the Subject line suggests, I want to run a fairly simple VB/Access Sub...
2
by: Max | last post by:
Hi. I really hope someone can help me. Going slowly insane with this problem. I have a two Access 2000 databases. One is the backend containing tables and some admin queries. The other is the...
7
by: sea | last post by:
Is it a good idea to programatically create a primary key? For example in a table called names, I have the following fields, (1) firstname (2)lastname (3) ID - will it be ok to create a primary...
2
by: Mindy | last post by:
Hey, I want to create links between my two tables. The primary keys of these two tables are character variables, with length =11. I followed exactly the instruction of Dummuy Book for ACCESS...
3
by: teedilo | last post by:
Our MS SQL (SQL Server 2000) DBA has database privileges locked down pretty tightly. We end users/developers do not have administrator privileges for most databases. That arrangement has worked...
6
by: Raji16 | last post by:
Hi, I am a new member. i am designing a simple judicial database system. however after creating tables i am a bit confused on setting the relationships between tables :confused: here is the link...
45
by: salad | last post by:
I'm curious about your opinion on setting relationships. When I designed my first app in Access I'd go to Tools/Relationships and set the relationships. Over time I'd go into the window and see...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
0
by: Shootah | last post by:
Hi, I have succeeded in adding automated relationships with refference tables after importing an excel file created from a query to an Access database. However I have the following problem: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.