T
tpcolson
I'm not any sort of programmer at all, and I managed to cobble
together the following bits of code in order to create an index and a
relationship attached to two buttons. I need for the two functions to
somehow be merged into one function and attached to formopen? Form
open allready has a good bit of vba attached to it, not sure how I'd
nest additional functions in it. One of the functions is private, one
is public, not sure how to change the syntax of the public one to run
as "private". Thanks!
Private Sub CreateIndex_Click()
Dim dbs As Database
Set dbs = CurrentDb
dbs.Execute "CREATE UNIQUE INDEX Location_ID " _
& "ON tbl_Locations (Location_ID) " _
& "WITH PRIMARY;"
dbs.Close
End Sub
Option Compare Database
' =================================
' Create 1 to m relationship from tbl_Locations to tbl_Events
Public Function CreateRelationship_Click()
Dim db As DAO.Database
Dim tdf1 As DAO.TableDef
Dim tdf2 As DAO.TableDef
Dim rels As DAO.Relations
Dim rel As DAO.Relation
Set db = CurrentDb
Set tdf1 = db.TableDefs("tbl_Locations")
Set tdf2 = db.TableDefs("tbl_Events")
Set rels = db.Relations
For Each rel In rels
If rel.Name = "myRelationship" Then
rels.Delete ("myRelationship")
End If
Next
Set rel = db.CreateRelation("myRelationship",
tdf1.Name, tdf2.Name, dbRelationUpdateCascade +
dbRelationDeleteCascade)
rel.Fields.Append rel.CreateField("Location_ID")
rel.Fields("Location_ID").ForeignName = "Location_ID"
rels.Append rel
Set rels = Nothing
Set tdf = Nothing
Set tdf2 = Nothing
Set db = Nothing
End Function
together the following bits of code in order to create an index and a
relationship attached to two buttons. I need for the two functions to
somehow be merged into one function and attached to formopen? Form
open allready has a good bit of vba attached to it, not sure how I'd
nest additional functions in it. One of the functions is private, one
is public, not sure how to change the syntax of the public one to run
as "private". Thanks!
Private Sub CreateIndex_Click()
Dim dbs As Database
Set dbs = CurrentDb
dbs.Execute "CREATE UNIQUE INDEX Location_ID " _
& "ON tbl_Locations (Location_ID) " _
& "WITH PRIMARY;"
dbs.Close
End Sub
Option Compare Database
' =================================
' Create 1 to m relationship from tbl_Locations to tbl_Events
Public Function CreateRelationship_Click()
Dim db As DAO.Database
Dim tdf1 As DAO.TableDef
Dim tdf2 As DAO.TableDef
Dim rels As DAO.Relations
Dim rel As DAO.Relation
Set db = CurrentDb
Set tdf1 = db.TableDefs("tbl_Locations")
Set tdf2 = db.TableDefs("tbl_Events")
Set rels = db.Relations
For Each rel In rels
If rel.Name = "myRelationship" Then
rels.Delete ("myRelationship")
End If
Next
Set rel = db.CreateRelation("myRelationship",
tdf1.Name, tdf2.Name, dbRelationUpdateCascade +
dbRelationDeleteCascade)
rel.Fields.Append rel.CreateField("Location_ID")
rel.Fields("Location_ID").ForeignName = "Location_ID"
rels.Append rel
Set rels = Nothing
Set tdf = Nothing
Set tdf2 = Nothing
Set db = Nothing
End Function