Using the DUOS

Using the DUOS typically involves adding fields to a window using the Modifier, and writing VBA code that saves, displays or deletes the data object in the DUOS when the user saves, displays or deletes a corresponding record in the window. The remainder of this section explains how to address each of these issues in more detail.

A URL Address field and Contact E-mail Address field were added to the Customer Maintenance window with the Modifier. The examples in the remainder of this section will create a DUOS customer collection and data objects to store and display information in these fields.

Saving a DUOS data object

There are two situations where you’ll need to save a DUOS data object: when the user clicks the Save button in a window, and when the user attempts to close the window or display another record without saving changes in the window (such as when using browse buttons). When you store a DUOS data object, you’ll need to address both of these situations.

Using the Save button

The following BeforeUserChanged field event procedure runs when the user clicks the Save button in the Customer Maintenance window. It first checks whether the user entered all required fields in the window using the window’s Required property. It then specifies the collection where it will store the object using the DUOSObjectsGet method.

To ensure the object has a unique ID, the ObjectID parameter of the Item property uses the value of the Customer ID field. The procedure defines the DUOS data object’s property names (“URL Address” and “Contact E-Mail Address”) and sets its property values to the values entered in the new fields:

Private Sub Save_BeforeUserChanged(KeepFocus As Boolean, _
CancelLogic As Boolean)
	Dim CustomerCollection As DUOSObjects
	Dim CustomerObject As DUOSObject

	If CustomerMaintenance.Required = True Then
	'The user entered all required fields. Create/get the collection
		Set CustomerCollection = DUOSObjectsGet("CustomerCollection")

		'Create the object, using the customer ID as the object ID
		Set CustomerObject = CustomerCollection.Item(CustomerID)

		'Set the object’s property names and values
		CustomerObject.Properties("URL Address") = URLAddress
		CustomerObject.Properties("Contact E-Mail Address") _
		= ContactEMailAddress
	End If
End Sub

Using the Save dialog

The Microsoft Dynamics GP save dialog appears when the user makes changes to a record, then attempts to move to another record or close the window without saving changes.

The user can click the Save button to save the changes, the Discard button to ignore the changes, or the Cancel button to close the dialog and cancel the save operation. In this situation, you’ll need to know when the user clicks the Save button, and save any changes to the corresponding DUOS data object. To do this, use a window AfterModalDialog event.

In the following example, the AfterModalDialog event procedure runs when a modal dialog appears for the Customer Maintenance window. It uses the PromptString parameter to ascertain whether the Microsoft Dynamics GP save dialog appeared. If the user clicks the Save button, the procedure specifies the collection, then specifies the data object using the collection’s Item property:

Private Sub Window_AfterModalDialog(ByVal DlgType As Long, _ PromptString As String, Control1String As String, Control2String _
As String, Control3String As String, Answer As Long)

	Dim CustomerCollection As DUOSObjects
	Dim CustomerObject As DUOSObject

	If PromptString = "Do you want to save changes to this " + _
	"customer?" Then
		'The save dialog appeared, asking if the user wants to save
		'changes
		If Answer = dcButton1 Then
			'The user clicked Save. Get the collection
			Set CustomerCollection = DUOSObjectsGet _
			("CustomerCollection")
	
			'Return the correct customer object
			Set CustomerObject = CustomerCollection.Item(CustomerID)
	
			'Save the values from the window fields
			CustomerObject.Properties("URL Address") = URLAddress
			CustomerObject.Properties("Contact E-Mail Address") _
			= ContactEMailAddress
		End If
	End If
End Sub

You’ll also use the window’s AfterModalDialog event to respond to the Microsoft Dynamics GP delete dialog. See Deleting a DUOS data object for more information.

Retrieving a DUOS data object

After you’ve saved a DUOS data object, you’ll need to display it when the user displays its associated record. To do this, add a Changed event procedure for the window’s control field (the field that controls the display of the record in the window). This field also provides the record with a unique value, such as an item or customer ID, or a document number.

In the following example, the Customer Maintenance window’s control field is the Customer ID field. Whenever this field changes (such as when the user browses records in the Customer Maintenance window), the Changed event procedure runs. It uses the collection’s Item property to return the data object matching the new value in the Customer ID field. The procedure then updates window fields using the customer object’s Properties property:

Private Sub CustomerID_Changed()
	Dim CustomerCollection As DUOSObjects
	Dim CustomerObject As DUOSObject

	Set CustomerCollection = DUOSObjectsGet("CustomerCollection")
	If CustomerID.Empty = False Then
		'There's a new value in the field. Retrieve the customer 
		'object from the collection using the value in the 
		'Customer ID field
		Set CustomerObject = CustomerCollection.Item(CustomerID)

		'Update the window fields
		URLAddress = CustomerObject.Properties("URL Address")
		ContctEMailAddress = CustomerObject.Properties _
		("Contact E-Mail Address")
	End If
End Sub


Deleting a DUOS data object

You’ll likely want to delete a DUOS data object from the collection when the user deletes its corresponding record in the accounting system. This ensures that you don’t have an “orphaned” DUOS data object after the user deletes the corresponding record.

