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.
Rather then using a 2 dimensional array, wouldn’t it be easier to use lists to store the fieldname and label? Then rather then using ubounds which I believe are expensive to process you could use a forall loop….
Just a thought…
[Ian : using a List would mean not worrying about the lenght, when you need to add or remove a field to check, and the syntax might be less awkward as well. I had not heard he about ubounds being expensive but it’s possible. all in all some thing it look in to. Many Thanks!]
Wouldn’t it be easier to put 2 multi-value computed for display fields on each form with a predifined name, e.g. Check_FieldName and Check_FieldLabel.
This way your validation function could be the same on each form, and the list can be easily adapted, without digging into the code.
(You could even use 1 field, with for each field e.g. “FName|First Name”).
[Ian: I like the idea of one field, and then pass the field name (something meaningful Like “ListofRequiredFields”) to the function. You still have have code to dig in to, but at a lessor level. Question, what type of field would be best : Computed, for Dispay, or hidden editable?]
The ‘universal code’ could look for the existance of a field ‘ListofRequiredFields’. If it exists, it would do validation, otherwise it doesn’t.
Your code does not need to be touched from one form to the other (you clould put it in a script or a header subform you use on every form.
Regarding the type of the field: Computer for Display for sure !
What I usally do is compute the field based on a parameter document (or a databae profile field). This way a db administrator can (re-)define the required fields without developer interference !
My parameter docs usually are called ‘Required_Company’ or ‘Required_Project (one for each form).
The advantage is that you can build quit elaborate validation rules.
My ‘Required_Project’ rule contains:
Pingback: False Positives » Blog Archive » SnTT : LotusScript Required Field Validation without pain Revisted.