Using window field events

Window field events execute VBA procedures when a user moves to, changes, or moves out of a window field, or a field you’ve added using the Modifier. To view field events, choose a field object in the Code window’s Object list; all the window field events will appear in the Procedure list.

When you choose an event, VBA automatically adds the event procedure syntax to the Code window. You can then write the event procedure using that event syntax.

There are four basic kinds of field events you’ll work with in VBA: got focus, user changed, changed and lost focus. Each event occurs relative to when the user moves the focus into a field (a got focus event), when the value of a field changes (a user changed or changed event) or when the user moves the focus out of a field (a lost focus event).

These events are useful when you want to respond to data the user enters in a field by performing other operations in the window. For example, the following AfterUserChanged event procedure runs when the user changes information in the State field:

Private Sub State_AfterUserChanged()
	If State = "NJ" Then
		'Default the salesperson and territory
		SalespersonID = "MARILYN H."
		TerritoryID = "TERRITORY 4"
	End If
End Sub

Field events also respond to actions performed by the user, such as clicking a button. In the following example, a new button (OpenMSWord) added to a window using the Modifier uses the AfterUserChanged event to open Microsoft Word:

Private Sub OpenMSWord_AfterUserChanged()
	Dim App As Word.Application
	Set App = CreateObject("Word.Application")
	App.Visible = True
End Sub

Each window field event you define occurs either before or after the Microsoft Dynamics GP code for the field event runs. Therefore, field events have names like “BeforeUserChanged” to indicate that the event runs when the user changed the contents, but before the Microsoft Dynamics GP application code runs.

Documentation Feedback