A modal dialog is a specific type of window that requires the user to perform some action in order to dismiss the dialog. The most common modal dialog is an ask dialog, where message text and one or more buttons appear in the window. A modal dialog appears in the following illustration:
You apply a modal dialog event to a window in the same manner as other window events. However, instead of occurring when the window opens, closes or activates, the modal dialog event occurs for the window whenever a modal dialog opens.
The VBA BeforeModalDialog event occurs when Microsoft Dynamics GP opens a modal dialog, but before it’s displayed. Since the dialog is open, but not visible, this event allows you to dismiss the dialog by programmatically “answering” it for the user. In the following example, if a user enters a non-existent shipping method in the Customer Maintenance window, Microsoft Dynamics GP displays an ask dialog asking whether the user wants to add the shipping method. Normally the user must dismiss the dialog manually, by clicking Add or Cancel. Instead, the event procedure automatically “answers” this dialog and removes a step in the data entry process:
Private Sub Window_BeforeModalDialog(ByVal DlgType As Boolean, _ PromptString As String, Control1String As String, Control2String _ As String, Control3String As String, Answer As Long) If PromptString = "Do you want add this Shipping Method?" Then 'Click the first button, the Add button Answer = dcButton1 End If End Sub
The PromptString parameter is the message text, which you can use to filter which modal dialog you want to respond to. The Answer parameter uses the constants dcButton1, dcButton2 and dcButton3. When indicated, these “click” the first, second or third button in the dialog.
The BeforeModalDialog event is also useful for altering the contents of the dialog before it’s displayed, including the modal dialog’s message text and button text. The following event procedure changes the message and button text:
Private Sub Window_BeforeModalDialog(ByVal DlgType As Boolean, _ PromptString As String, Control1String As String, Control2String _ As String, Control3String As String, Answer As Long) If PromptString = "Do you want to add this shipping method?" Then 'Change the message text PromptString = "Create this shipping method?" 'Change "Add" button to "Create" Control1String = "Create" End If End Sub
The VBA AfterModalDialog event occurs when Microsoft Dynamics GP opens a modal dialog, but after the user dismisses it. This event allows you to ascertain how the user responded to the dialog and perform any additional tasks based on that response.
The primary use for the AfterModalDialog event is to keep data in the accounting system synchronized with data you’ve stored using the Dynamic User Object Store (DUOS). For instance, if you’ve created a DUOS object that stores Internet address information for a customer, you’ll likely want to save it when the user saves the corresponding customer record.
In the following example, the user attempts to close a window without saving a customer record. In this case, Microsoft Dynamics GP will display a modal dialog asking if the user wants to save the record. If the user clicks the modal dialog’s Save button, the event procedure saves the corresponding DUOS object:
Private Sub Window_AfterModalDialog(ByVal DlgType As Long, _ PromptString As String, Control1String As String, _ Control2String As String, Control3String As String, Answer As Long) Dim Customers As DUOSObjects Dim Customer As DUOSObject Dim CustomerProperty As DUOSProperty If PromptString = "Do you want to save changes to this " + _ "customer?" Then 'The user is trying to save the record using the save dialog. If Answer = dcButton1 Then 'The user clicked Save. Set Customers = DUOSObjectsGet("Customers") Set Customer = Customers(CustomerID) Customer.Properties("URL Address") = URLAddress Customer.Properties("Contact E-Mail Address") = _ ContactEMailAddress End If End If End Sub