Lotus Notes/Domino How To Document

主 題 如何在Notes 資料庫中作文件鎖定,防止被同時修改
發表人 Jeffrey Wu.
E-mail wsm00@pchome.com.tw

內     容

**** 在資料庫中建立以下設計元件

1. Create New Form ( FormName = FrmLock)



2. Create a New View , ViewName = DocLock , And Sort By DocNo (Desending)



**** 以下在要作文件鎖定控制之表單上增加


1 . 在表單 Global Declaration 中宣告以下變數

Dim LockFlag As Integer ' 表示正由自己或別人鎖定中
Dim LockByMe As Integer ' 表示正由自己鎖定中 , 在QueryClose 時清除
Dim LockTime As Variant ' 儲存鎖定文件之時間 , 於 PostOpen 時取得


2. 在 Form PostOpen Event 中加入下段程式 (取得文件開啟當時, 鎖定文件之最後異動時間)

Dim sn As New NotesSession
Dim db As NotesDatabase
Dim View As NotesView
Dim Doc As NotesDocument
Dim Sno As String
Dim UName As String

'//取得現用資料庫
SNo = Source.Document.SNo(0)
Set db = sn.CurrentDatabase
Set View = Db.GetView("DocLock")
UName = sn.CommonUserName

'//取得鎖定文件,如果文件不存在, 則重新建立新的鎖定文件

Set Doc = View.GetDocumentByKey(SNo)
If Doc Is Nothing Then
Set Doc = db.CreateDocument
Doc.Form = "frmLock"
Doc.DocNo = SNo
Doc.Locker = UName
Doc.AppendItemValue "$PublicAccess", "1"
Doc.LockTime = Now()
Doc.Save True, True

LockFlag = True
LockByMe = True
End If

LockTime = Doc.LastModified


3. 在 Form QueryModeChange Event 中加入下段程式 (取得文件開啟當時 , 鎖定文件之最後異動時間)

Dim sn As New NotesSession
Dim db As NotesDatabase
Dim View As NotesView

Dim Doc As NotesDocument
Dim Sno As String
Dim strMsg As String
Dim UName As String
Dim NewLockTime As Variant

'//SNo 是筆者設定的文件編號欄位, 用來作為識別鎖定文件的 Key 值, 各位可以斟酌參考運用
SNo = Source.Document.SNo(0)

'//取得現用資料庫
Set db = sn.CurrentDatabase

Set View = Db.GetView("DocLock")
Set Doc = View.GetDocumentByKey(SNo)
UName = sn.CommonUserName

'//取得鎖定文件之最後異動時間
NewLockTime = Doc.LastModified

If NewLockTime <> LockTime Then
Msgbox "文件已被其他人員修改過 , 請離開重新進入" , 16 , "訊息"
Continue = False
Exit Sub
End If

'//判斷文件是否存在或被自己鎖定中
If (Doc.Locker(0) = "") Or (Doc.Locker(0) = UName) Then
Doc.Locker = UName
Doc.LocTime = Now()
Doc.Save True , True

LockFlag = True
LockByMe = True
Continue =True
Else

strMsg = "此文件正被 " + Doc.Locker(0) + " 編輯中 !"
Msgbox strMsg , 16 , "訊息"
LockFlag = True
LockByMe = False
Continue =False
End If


4. 在 Form QueryClose Event 中加入下段程式 (清除自己鎖定之資料)
Dim sn As New NotesSession
Dim db As NotesDatabase
Dim View As NotesView
Dim Doc As NotesDocument
Dim Sno As String
Dim strMsg As String

'//取得現用資料庫
SNo = Source.Document.SNo(0)
Set db = sn.CurrentDatabase
Set View = Db.GetView("DocLock")
Set Doc = View.GetDocumentByKey(SNo)

'//被自己鎖定中 , 清除
If LockByMe = True Then
Doc.Locker = ""
Doc.Save True , True
Continue =True
End If

評分機制尚未完成, 請稍待!