Verifying field values

Microsoft Dynamics GP verifies data entered in many fields to check whether the data is valid. When you enter data in a field manually, verification takes place in the Microsoft Dynamics GP user changed event (when the user enters the data, then moves out of the field).

When you set the value of a field using VBA, this verification will still occur, but has different results depending on whether you set the field from “inside” the field, or “outside” the field. The following sections describe data verification issues in more detail.

Setting a value from inside the field

If you set a value from “inside” a field (after the field gains focus), using the field’s BeforeGotFocus, AfterGotFocus, or BeforeUserChanged events, Microsoft Dynamics GP field verification can perform normally. This is the recommended method for setting a field value, since it most closely matches how the user enters data, and how the accounting system verifies that data.

In the following example, the AfterGotFocus event procedure for the Shipping Method field sets the value of the Shipping Method field:

Private Sub ShippingMethod_AfterGotFocus()
	If ShippingMethod.Empty = True Then
		ShippingMethod = "NEW"
	End If
End Sub

When the user moves out of the Shipping Method field, the Microsoft Dynamics GP code for the user changed event checks whether the field’s data is valid. In this case, it will make sure “NEW” is an existing shipping method. Since this is a new shipping method, a dialog will appear asking whether the user wants to add it.

You cannot set the field’s value using events that follow the Microsoft Dynamics GP user changed event (AfterUserChanged, AfterLostFocus or BeforeLostFocus events). Microsoft Dynamics GP doesn’t have the opportunity to verify values set in these events, and a VBA error will occur. You can use these events to read the value of the field.


Setting a value from outside the field

If you set a field’s value from “outside” the field (before the field gains focus), such as by using the window’s BeforeOpen or AfterOpen event, or another field’s BeforeUserChanged or AfterUserChanged event, VBA automatically runs the Microsoft Dynamics GP user changed event for the field you’re setting. This is necessary so the application code associated with these events can verify the field’s value.

For many fields, Microsoft Dynamics GP does little or no field verification, and setting the field’s value from “outside” the field using VBA will not generate errors. However, Microsoft Dynamics GP will perform verification for fields that affect business logic (such as an invoice discount percent, or a tax amount) or for add-on-the-fly fields. Typically, these are fields that generate Microsoft Dynamics GP alert dialogs when you enter invalid data, indicating that the value entered is incorrect. If the field is an add-on-the-fly field, a dialog typically appears asking whether you want to add a new record.

If you set a field value from “outside” the field using VBA, and Microsoft Dynamics GP determines that its value is invalid, the accounting system will first display its alert dialog. This will be followed by a VBA error dialog.

The VBA error occurs because the accounting system attempts to place the focus in the field and restore the field’s previous value. Since the current field does not have focus, Microsoft Dynamics GP cannot place the focus and the VBA error results.

This type of error will commonly occur when you set an add-on-the-fly field to a new value from “outside” the field. The following AfterOpen event procedure for the Receivables Transaction Entry window sets the value of an add-on-the-fly field (Shipping Method):

Private Sub ReceivablesTransactionEntry_AfterOpen()
	'Set the Shipping Method field to a value that doesn't exist.
	ShippingMethod = "NEW"
End Sub

In this case, the accounting system will display a dialog asking whether you want to add the new shipping method. When you dismiss the dialog, VBA generates an error, since the system cannot place the focus in the Shipping Method field.

There are two ways to avoid this type of error:

Private Sub ShippingMethod_AfterGotFocus()
	'Set the Shipping Method field to a value that doesn't exist.
	ShippingMethod = "NEW"
End Sub

Private Sub CustomerID_AfterUserChanged()
	'Move the focus to the field, then set the value 
	ShippingMethod.Focus("NEW")
End Sub


Documentation Feedback