Prevent Duplicate Documents On Save

Prevent Duplicate Documents On Save

This is a handy way I use to prevent users from saving duplicate documents. A
duplicate
document is defined by the document key, which is determined by your
application.

1. Create a view of all documents, sorted by the key field or value of the key.
Limit the
view selection formula to those forms you wish to prevent duplicates for.
2. Create a field on the each form which evaluates to the key value. It should
be a
computed field.
3. Place the code for the Ok2Save function below in the Globals section of your
form.
This allows the function to be called anywhere within your form.
4. Place the following code fragment into the QuerySave event on each form
which contains
the key field: Continue = Ok2Save( source ).
5. In any buttons on your form which save the document, call Ok2Save(
NotesUIDocument ) to
determine if you can save the document.

This will prevent documents from being saved if a document with the same key
exists in the
database. Enjoy!

Function Ok2Save( source As NotesUIDocument ) As Variant
Dim view As NotesView
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim s As New NotesSession
Dim key As String
Dim key2 As String
Dim item As NotesItem

' get the current database and view
Set db = s.CurrentDatabase
Set view = db.GetView("(all)") ' this is the view created in step 1 above

' refresh document

    Requires Free Membership to View

    Register today to access targeted resources from our editorial writers and independent industry experts focused on Lotus Domino, Notes, Workplace and other related technologies.

    By submitting your registration information to SearchDomino.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchDomino.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

for validation formulas and get key for this document
Call source.Refresh
key = source.fieldgettext("key")

' check to see if the document key already exists....
Set doc = view.getdocumentbykey( key )

If doc Is Nothing Then
' first time saved
Ok2Save = True
Exit Function
Else
Set item = doc.GetFirstItem("key")
key2 = item.values(0)
If key2 = key Then
' key matched...possible duplicate"
' subsequent saves
If doc.UniversalID = source.document.UniversalID Then
'saving same document
'unid matched, same doc, saving..."
Ok2Save = True
Exit Function
Else
' duplicate document
' unid is different with same key...duplicate document"
Messagebox "This would create a duplicate document. Save
cancelled.",64,"Save"
Ok2Save = False
End If
Else
' keys dont match not the same document
'keys dont match...saving"
Ok2Save = True
End If
End If
End Function

This was first published in November 2000

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.