0
我有一張表,我想鏈接其他三張表,我意識到這是不可能的,因此我決定找到一種方法來處理VBA。我抓住了一些代碼,我真的希望將工作,並已編輯它,使之與我的具體需求根據Access中的字段值更改關係
Option Compare Database
Function changeRel()
If Players.Cond = "Quest" Then
Dim dbs As DAO.Database
Dim rel As DAO.Relation
Set dbs = CurrentDb
Set rel = dbs.CreateRelation("QuestRel", "Players", "Player Quests", dbRelationDeleteCascade)
rel.Fields.Append rel.CreateField("PlayerID")
rel.Fields("PlayerID").ForeignName = "PQuestID"
dbs.Relations.Append rel
dbs.Relations.Refresh
ElseIf Players.Cond = "Level" Then
Dim dbs As DAO.Database
Dim rel As DAO.Relation
Set dbs = CurrentDb
Set rel = dbs.CreateRelation("LevelRel", "Players", "Player Levels", dbRelationDeleteCascade)
rel.Fields.Append rel.CreateField("PlayerID")
rel.Fields("PlayerID").ForeignName = "PLevelID"
dbs.Relations.Append rel
dbs.Relations.Refresh
ElseIf Players.Cond = "Stat" Then
Dim dbs As DAO.Database
Dim rel As DAO.Relation
Set dbs = CurrentDb
Set rel = dbs.CreateRelation("StatRel", "Players", "Player Stats", dbRelationDeleteCascade)
rel.Fields.Append rel.CreateField("PlayerID")
rel.Fields("PlayerID").ForeignName = "PStatsID"
dbs.Relations.Append rel
dbs.Relations.Refresh
Else
'Do nothing
End Function
我從來沒有在VBA之前編碼工作,我不知道是什麼Option Compare Database
手段,它是就在那裏。我很確定我需要一個子程序,儘管我對它是什麼感到困惑。
有人能告訴我如何在每次單元格Players.Cond
的值發生變化時運行此函數嗎?
[上docs.SO選項關鍵字(http://stackoverflow.com/documentation/vba/3992/vba-option -keyword/13937/option-compare-binary-text-database#t = 20161121223654120041) –
您不應該根據數據值定期更改數據庫關係。表關係維護數據庫的結構完整性,並應在數據庫設計期間僅調整一次。否則你可能會破壞很多應用程序邏輯和記錄管理。 – Parfait