For source control I am trying to export the table relations from Access in order to be able to re-import them. From what I can tell in order to do this I only need four pieces of information.
I got the first three down, but having an issue figuring out the last bit of information. Usually my tables and field names are always the same so it is generally not a problem but I would really like if this vbscript can account for times in the future where maybe perhaps it is not as evident. Here is a snippit of the code I am using:
Dim db, totalrelations, i, j
Set db = oApplication.CurrentDb
totalrelations = db.Relations.Count
WScript.Echo totalrelations
If totalrelations > 0 Then
WScript.Echo "opening " & totalrelations
For i = totalrelations - 1 To 0 Step -1
'WScript.Echo (db.Relations(i).Name)
'WScript.Echo (db.Relations.Item(i).name)
WScript.Echo "Attributes: " & db.Relations.Item(i).Attributes
'** 'iterate through the count!!!
WScript.Echo "Field count: " & db.Relations.Item(i).Fields.Count
If db.Relations.Item(i).Fields.Count > 0 Then
For j = 0 To db.Relations.Item(i).Fields.Count - 1
WScript.Echo j & " Field name: " & db.Relations.Item(i).Fields.Item(j).Name
Next
End If
WScript.Echo "ForeignTable: " & db.Relations.Item(i).ForeignTable
WScript.Echo "Name: " & db.Relations.Item(i).Name
WScript.Echo "PartialReplica: " & db.Relations.Item(i).PartialReplica
WScript.Echo "propertycount: " & db.Relations.Item(i).Properties.Count
'Simply repeats everything we already know
If db.Relations.Item(i).Properties.Count > 0 Then
For j = 0 To db.Relations.Item(i).Properties.Count -1
WScript.echo j & " PropertyName:" & db.Relations.Item(i).Properties.Item(j).Name
WScript.echo j & " PropertyValue:" & db.Relations.Item(i).Properties.Item(j).Value
Next
End if
WScript.Echo "Table: " & db.Relations.Item(i).Table
Next
End If
I had assumed that I could be clever and check the field counts and iterate through those. However the joke is on me as I made a relationship between two fields that do not share the same name and the field count remains 1. Thus my cleverness did not reveal to me the secret of mana :P Does anybody know how I can coax this information out?
You have this ...
" Field name: " & db.Relations.Item(i).Fields.Item(j).Name
To retrieve the name of the related field in the foreign table, use this:
" Foreign Field name: " & db.Relations.Item(i).Fields.Item(j).ForeignName
Actually you don't need to explicitly include .Item()
to retrieve the ith or jth member of each collection. So I would shorten it to this:
" Foreign Field name: " & db.Relations(i).Fields(j).ForeignName