473,473 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Recreating relationships in code

Hopefully someone out there can help. I am currently trying to write
some code to allow me to delete a table, then recreate it and
re-establish the relationships. I seem to have hit a snag in the
recreation of the relationships. Code is:
Sub Woof()
Dim dbs As Database
Dim fld As Field, rel1 As Relation, rel2 As Relation
Set dbs = CurrentDb

Set rel1 = dbs.CreateRelation("Emp No", "Upload", "FTE Table")
rel1.Attributes = dbRelationDeleteCascade +
dbRelationUpdateCascade
Set fld = rel1.CreateField("Emp No")
fld.ForeignName = "Emp No"
rel1.Fields.Append fld
dbs.Relations.Append rel1
MsgBox "Relation '" & rel1.Name & "' created."
Set dbs = Nothing

Set dbs = CurrentDb
Set rel2 = dbs.CreateRelation("Cost Centre", "Department",
"FTE Table")
rel2.Attributes = dbRelationDeleteCascade +
dbRelationUpdateCascade
Set fld = rel2.CreateField("DepartmentCostCentre")
fld.ForeignName = "Cost Centre"
rel2.Fields.Append fld
dbs.Relations.Append rel2
MsgBox "Relation '" & rel2.Name & "' created."
Set dbs = Nothing

End Sub

The code gets as far as dbs.Relations.Append rel1 and then errors out
with a message "Can't append a relation without fields defined". I
have tried a few different configurations of the field definition
section but i seem to be missing something. As this is the first time
I have tried this piece of code i'm not one hundred percent sure what
is causing the error.

Can anyone help?

Thanks

Jenni
Nov 12 '05 #1
4 5061
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In Access 2K & >, I use the JET DDL (Data Definition Language) to
create tables & relationships at the same time. This seems easier to
me than using the DAO/ADO methods. E.g.:

dim strSQL as string

strSQL = "CREATE TABLE myTable ( " & _
"FirstName VARCHAR(25) NOT NULL , " & _
"SSN VARCHAR(9) PRIMARY KEY, " & _
"EmployeeType INTEGER NOT NULL " & _
" REFERENCES EmpTypes ON UPDATE CASCADE ON DELETE CASCADE)"

CurrentDB.Execute strSQL, dbFailOnError

This will create a table "myTable" with a Primary Key on the SSN
column and a relationship (Foreign Key) between column EmployeeType in
table myTable and column EmployeeType in table EmpTypes (table
EmpTypes must exist before this CREATE TABLE statement is run). What
happens on the Delete and Update actions is defined by the "ON DELETE"
AND "ON UPDATE" clauses. See the Access Help articles "CONSTRAINT
clause" and "CREATE TABLE" for more info.

The REFERENCES statement (a CONSTRAINT) will only work in Access 2000
& greater when the Tools > Options > Tables/Query [Tab] : SQL Server
Compatible Syntax (ANSI-92) - "This database" Check Box is checked.

HTH,

MGFoster:::mgf
Oakland, CA (USA)
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBP8ZRwIechKqOuFEgEQIDBgCfbL0UU1tltOunQ7/jo4fkfU40/MUAoJmx
xV6+Q01OeFvA75kpzzxKPoa+
=OEho
-----END PGP SIGNATURE-----

Jenni wrote:
Hopefully someone out there can help. I am currently trying to write
some code to allow me to delete a table, then recreate it and
re-establish the relationships. I seem to have hit a snag in the
recreation of the relationships. Code is:
Sub Woof()
Dim dbs As Database
Dim fld As Field, rel1 As Relation, rel2 As Relation
Set dbs = CurrentDb

Set rel1 = dbs.CreateRelation("Emp No", "Upload", "FTE Table")
rel1.Attributes = dbRelationDeleteCascade +
dbRelationUpdateCascade
Set fld = rel1.CreateField("Emp No")
fld.ForeignName = "Emp No"
rel1.Fields.Append fld
dbs.Relations.Append rel1
MsgBox "Relation '" & rel1.Name & "' created."
Set dbs = Nothing

Set dbs = CurrentDb
Set rel2 = dbs.CreateRelation("Cost Centre", "Department",
"FTE Table")
rel2.Attributes = dbRelationDeleteCascade +
dbRelationUpdateCascade
Set fld = rel2.CreateField("DepartmentCostCentre")
fld.ForeignName = "Cost Centre"
rel2.Fields.Append fld
dbs.Relations.Append rel2
MsgBox "Relation '" & rel2.Name & "' created."
Set dbs = Nothing

