Lost focus events

VBA lost focus events occur when the user exits a field, using the tab key or the mouse, regardless of whether the field’s contents changed. You can use two VBA events, BeforeLostFocus and AfterLostFocus, to execute VBA event procedures before and after the Microsoft Dynamics GP code for the lost focus event runs.

Microsoft Dynamics GP rarely uses the lost focus event. When it is used, it typically locks control fields (fields that control the display of a record).


The BeforeLostFocus event occurs before the Microsoft Dynamics GP code for the lost focus event. You can use the BeforeLostFocus event to cancel any subsequent lost focus events for the field. When set to True, the CancelLogic parameter cancels the Microsoft Dynamics GP lost focus and VBA AfterLostFocus events.

The following BeforeLostFocus event procedure runs for the Document Number field in the Receivables Transaction Entry window. This is a control field, and normally the lost focus event locks this field. In this case, the procedure cancels all other lost focus events, and the Document Number field remains unlocked and editable:

Private Sub Number_BeforeLostFocus(KeepFocus As Boolean, _ 
CancelLogic As Boolean)
	CancelLogic = True
	Description.Focus = True
End Sub

When set to True, the KeepFocus parameter allows you keep the focus in the current field.


The AfterLostFocus event occurs after the Microsoft Dynamics GP code runs for the lost focus event. You can use the AfterLostFocus event to evaluate the value of the field losing focus, and perform any additional operations in the window. The following AfterLostFocus event procedure checks whether the Payment Terms field is empty. If it is, the procedure displays a VBA dialog that asks the if the user wants to enter a payment term. If the user clicks Yes, the procedure opens the Payment Terms Lookup window:

Private Sub PaymentTerms_AfterLostFocus()
	Dim Response As Integer
	If PaymentTerms.Empty = True Then
		'Set a default payment term
		Response = MsgBox("Do you want to enter a payment term?", _
		If Response = vbYes Then
			LookupButtons = 1
		End If
	End If
End Sub

Documentation Feedback