刪除Access中的所有關繫

2017-12-20 17:01:00
黃善超
原創
711

説到關繫,可能有兩種意見。正方認爲關繫好用,可以約束數據,減少錯誤。對於新手有很好的幫助。反方覺得沒必要建立關繫。因爲數據庫的建立本身就是基於關繫數據庫的。建立關繫反而會限製一些操作。今天我們不把這箇作爲討論的重點。


有沒有人和小編一樣覺得逐箇刪除關繫是一件很麻煩的事情。如何批量刪除關繫呢?


運行下麵的代碼:

Function DeleteAllRelationships() As String
 '作者:Allen Browne
     Dim db As dao.Database      ' Current DB
     Dim rex As Relations    ' Relations of currentDB.
     Dim rel As Relation     ' Relationship being deleted.
     Dim iKt As Integer      ' Count of relations deleted.
     Dim sMsg As String      ' MsgBox string.
 
    sMsg = "是否刪除當前數據庫中的所有關繫" & vbCrLf
     If MsgBox(sMsg, vbQuestion + vbYesNo + vbDefaultButton2, "Are you sure?") = vbNo Then
         DeleteAllRelationships = "Operation cancelled"
         Exit Function
     End If
 
    Set db = CurrentDb()
     Set rex = db.Relations
     iKt = rex.Count
     Do While rex.Count > 0
         Debug.Print rex(0).Name
         rex.Delete rex(0).Name
     Loop
     DeleteAllRelationships = iKt & " relationship(s) deleted"
 End Function



提醒是否刪除關繫


選擇“是”,刪除所有關繫


註意:此代碼有一定的危險性,請謹慎使用。牠會刪除當前數據庫中的所有關繫。

    分享