By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Creating relationships using VBA

P: n/a
I'm creating relationships between tables using VBA and ADOX. I can
create one-to-one relationships with an inner join, but I can't figure
out how to create these relationships with an outer join (specifically
a left outer join). I'm including the code that creates the
relationships with the inner join. Any help or suggestions would be
greatly appreciated.

Thanks!

<----- CODE FOLLOWS ----->

Function CreateRelationship()

Dim catDB As ADOX.Catalog
Dim tbl As ADOX.Table
Dim key As ADOX.key

Dim strForeignTbl As String
Dim strRelName As String
Dim strFTKey As String
Dim strRelatedTbl As String
Dim strRTKey As String

strForeignTbl = "theChildTable"
strRelName = "myRelationship"
strFTKey = "foreignTableKey"
strRelatedTbl = "parentTable"
strRTKey = "parentTableKey"

Set catDB = New ADOX.Catalog
catDB.ActiveConnection = CurrentProject.Connection

Set key = New ADOX.key
With key
.Name = strRelName
.RelatedTable = strRelatedTbl
.Type = adKeyForeign
.Columns.Append strFTKey
.Columns(strFTKey).RelatedColumn = strRTKey
End With

Set tbl = New ADOX.Table
Set tbl = catDB.Tables(strForeignTbl)
tbl.Keys.Append key

Set key = Nothing
Set tbl = Nothing
Set catDB = Nothing

End Function

Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
CurrentProject.Connection.Execute _
"ALTER TABLE Classes " _
& "ADD CONSTRAINT ClassSchool " _
& "FOREIGN KEY (SchoolID) " _
& "References Schools (SchoolID)"

Gandalf wrote:
I'm creating relationships between tables using VBA and ADOX. I can
create one-to-one relationships with an inner join, but I can't figure
out how to create these relationships with an outer join (specifically
a left outer join).

Nov 13 '05 #2

P: n/a
Gandalf,
I'm with Lyle. I'd write either a CREATE TABLE statement with my
constraints declared in it or an ALTER TABLE statement to change the table
design.
--
Alan Webb
kn*******@SPAMhotmail.com
"It's not IT, it's IS

"Gandalf" <Ga***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I'm creating relationships between tables using VBA and ADOX. I can
create one-to-one relationships with an inner join, but I can't figure
out how to create these relationships with an outer join (specifically
a left outer join). I'm including the code that creates the
relationships with the inner join. Any help or suggestions would be
greatly appreciated.

Thanks!

<----- CODE FOLLOWS ----->

Function CreateRelationship()

Dim catDB As ADOX.Catalog
Dim tbl As ADOX.Table
Dim key As ADOX.key

Dim strForeignTbl As String
Dim strRelName As String
Dim strFTKey As String
Dim strRelatedTbl As String
Dim strRTKey As String

strForeignTbl = "theChildTable"
strRelName = "myRelationship"
strFTKey = "foreignTableKey"
strRelatedTbl = "parentTable"
strRTKey = "parentTableKey"

Set catDB = New ADOX.Catalog
catDB.ActiveConnection = CurrentProject.Connection

Set key = New ADOX.key
With key
.Name = strRelName
.RelatedTable = strRelatedTbl
.Type = adKeyForeign
.Columns.Append strFTKey
.Columns(strFTKey).RelatedColumn = strRTKey
End With

Set tbl = New ADOX.Table
Set tbl = catDB.Tables(strForeignTbl)
tbl.Keys.Append key

Set key = Nothing
Set tbl = Nothing
Set catDB = Nothing

End Function

Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.