Instead of writing VBA code that deletes a DUOS data object when the user clicks the Delete button, you delete a DUOS data object based on how the user responds to the delete dialog.

The user can either click Delete to delete the record, or click Cancel and cancel the operation. You’ll want to delete a DUOS object only if the user clicks Delete in response to the dialog. Similar to saving DUOS objects, you can accomplish this by using the AfterModalDialog event. The code that is added to this event will then track whether the user selected the delete button.

In some situations, there’s one other condition you’ll need to address when deleting a DUOS object. A second message dialog may appear if the accounting system was unable to delete the record. For example, an additional message will be displayed if a customer record has any outstanding transactions corresponding to it.

The second message dialog needs to be handled differently than the delete dialog. This is because there’s no AfterModalDialog event available for the secondary message. Instead, you’ll need to handle the message dialog as a separate Microsoft Dynamics GP window and add it to your VBA project. Once added, you use VBA code in the window’s AfterOpen event to ascertain whether the message prevented the record from being deleted.

The following series of examples show how to do this for DUOS data objects stored with customer information.

Example: Customer Maintenance window

The following example shows the VBA code for the Customer Maintenance window that’s necessary to correctly delete a DUOS data object. This example is intended to work in conjunction with the example for the Microsoft Dynamics GP message dialog box, which follows this example. Together, these two examples allow you to correctly handle DUOS delete operations for the Customer Maintenance window.

Notice that there are three different variables (WanttoDelete, OktoDelete and CustIDtoDelete) used throughout these examples. These are declared as public variables and then initialized and set in the appropriate events:

'Declare module-level variables in the General section of the 'Customer Maintenance window object
Public WanttoDelete As Boolean
Public OktoDelete As Boolean
Dim CustIDtoDelete As String
---------------------------------------------------------------------
Private Sub CustomerID_Changed()
	If CustomerID.Empty = False Then
		'Initialize variables
		CustIDtoDelete = CustomerID
		OktoDelete = True
		WanttoDelete = False
	End If
End Sub
---------------------------------------------------------------------
Private Sub Window_AfterModalDialog(ByVal DlgType As Long, _ PromptString As String, Control1String As String, Control2String As _ String, Control3String As String, Answer As Long)

	If PromptString = "Are you sure you want to delete this + _ 
	"customer record?" Then
		If Answer = dcButton1 Then
			'The user clicked Delete on the delete dialog message
			WanttoDelete = True
		End If
	End If
End Sub
---------------------------------------------------------------------
Private Sub Delete_AfterUserChanged()
	Dim CustomerCollection As DUOSObjects
	Dim CustomerObject As DUOSObject

	'The user clicks the Delete button and no transactions exist
	If WanttoDelete And OktoDelete Then
		'Create the collection
		Set CustomerCollection = DUOSObjectsGet("Customer _ 
		Information")
		'Create the object using the CustomerID that was on the 
		'window
		Set CustomerObject = CustomerCollection.Item(CustIDtoDelete)
		'Remove the object
		CustomerCollection.Remove (CustIDtoDelete)
	End If
End Sub

Example: Dynamics1 message dialog window

The following example is attached to the Dynamics1 window object (the second message dialog). This example works with the example for the Customer Maintenance window (shown in the previous example). In this case, the public variable declared in the General section of the Customer Maintenance window code (OktoDelete) is set to False, since Microsoft Dynamics GP cannot delete the customer record:

Private Sub Window_AfterOpen()
	CustomerMaintenance.OktoDelete = False
End Sub

Printing data from the DUOS

After you’ve used the DUOS to display additional information in a window, you can use VBA to print DUOS information on a report. Printing data from the DUOS involves adding calculated fields to the report, adding the report and the calculated fields from the report to your project, then using VBA to set the calculated fields from the DUOS.

In the following illustration, the report layout for the RM Customer Report includes two new calculated string fields. The VBA example later in this section will use these fields to display values from a DUOS data object.

[spacer]

In the following example, the event procedure runs when the user prints the RM Customer Report. It updates two calculated string fields on the report (DUOSCustomerWebAddress and DUOSCustomerEMailAddress) with the data object in the customer collection. Since the new calculated fields are in the report’s additional header, this procedure uses the BeforeAH event to set the field values before data in the additional header prints.

Note that the RM Customer Report uses a field named Customer Number. This is the same field as the Customer ID field referenced in the Customer Maintenance window:

Private Sub Report_BeforeAH(ByVal Level As Integer, SuppressBand _
As Boolean)
	Dim CustomerCollection As DUOSObjects
	Dim CustomerObject As DUOSObject

	'Return the customer collection 
	Set CustomerCollection = DUOSObjectsGet("CustomerCollection")
	Set CustomerObject = CustomerCollection(CustomerNumber)

	'Update the report fields
	DUOSCustomerWebAddress = CustomerObject.Properties("URL Address")
	DUOSCustomerEMailAddress = CustomerObject.Properties _ 
	("Contact E-Mail Address")
End Sub


Documentation Feedback