Sub Querysave(Source As Notesuidocument, Continue As Variant) If Not IsValueUniqueForForm (Source.Document , "Company","CompanyName", |Status = "Current"| ) Then Messagebox "The Company Name must be unique.",MB_OK+MB_ICONSTOP, db.title + " Business Rule Error." Continue = False Exit Sub End If End Sub Function IsValueUniqueForForm ( Doc As NotesDocument, FormName As String, FieldName As String, AddtionalConditions As String ) 'Doc,the back-end Document we are testing. 'FormName,the formName, or alias, is passed because this value is not populated until after form is saved. 'FieldName,the name of the field we want to test of uniqueness 'AddtionalConditions, an optional field (can be black string), to skip some documents : ie |Status = "Current"| Dim dc As NotesDocumentCollection Dim db As NotesDatabase Dim search As String Dim fDoc As Notesdocument Dim session As New NotesSession Set db = session.CurrentDatabase Dim dateTime As New NotesDateTime(Cstr(Datenumber(2000, 5, 1))) ' search looks for unique values (case insensitive and stripping out spaces) on the from FormName search = | Form = "| + FormName +_ |" & @Trim (@UpperCase (| + FieldName + |))= @Trim (@UpperCase ("| + Doc.GetItemValue (FieldName) (0) +|"))| If AddtionalConditions = "" Then search = search + | & | + AddtionalConditions End If 'get a Doc Collection of possible matches. Set dc = db.Search(search,dateTime,0) If dc.count > 1 Then ' we already have more than 1 document matching are key string, boy have we got problems!! IsValueUniqueForForm = False Exit Function Elseif dc.Count = 0 Then ' we didn't find anything so we should be okay IsValueUniqueForForm = True Exit Function Elseif dc.Count= 1 Then Set fDoc = dc.GetFirstDocument If Doc.UniversalID = fDoc.UniversalID Then ' we just found the same document , so we should be okay IsValueUniqueForForm = True Exit Function Else ' we found another document IsValueUniqueForForm = False Exit Function End If Else ' we got a weird result, maybe bigger problems? IsValueUniqueForForm = False Exit Function End If End Function Function IsValueUniqueForFormViaView (Doc As NotesDocument, ViewName As String,FieldValue As Variant ) 'Doc,the back-end Document we are testing. 'ViewName,a view sorted by the field name, containing the documents we want to test. 'FieldValue, the value we what to test for uniqueness Dim session As New NotesSession Set db = session.CurrentDatabase Set view = db.GetView( ViewName ) If view Is Nothing Then Goto HandleFalse Set vc = view.GetAllEntriesByKey( FieldValue, True ) If vc.count > 1 Then Goto HandleFalse If vc.Count = 0 Then Goto HandleTrue If vc.Count= 1 Then If Doc.UniversalID = vc.GetFirstEntry.document.UniversalID Then Goto HandleTrue ' we just found the same document , so we should be okay End If HandleFalse: IsValueUniqueForFormViaView = False Exit Function HandleTrue: IsValueUniqueForFormViaView = True End Function