End Sub

The code gets as far as dbs.Relations.Append rel1 and then errors out
with a message "Can't append a relation without fields defined". I
have tried a few different configurations of the field definition
section but i seem to be missing something. As this is the first time
I have tried this piece of code i'm not one hundred percent sure what
is causing the error.

Can anyone help?

Thanks

Jenni

Nov 12 '05 #2
jc******@stroudandswindon.co.uk (Jenni) wrote in
news:9f**************************@posting.google.c om:
Set fld = rel2.CreateField("DepartmentCostCentre")
fld.ForeignName = "Cost Centre"
rel2.Fields.Append fld
dbs.Relations.Append rel2
MsgBox "Relation '" & rel2.Name & "' created."
Set dbs = Nothing


Maybe

Set fld = rel2.CreateField("DepartmentCostCentre")
rel2.Fields.Append fld
rel2.Fields("DepartmentCostCentre").ForeignName = "Cost Centre"
dbs.Relations.Append rel2
MsgBox "Relation '" & rel2.Name & "' created."
Set dbs = Nothing
--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #3
TC
You will need to append >two< fields to the relation before you can append
the relation?

HTH,
TC
"Jenni" <jc******@stroudandswindon.co.uk> wrote in message
news:9f**************************@posting.google.c om...
Hopefully someone out there can help. I am currently trying to write
some code to allow me to delete a table, then recreate it and
re-establish the relationships. I seem to have hit a snag in the
recreation of the relationships. Code is:
Sub Woof()
Dim dbs As Database
Dim fld As Field, rel1 As Relation, rel2 As Relation
Set dbs = CurrentDb

Set rel1 = dbs.CreateRelation("Emp No", "Upload", "FTE Table")
rel1.Attributes = dbRelationDeleteCascade +
dbRelationUpdateCascade
Set fld = rel1.CreateField("Emp No")
fld.ForeignName = "Emp No"
rel1.Fields.Append fld
dbs.Relations.Append rel1
MsgBox "Relation '" & rel1.Name & "' created."
Set dbs = Nothing

Set dbs = CurrentDb
Set rel2 = dbs.CreateRelation("Cost Centre", "Department",
"FTE Table")
rel2.Attributes = dbRelationDeleteCascade +
dbRelationUpdateCascade
Set fld = rel2.CreateField("DepartmentCostCentre")
fld.ForeignName = "Cost Centre"
rel2.Fields.Append fld
dbs.Relations.Append rel2
MsgBox "Relation '" & rel2.Name & "' created."
Set dbs = Nothing

End Sub

The code gets as far as dbs.Relations.Append rel1 and then errors out
with a message "Can't append a relation without fields defined". I
have tried a few different configurations of the field definition
section but i seem to be missing something. As this is the first time
I have tried this piece of code i'm not one hundred percent sure what
is causing the error.

Can anyone help?

Thanks

Jenni

Nov 12 '05 #4
Thanks for the suggestions, I have taken them all on board and will
try them out. At the moment I'm on a bit of a time constraint and I
have managed to get them going by using them in an Even Proceedure
rather than calling them from a function - who knows why!

Version 2 will definately contain your suggestions.

Thanks

Jenni
Nov 12 '05 #5

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

Similar topics

7
by: Andante.in.Blue | last post by:
Hello everyone! I've been working with a problematic legacy database for a while. While I am still fairly new to Access, the more I work with the database, the more problems I've uncovered. ...
49
by: Mal | last post by:
Hi, As I gain knowledge through a lot of trial, error, and usenet posts.. I have a potentially odd question. I am using a commercial access application. It is a front-end / back...
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...
5
by: Mike Turco | last post by:
What is the difference between creating relationships in the front-end vs. the back-end database? I was trying to create a relationship in a database front-end and noticed that I could not check...
3
by: Tom van Stiphout | last post by:
I have written some code to export all objects to text files, and to import those files back into a new database. Hopefully this will eliminate some forms of corruption. The icing on the cake...
7
by: davegb | last post by:
I'm totally new to relational database design. My boss has asked me to create a database of information on the employees in our group. Seemed to me like a simple application to learn the ropes. A...
10
by: Dixie | last post by:
I need to delete some relationships in code. How do I know what the names of those relationships are?
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...
13
by: ARC | last post by:
Hello all, Prior to going live with my app, I have questions on relationships theory. My prior app was done in Access 97, and I did NOT use relationships at all. I have 65 tables in my...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.