VBA user changed events occur when the user changes the contents of a field, then moves the focus out of the field, using the tab key or the mouse. In fields that toggle, such as push buttons and visual switches, the user changed event occurs when the user clicks the field. In list fields, the user changed event occurs when the user selects an item in the list.
You can use two VBA events, BeforeUserChanged and AfterUserChanged, to execute VBA events either before or after the Microsoft Dynamics GP code for the user changed event runs.
The BeforeGotFocus, AfterGotFocus and BeforeUserChanged events are the only three window field events where VBA allows you to set the value of the current field (the field whose events are running).
It’s common for Microsoft Dynamics GP to use the user changed event to verify the contents of a field when the field changes; however, this isn’t done for all fields.
In fields that toggle, such as push buttons and visual switches, the user changed event occurs when the user clicks the field. Microsoft Dynamics GP always executes application code during the user changed event for these fields.
In list fields, such as button drop lists and list boxes, user changed events occur when the user selects an item from the list, or presses the up arrow or down arrow keys to move to a different list item.
The BeforeUserChanged event occurs before the Microsoft Dynamics GP code runs for the user changed event. Use this event to evaluate the value of the field and bypass, or cancel, any Microsoft Dynamics GP processing that occurs for the same field.
Use caution when canceling Microsoft Dynamics GP processing for a field’s user changed event. Canceling the user changed event processing inhibits the accounting system’s ability to verify the contents of a field.
In the following example, the BeforeUserChanged event procedure runs for the Invoice Entry window’s Trade Discount field. Using the CancelLogic parameter, it cancels processing for the Trade Discount field if the user enters an amount greater than 4% of the invoice subtotal. It also uses the KeepFocus parameter to place the focus in the same field, allowing the user to enter a lower amount:
Private Sub TradeDiscount_BeforeUserChanged(KeepFocus As Boolean, _ CancelLogic As Boolean) If TradeDiscount > Subtotal * 0.04 Then 'The trade discount is greater than 4% of the subtotal 'Cancel the invoice calculation CancelLogic = True TradeDiscount.Value = Subtotal * 0.04 KeepFocus = True MsgBox "You cannot enter a discount greater than 4% of " + _ "the subtotal." End If End Sub
The AfterUserChanged event occurs after the Microsoft Dynamics GP code for the user changed event runs. Use this event to evaluate information the user entered in a field. The following example uses the AfterUserChanged event to check the value of the Customer ID field in the Invoice Entry window, then disable the Trade Discount field:
Private Sub CustomerID_AfterUserChanged() If CustomerID = "ADVANCED0002" Then 'Don't offer a trade discount TradeDiscount.Enable = False End If End Sub