Reg Exp
Web Design
Notes Client
Custom OK/Cancel Buttons and Validation
I discovered something interesting that I thought I would share with everyone. It's only applicable if you're using your own OK/Cancel buttons in a dialog box (as opposed to the buttons that Notes generates). For those of you that don't know, you can specify your own OK and Cancel buttons in a dialog box. All you have to do is add a hotspot button to your dialog box form/subform and choose "OK" or "Cancel" (there's also one for "Help") under the "Type" of button (see figure 1). You don't put any code behind the button. Then, in your Dialog Box code you tell Notes to not generate the OK and Cancel buttons. Those are the 4th and 11th parameters, respectively, in the ws.DialogBox method in script.

Here is some sample code:

If ws.DialogBox("sfrmTemp", True, True, True, False, False, False, "Title", doc, True, True) Then
   Msgbox "OK Clicked"
   Msgbox "Cancel Clicked"
End If

The 4th parameter being "True" says that no Cancel button should be generated, and the 11th parameter being "True" says that no OK button should appear.

So, that's how you generate your own OK and Cancel buttons. But that's not the point of this tip. The purpose of this tip is a "gotcha" when you are using your own OK and Cancel buttons. If you have fields on the dialog box that have input validation, then you can't use your own OK/Cancel buttons. I don't know the underlying reason, but I can describe the situation that causes the problem. If you open up the dialog box, then click OK in a situation where the validation will cause a failure (like if you don't have a field filled out), then after clicking OK in the validation prompt the OK and Cancel buttons will no longer work (neither one causes the dialog box to close). If all the validation succeeds the first time, then everything is fine (the dialog box closes).

So it appears that the custom OK can Cancel buttons will work only the first time they are clicked and not any subsequent times without closing the dialog box and re-opening it. Our work-around for this situation is to do the validation in the script calling the dialog box. For example:

continue = True
While continue
   If ws.DialogBox("sfrmTemp", True, True, True, False, False, False, "Title", doc, True, False) Then
       If doc.GetItemValue("Name")(0) = "" Then
           Msgbox "You must supply the name.", 16, "Field Contains Incorrect Value"
           continue = False
       End If
       continue = False
   End If

The user will continue in the While loop until they click cancel (the ws.DialogBox method returns False) or until the name is filled out. If they haven't filled out the name, they get the message box and continue in the loop. So, the validation is done in the script instead of in the underlying form. In this case, the OK and Cancel buttons continue to work because the dialog box is closed and re-opened each time. Note that an underlying document is being used for the dialog box. That document is not being reset, so the values the user typed into the dialog box are maintained. This means that the user won't have to start over just because the Name field was empty - everything else they entered is still in the underlying document and will be re-presented to the user.

If you have multiple validation items you need to check, you should perform a series of Elseif statements in the inner block - if none of the situations succeed (if all the validation is fine) then continue will be set to False and the loop will exit.