| Home | Previous Lesson: ItemChanged Event Next Lesson: ItemFocusChanged Event |
This event gets triggered when validation rules fail (the data doesn't pass the validation rule), and also when the ItemChanged event returns non-zero return code. If there is no code in this event by default, PowerBuilder displays an error message in the default format, and rejects the data and then the cursor remains in the current field.
You can tell PowerBuilder to display error messages or not, to reject data or not, by using proper return codes.
|
Return Code |
Action |
|
0 |
(Default) Reject the data value and show the error message box. |
|
1 |
Reject the data value with no message box. |
|
2 |
Accept the data value. |
|
3 |
Reject the data value, but allow the focus to change. |
Now, you know why PowerBuilder was displaying two error messages. Did you find the solution for the problem from the return codes above. If not, just type Return 1 in the ItemError event for the dw_product DataWindow. Now, run the application and test it.
It works perfectly. Do you know what we did? Since we are displaying error message in the ItemChanged event, we are preventing the default error message from the ItemError event with the return value 1. However, we fixed one problem and started another.
We have two situations. Display an error message in the ItemChanged event, and don't let PowerBuilder display an error message in the ItemError event, OR, don't display an error message in the ItemChanged event, and let PowerBuilder display its default error message in the ItemError event.
We are displaying our message only for product_no. We are NOT displaying for other fields and for failed validation rules. So, let PowerBuilder display it's own message.
"Return 1" doesn't display the error message. That means we are telling PowerBuilder never to display error messages in the ItemError event, rather than telling it to display conditionally. With the above code, if the user enters a negative product_balance value, it doesn't pass the validation rule, but, PowerBuilder is not going to display any message. Now, let's display error messages in the ItemError event, conditionally.
Declare an instance variable as shown below. To declare an instance variable, display the popup menu in the Script editor and select 'Go To/Instance Variables' menu option and you can declare those variables in the script area.
Boolean i_DoNotDisplayErrMsg = FALSE
Insert the following code, just above the "Return 1" statement in the ItemChanged event.
i_DoNotDisplayErrMsg = TRUE
Write the following code in the ItemError event for the dw_product.
// Object: DataWindow_product in w_product_master
// Event: ItemError
If i_DoNotDisplayErrMsg Then
i_DoNotDisplayErrMsg = False
Return 1
Else
Return 0
End If
To test this, run the application. In a new record, enter an existing product_no and see how many error messages you get. Enter 0 (zero) in the product_reorder_level and see if you get the error message you defined in the validation rule.
| Home | Previous Lesson: ItemChanged Event Next Lesson: ItemFocusChanged Event |