Rather than using input validation formulas, or even a separate LotusScript validation code for each field, this function provides a reusable, easily-maintained validation method. Also, since actual field names are often not 'user-friendly' this function returns friendly field labels when prompting the user for input.
Calling the function: QuerySave event If not ValidateFields(source) then continue=False exit sub end if The functions: Function ValidateFields(source as notesuidocument)as integer dim requiredfields as variant dim requiredlabels As variant dim fieldlist as string dim fieldlabels as string dim badfields as string dim errortext as string dim firstfield as string ValidateFields=True 'create two strings, one for field names and one for the associated user- friendly field labels 'you need only to modify these strings to add, remove, or modify the fields to be validated fieldlist = "fname;lname;homephone; cellphone;dob" fieldlabels = "First Name; Last Name; Home Phone; Cell Phone; Birthday" 'call a separate string-handling function to convert the strings to arrays requiredfields = explodestring(fieldlist,";") requiredlabels = explodestring(fieldlabels,";") dim i,z as integer i=0 z=ubound(requiredfields) forall fields in requiredfields tmpval = source.fieldgettext(requiredfields(i)) if tmpval = "" then if badfields = "" then 'build a string of friendly field labels for the validation error badfields = requiredlabels(i) 'grab the actual field where we will place the cursor if there is an error firstfield = requiredfields(i) else badfields = badfields & Chr(13) & requiredlabels(i) end if end if i=i+1 end forall 'if any field raises an error, return a validation error listing fields to be completed if badfields <> "" then dim warning as string ValidateFields=False warning = "The following required fields must be completed before you can save this record:" & Chr(13) & Chr(13) & badfields messagebox(warning) call source.gotofield(firstfield) end if end function Note: The following function is a modified version of a tip that has been entered by various persons at several tips sites. Function ExplodeString (txtstring as string, delimiter as string) as variant dim reqfields() as string dim fieldname as string dim pos as integer dim i as integer pos=instr(txtstring, delimiter) i=0 redim reqfields(i) while pos > 0 fieldname = Left(txtstring, pos-1) redim preserve reqfields(i) reqfields(i)=fieldname txtstring=Right(txtstring, Len(txtstring)-pos) pos=Instr(txtstring,delimiter) i=i+1 wend 'add the last remaining value of txtstring to the array redim preserve reqfields(i) reqfields(i) = txtstring explodestring = reqfields end function
Many thanks to William Key and the SearchDomino team for bringing us this very valuable and useful tip.
In a large scale application development, using densely populated forms, this generic validation routine helps save a lot of time and efforts. It completely eliminates the chore of coding repetitive validation messages and prompts. The author has done an excellent job of providing a neat and error-free code, not only for the main function but also for other called functions.
A job well-done !
This is a great tip. I have used it twice already with excellent results!
Do you have comments on this tip? Let us know.
This tip was submitted to the SearchDomino.com tip exchange by member William Key. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.