Lotus Notes FAQ Visit Our Sponsor!

How do you lock a document that is being edited?

From Martin Zuercher (martin.zuercher@web.de):

Sub Queryopen (Source As Notesuidocument, Mode As Integer, Isnewdoc As Variant, Continue As Variant)
Dim ws As New notesuiworkspace
Dim s As New notessession
Dim Uidoc As Notesuidocument
Dim doc As NotesDocument
Dim boxType As Long
Dim answer As Integer

Set uidoc = source
Set doc = source.Document
Continue = True

If Mode = 1 And Not source.IsNewDoc Then
If uidoc.document.LockedBy (0) = "" Then
uidoc.document.lockedby = s.COMMONUSERNAME
If Not uidoc.Document.Save (False, False, True) Then ' If problem mit save => wird konflikt geben -> kein Edit Status
Messagebox("Das Dokument ist schon in Bearbeitung" )
Call uidoc.Close
End If
uidoc.document.save True, False
Else
If uidoc.document.lockedby(0) = s.COMMONUSERNAME Then
boxType = MB_YESNO + MB_ICONQUESTION
Answer = Messagebox ("Die letzte Bearbeitung wurde nicht ordnungsgemäss beendet. Wollen Sie das Dokument freigeben?", MB_YESNO, "Unlock?")
If answer = 7 Then
Continue = False
Else
uidoc.document.lockedby = s.COMMONUSERNAME
uidoc.document.save True, False
End If
Else
continue = False
Messagebox("Das Dokument ist schon in Bearbeitung bei " + uidoc.document.lockedby(0))
End If
End If
End If

End Sub


Sub Querymodechange(Source As Notesuidocument, Continue As Variant)
Dim s As New notessession
Dim doc As NotesDocument
Dim boxType As Long
Dim answer As Integer

Set doc = source.Document
Continue = True

If source.EditMode = False Then ' READ -> EDIT
If doc.LockedBy (0) = s.COMMONUSERNAME Then
boxType = MB_YESNO + MB_ICONQUESTION
Answer = Messagebox ("Die letzte Bearbeitung wurde nicht ordnungsgemäss beendet. Wollen Sie das Dokument freigeben?", MB_YESNO, "Unlock?")
If answer = 7 Then
Continue = False
End If

Elseif doc.LockedBy (0) <> "" Then
Messagebox ("Das Dokument ist schon in Bearbeitung bei " + doc.lockedby(0))
continue = False

End If


Else ' EDIT -> READ
Call source.reload ()
If Not doc.lockedby = "" Then
doc.lockedby = ""
doc.save True, False
End If

End If

End Sub


Sub Postmodechange(Source As Notesuidocument)
Dim s As New notessession
Dim doc As NotesDocument

If source.EditMode = True Then ' READ -> EDIT
Call Source.reload ()
Set doc = source.Document

If doc.LockedBy (0) = "" Then
doc.lockedby = s.COMMONUSERNAME
If (False = doc.Save (False, False, True) ) Then
Messagebox ("Das Dokument ist schon in Bearbeitung" )
doc.SaveOptions = "0"
Call Source.close ()
Exit Sub
End If
doc.save True, False

End If

End If

End Sub


Sub Queryclose(Source As Notesuidocument, Continue As Variant)
Dim uiw As New notesuiworkspace
Dim doc As notesDocument

Set doc = Source.Document

If ( Source.EditMode ) And Not (Source.Isnewdoc)Then
doc.LockedBy = ""
doc.SaveOptions = "1"
If (False = doc.Save (False, False, True)) Then
Print "Fehler beim Speichern, da Dokuments bereits von anderem Benutzer gespeichert wurde."
End If
' Source.refreshhideformulas
End If

Call uiw.ViewRefresh

End Sub

The Lotuscript code works by adding a lock field to the document the user wishes to edit and then immediately saving the document; when other users try to open the document, they will see that there is a lock field so they cannot edit the document.

This technique will only work if the database is on a single server (no replication). There is also a small chance for a race condition if two users manage to read a document simultaneously without the lock. Finally, if the user's system crashes while editing this locked document, the document will remain locked unless you implement some sort of emergency unlock mechanism.

Domino 6 has a supported way of locking a document for editing, even in replicated environments.


Applies to Notes Versions: 4, 4.5, 4.6, 5
Last Modified: February 24, 2002