QuerySave validation (LS question)

I want to do a “required fields” validation in the QuerySave, and I want to use the field names and their corresponding labels as variables. Hopefully this will make my current QuerySave code more compact, because in reality I have something like 30 required fields (all but 1 or 2 fields on the form).

And I suppose where it goes wrong, is that I am trying to use a variable to refer to a variable, if that makes any sense. Anybody any ideas or am I completely on the wrong track with this?

Sub Querysave(Source As Notesuidocument, Continue As Variant)

    Dim counter As Integer, emptyCounter As Integer

    'variable = fieldName#label

    field1 = "txtName#Company Name"

    field2 = "txtAddress#Company Address"

    field3 = "txtPCode#Company Postcode"

    field4 = "txtCity#Company City"



    Dim requiredFields(1 To 4) As String

    requiredFields(1) = source.FieldGetText( Strleft(field1, "#") ) 

    requiredFields(2) = source.FieldGetText( Strleft(field2, "#") ) 

    requiredFields(3) = source.FieldGetText( Strleft(field3, "#") ) 

    requiredFields(4) = source.FieldGetText( Strleft(field4, "#") ) 

    counter = 0 

    emptyCounter = 0 

    Forall i In requiredFields

            counter = counter + 1

            If i = "" Then

                    emptyCounter = emptyCounter + 1

                    msg = msg & "," & Strrightback("field" & counter, "#") '---> this is we're it goes wrong, because my variable is no longer a variable

            End If

    End Forall

    If emptyCounter > 0 Then

            Messagebox "Following fields are required: " & Chr(10) & msg, 0 + 16, "Lotus Notes"

            Continue = False

    Else 

            Continue = True

    End If 

End Sub

Subject: QuerySave validation (LS question)

Hi Anne,

You’re on the right track, but you need to use lists-

dim lstFieldLabels list as string

lstFieldLabels(“txtName”) = “Company Name”

lstFieldLabels(“txtAddress”) = “Company Address”

forall formField in lstFieldLabels

if source.FieldGetText(listtag(formField)) = “” then

msg = msg & “,” & formField

end if

end forall

If msg <> “” Then

Messagebox "Following fields are required: " & Chr(10) & msg, 0 + 16, “Lotus Notes”

Continue = False

Else

Continue = True

End If

Haven’t checked the code, but should work okay.

Cheers,

Andrew

Subject: Solution - QuerySave validation (LS question)

Thanks guys!For maintenance purposes, I actually prefer the QuerySave because you can have all the relevant code in the same place.

Andrew, your solutions works a treat. Many, many thanks !

Subject: RE: Solution - QuerySave validation (LS question)

Anne, here’s how you could do it using an array, if you wanted to

'variable = fieldName#label

Dim FieldInfo(1 To 4) As String

fieldInfo(1) = "txtName#Company Name"

fieldInfo(2) = "txtAddress#Company Address"

fieldInfo(3) = "txtPCode#Company Postcode"

fieldInfo(4) = "txtCity#Company City"



Msg$ = ""

For j% = 1 To 4

	If source.FieldGetText( Strleft(fieldInfo(j%), "#") ) = "" Then			

		msg$ = msg$ & "," & Strrightback(fieldInfo(j%), "#")

	End If

Next

If Msg$ <> "" Then

	Msgbox "Following fields are required: " & Chr(10) & msg, 0 + 16, "Lotus Notes"

	Continue = False

Else

	Continue = True

End If

Now, if you wanted to set it up to be more dynamic, you could do this:

FldInfo$ = {“txtName#Company Name” : “txtAddress#Company Address”: “txtPCode#Company Postcode”: “txtCity#Company City”}

fieldInfo = Evaluate(FldInfo$)



Msg$ = ""

For j% = Lbound(FieldInfo) To Ubound(FieldInfo)

	If source.FieldGetText( Strleft(fieldInfo(j%), "#") ) = "" Then			

		msg$ = msg$ & "," & Strrightback(fieldInfo(j%), "#")

	End If

Next

If Msg$ <> "" Then

	Msgbox "Following fields are required: " & Chr(10) & msg, 0 + 16, "Lotus Notes"

	Continue = False

Else

	Continue = True

End If

This would allow you to add fields to check by modifying the list that is Evaluated.

Subject: QuerySave validation (LS question)

This might be an atypical approach but instead of placing your validation code in the QuerySave event (especially for 30 fields), just code the validation using @Failure in the standard input validation areas of each field. Then in your QuerySave, one of the first things you do is a Source.Refresh. If any field fails validation, a 4412 error is generated. You will have an error handled to catch this specific error which will simply return the user back to the form, displaying the error message from the @Failure. It would look something like this:

Sub Querysave(Source As Notesuidocument, Continue As Variant)

Dim . . .

.

.

.

On Error Goto ErrRtn

On Error 4412 Goto ValidationErr

Call Source.Refresh 'trigger validation formulas

.

.

.

your code

.

.

.

Exit Sub

ErrRtn:

Msgbox "Error " & Cstr(Err) & " - " & Error & " occurred at line " & Cstr(Erl), 0 + 16, "Error on QuerySave Event"

Continue = False

Exit Sub

ValidationErr:

Continue = False

Exit Sub

End Sub

This way, your QuerySave event remains uncluttered with validation logic and operates seamlessly with a @function validation. And if new fields are added in the future, you can easily set up the field with @function validation without needing to go into your LS code.

Hope that helps.

Subject: Small Correction to Wing Mar’s Suggestion

Wing,

Your solution is exactly what I was looking for. I tried it and found it has a slight flaw – no Resume statements. I modified your code as follows and it worked (just showing the last few lines and FYI, I did NOT test the ErrRtn branch)…

.

.

.

Exit Sub

ErrRtn:

Msgbox "Error " & Cstr(Err) & " - " & Error & " occurred at line " & Cstr(Erl), 0 + 16, “Error on QuerySave Event”

Continue = False

Resume Finished

ValidationErr:

Continue = False

Resume Finished

Finished:

End Sub

Thanks!

Ken