Got focus events

VBA got focus events occur when the user initially enters a field, using the tab key or the mouse. You can use two VBA events, BeforeGotFocus and AfterGotFocus, to execute VBA event procedures before and after the code for the got focus 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).

If Microsoft Dynamics GP runs application code during the got focus event, it typically checks the value of the field that’s gaining focus, and performs calculations or sets other field values based on that value. However, Microsoft Dynamics GP uses this event infrequently to do such operations.

BeforeGotFocus event

The BeforeGotFocus event occurs before the Microsoft Dynamics GP code for the got focus event. Use this event to set or evaluate the contents of the field gaining focus, and cancel any further got focus events from running (the Microsoft Dynamics GP got focus event and the VBA AfterGotFocus event). When set to True, the CancelLogic parameter will cancel the field’s other got focus events. In this example, the BeforeGotFocus event procedure cancels the AfterGotFocus event for the Batch ID field (shown in the description of the AfterGotFocus event below):

Private Sub BatchID_BeforeGotFocus(CancelLogic As Boolean)
	If DocumentType = 7 Then
		'The document type is a return. Don’t use a batch
		BatchID.Empty = True
		'Cancel the AfterGotFocus event, which opens the lookup
		CancelLogic = True
		MsgBox "Post returns individually, not in a batch."
	End If
End Sub


The AfterGotFocus event occurs after the Microsoft Dynamics GP code runs for the got focus event. Use this event to set or evaluate the contents of a field gaining focus. In the following example, the AfterGotFocus event for the Batch ID field checks whether the field is empty when the user moves to it. If it is, the event procedure opens the batch lookup window:

Private Sub BatchID_AfterGotFocus()
	If BatchID.Empty = True Then
		'The field is empty. Click the lookup button
		LookupButton3 = 1
	End If
End Sub

Documentation Feedback