QuerySave and Field Validation

I have a lot of field validation formulas on a form and have some code in QuerySave event. I would like to stop the code in QuerySave event UNTIL all validations are done. Is this possible? If so, how do you suggest I do this? Thanks for your help in advance!

Subject: QuerySave and Field Validation

LS Girl,

Are your field validations in Formula in the Input Validation formulas or are they part of an action button that uses LS?

If it is the former, I believe that it already acts that way. If it is the latter (and I suspect it is) change your action code to use the @Command([FileSave]) command and have Querysave do all of your field validation–that way, when a field is not validated, you can set continue equal to false and get the behavior you desire.

after all, it only takes one validation to stop the save dead in its tracks…it’s not like it is going to save after validation fails, right?

brandt

Subject: RE: QuerySave and Field Validation

Thank you, Brandt, for your quick reply. Actually, the field validations are in formula in the Input Validation formulas. I could put them in the QuerySave event but it’ll be a bit of a pain since some of them are a little complicated. I read in Help that QuerySave runs first and then the field validations. Is that true?

Subject: RE: QuerySave and Field Validation

it looks like you might be right, since the table says that Input Validation happens when the doc is saved, and QuerySave before the doc is saved.

But both should stop a save, if the Input Validation has an @Failure thrown when validation doesn’t occur.

What exactly are you trying to accomplish?

brandt

Subject: QuerySave and Field Validation

Hello,

Have you taken a look at ComputeWithForm in the NotesDocument class?

From Help – “Validates a document by executing the default value, translation, and validation formulas, if any are defined in the document form.”

Call Source.Document.ComputeWithForm(True,True)

Hope this helps,

  • David

Subject: RE: QuerySave and Field Validation

Thank you, David, for your response. I tried using the ComputeWithForm method and the error message showed up twice. The 1st message says “Notes Error: Validation failed” and then the 2nd msg says the field validation error. Is there a way I can avoid having two error messages?

What I am trying to accomplish is to populate the ProjectNumber field AFTER all the required fields are entered. This way, the project number doesn’t recompute itself after the user tries to save the document again. I hope this helps.

Subject: RE: QuerySave and Field Validation

Assign the return of ComputeWithForm to a variable, and use the False value for the RaiseError argument. If validation fails (that is, if your flag variable is false), you can stop your script (Continue = False then Exit Sub) and the user should only see the Formula validation @Failure message.

Subject: RE: QuerySave and Field Validation

Thank you Stan for your help. Can you give me an example? The code I have doesn’t seem to work although I could not be understanding it correctly.

isValid = source.Document.ComputeWithForm(False, False)

If isValid Then

Continue = False

Exit Sub

End If

Subject: RE: QuerySave and Field Validation

You were close. With the 2nd parameter of ComputeWithForm set to false, means no error will be raised but upon a validation failure, the method will return false. So you need to do this:

If Not isValid Then

Continue = False

Exit Sub

End If

Hope that helps.

Subject: RE: QuerySave and Field Validation

Thank you, Wing, for your help. Your suggestion worked but it did not validate the required fields. The code only exited the QuerySave. Even though I understand that QuerySave events happens before the validation formulas, is there a way that I could have the QuerySave event to compute AFTER the validation formulas? Am I going about this the wrong way? Any suggestions would be appreciated!

Subject: RE: QuerySave and Field Validation

Bite the bullet and do the validation in the QS. To make your life easier, try this:

http://www.openntf.org/Projects/pmt.nsf/ProjectLookup/FieldValidator

If worse comes to worst, you can always Evaluate your existing formulas – at least the test clauses of the @If statements.

Subject: RE: QuerySave and Field Validation

This might be considered a “radical” approach but as an alternative to Stan’s suggestion, try this:

In addition to your normal error handling routine (if you don’t put in error handling, you really should get in the habit), add in the following line:

On Error Goto ErrRtn 'normal error handling

On Error 4412 Goto ValidationErr 'specifically to catch validation errors

Then before executing any of the other LS code in QuerySave (normally my next line of code), add the line:

Call Source.Refresh 'trigger field validation formulas

By refreshing the form, the field validation formulas will be triggered. And if any field fails validation, LS detects error #4412.

So at the end of your QuerySave code where the error handling code goes, you should have something like this:

Exit Sub 'after executing last “regular” line of QuerySave LS code

ErrRtn:

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

Continue = False

Exit Sub

ValidationErr:

Continue = False

Exit Sub

End Sub 'of Querysave Event

So if any field fails validation, the error handling code skips down to the end of the event, bypassing the bulk of the LS code. This way, you only get one, normal validation error and only execute your LS code in the QuerySave event if everything is properly filled out.

If I have a lot of field validation, I prefer to do it using @formulas and @commands within the input validation of the fields. Its supposed to run a little quicker this way rather than coding it in LS and I find it easier for maintenance purposes if I immediately see which fields have validation formulas (by clicking on the field in designer) rather than coding that validation somewhere else. This method allows me to do just that.

hope that helps.