Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Build Relationships in VB Code

Status
Not open for further replies.

Francis

Programmer
Jan 26, 1999
124
GB
My application uses Access to store the users data.
When starting a new file it copies a 'Master Database' .
Sometimes I update (add tables, fields, Queries or relationships) the Master and then need to update the application files. I have written a routine to do this - it checks the pplication DB against the master, and if it finds tables, fields, Queries or relationship missing copies them into the application files. this works well, except for the some Relationships. If my new master has a relationship that has referential integrity it does not copy - I get the error message "Index already exists"
What is worse it seems to create the Relationship partially so that even in Access the same message appears when trying to manually add the Relationship. And I cannot even find (in Access) the partially added relationship to delete it.
Can anyone see what I am doing wrong?
Thanks
Here is the code for the Relationships part
'refDB is the master
'newdb id the DB that needs updating
For Each RefRel In refDB.Relations
Set NewRel = Nothing
Set NewRel = newdb.Relations(RefRel.Name)
If NewRel Is Nothing Then'need to add the relationship to the new DB
msg = msg & "Add Relationship " & RefRel.Name & vbCrLf
'copy all relationship details from the master to the new db
Set NewRel = newdb.CreateRelation(RefRel.Name, RefRel.Table, RefRel.ForeignTable, RefRel.Attributes)
For Each refField In RefRel.Fields
Set newField = NewRel.CreateField
newField.Name = refField.Name
newField.ForeignName = refField.ForeignName
NewRel.Fields.Append newField
Next
Err = 0
newdb.Relations.Append NewRel
If Err > 0 Then
RelError = True
eMsg = eMsg & RefRel.Name & " " & RefRel.Table & "/" & RefRel.ForeignTable & " " & Err.Description & vbCrlf
End If
End If
Next

 
Look into the indexes property of the table definition object. You might have to delete them and append new ones to the table definition objects.

 
Thanks
What I failed to mention in my first message was that I had copied all the indexes before copying the relationships. After some investigation, following the suggestion to look at the indexes I realised that creating Relationships can create indexes (duh). I modifed the routine so that only indexes that were not 'foriegn' were copied. This seems to have worked, thanks.
I cannot quite understand the logic behind this,I guess it is just a hangover from pre-object orientation. It seems that when you create a relationship between two tables, Access names them according to where it is in it's user interface - if you are defining a field it names them afetr the field, if you are defining a relationship it creates an index unless the table already has one. It cannot work out if the two are really the same.

Francis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top