Updated : Revisited with 300% less Pain! and much better code.
I’m missed the last couple of weeks but I’m back for another Show-n-Tell+Thursday, and doing a followup on my first post Building a better “Save & Exit” action button in Lotus Notes formula language, and the disscsion that in generated and doing required field Validation without pain. What I wanted some some good, but quick way to validate that the required field have been filled.
Sub Querysave(Source As Notesuidocument, Continue As Variant)
Dim doc As NotesDocument
Set doc = Source.Document
Dim fieldList(1 To 130, 1 To 2) As String
fieldList (1,1) ="FirstName" ' Field Name
fieldList (1,2) ="First Name" ' Field Label
fieldList (2,1) ="LastName"
fieldList (2,2) ="Last Name"
fieldList (3,1) ="StreetAddress"
fieldList (3,2) ="Street Address"
If ValidateForm ( Doc, "Admin Profile", FieldList )Then
Continue = True
Continue = False
Function ValidateForm (Doc As NotesDocument, FormName As String, FieldList As Variant ) As Integer
On Error Goto processError
Dim errPre As String
Dim errMsg As String
Dim msgTitle As String
msgTitle = "Validation Error saving " + FormName + " Request"
errPre ="The following field(s) are blank , or incorrect, and must be inputted before saving:" & CRLF & CRLF
'Dim item As notesItem
Dim x As Integer
For x = 1 To Ubound ( FieldList, 1)
ErrMsg = errMsg + VaildateFieldNotEmpty (Doc,FieldList(x,1), FieldList(x,2) )
If Not errMsg = "" Then
Messagebox errPre & errMsg, MB_OK+MB_ICONSTOP, msgTitle
ValidateForm = False
ValidateForm = True
Messagebox "System Error " & Err() & ": " & Error$ & ", line: " & Erl
ValidateForm = False
Function VaildateFieldNotEmpty (Doc As NotesDocument , FieldName As String , Msg As String) As String
Dim CRLF As String
If doc.GetFirstItem (FieldName).text = "" Then
VaildateFieldNotEmpty = CRLF+Msg
VaildateFieldNotEmpty = ""
So the first routine is the QuerySave, on the form, which bulds array of FieldName and FieldLabels to check for very basic required field validatation.
The second and third routines could be on the form but should be in a script library. The
%INCLUDE “LSCONST.LSS” needs to be in the declaration section.
The third routine takes the Document and FieldName and tests to see if the field is empty, if it is then it passes back the fields label (or it could be another message). My test seems to work for most fields in my – brief – testing, but some work is need for more robost testing.
The second routine, loops though the passed array and builds a string of field labels (or messages) and if not empty display a dialog box with one list of field requiring content. It also tell the QuerySave that it failed.
All this works in conjuction with my earlier : @If(@Command([FileSave]);@Command([FileCloseWindow]);””)
It also will work with any induvuial field Valiation, which get checked after the QuerySave if that is successful. You could also do more completed validation (of the A must be greater that B, or C must be odd) after this, in the query save.