Using window field properties and methods

The following table explains the available methods and properties for window fields. The remainder of this section explains some of the more common methods and properties you’ll use when working with window fields.

[spacer]

Property/Method

Description

Caption property

Specifies the field’s caption (prompt).

Empty property

Specifies whether the field is empty.

Enabled property

Specifies whether the field is enabled.

Focus method

Moves the focus to a field.

FocusSeg method

Moves the focus to a segment in a composite.

Height property

Specifies the height of the field (in pixels).

Left property

Specifies the horizontal position of a field (in pixels).

Locked property

Specifies whether the current field is locked.

Move method

Moves the field to a specified set of coordinates within the window's client area.

Name property

Specifies the internal name VBA uses to reference the field.

Object property

Returns a field object without the standard properties and methods extended to it by Visual Basic.

Parent property

Returns a window or report object containing a specified field object.

Required property

Specifies whether the field requires data for the window to save the record properly.

TabStop property

Specifies whether the field is in the window’s tab sequence.

Top property

Specifies the vertical position of a field (in pixels).

Value property

Specifies the value of a field.

ValueSeg method

Specifies the value of a segment in a composite field.

Visible property

Specifies whether the field is visible.

Width property

Specifies the width (in pixels) of the field.


Disabling and locking fields

The Enabled property and Locked property allow you to make a field read-only and inaccessible to the user. To disable a field, set the Enabled property to False. A disabled field will appear dimmed, and any related controls (such as lookup or expansion buttons) will be inaccessible. To lock a field, set its Locked property to True. A locked field will appear with a gray background, and its related controls will still be accessible.

In the following example, an event procedure for the Account Maintenance window disables the Delete button and locks the Account Number field:

Private Sub Window_AfterOpen()
	If UserInfoGet.UserID = "LESSONUSER1" Then
		Delete.Enabled = False  'Disable the Delete button
		Account.Locked = True   'Lock the account number
	End If
End Sub

In this case, the user can browse information in the window, but not create new account records or delete existing ones.

You can use VBA to set the value of a field you’ve locked or disabled using VBA. However, you cannot set the value of a field locked or disabled by Microsoft Dynamics GP, nor can you unlock or enable these fields. Microsoft Dynamics GP disables fields when they don’t apply in a given situation, and lock fields to preserve the integrity of accounting data (such as document totals).

Controlling the focus

The Focus method and FocusSeg method let you control the movement of the focus in a window.

The Focus method moves the focus to a specified field. This will also activate the field’s BeforeGotFocus and AfterGotFocus events. In the following example, the Focus method moves the focus to the Check field if the current customer uses Check as the payment term:

Private Sub Tax_AfterLostFocus()
	If PaymentTerms = "Check" Then
		'The customer pays with checks. Move to the Check field
		Check.Focus
	End If
End Sub

The FocusSeg method moves the focus between segments in a composite. This method uses an index corresponding to the segment you want the focus to move to. The following example uses both the ValueSeg property and FocusSeg method to set a default value for an account number field:

Private Sub CashAccount_AfterGotFocus()
	If CheckbookID = "PAYROLL" Then
		'Default payroll account segments
		CashAccount.ValueSeg(1) = "100"
		CashAccount.ValueSeg(2) = "1500"
		'Move the focus to the third segment
		CashAccount.FocusSeg(3)
	End If
End Sub

When working with windows, you cannot move the focus using the window’s BeforeOpen event. Since the window isn’t open yet, there’s no place for the focus to be placed. You can, however, move the focus using the window’s AfterOpen event.

Showing and hiding fields

The Visible property (field) allows you to show and hide fields in a window. To hide a field, set the Visible property to False; the field and its caption will still be in the window, but won’t be visible to the user.

The following example hides sales fields when a given user opens the Salesperson Maintenance window:

Private Sub Window_AfterOpen()
	If UserInfoGet.UserID = "LESSONUSER1" Then
		CommissionedSales.Visible = False
		CostofSales.Visible = False
		NonCommissionedSales.Visible = False
		TotalCommissions.Visible = False
	End If
End Sub

You can still return or set the values of fields you make invisible with VBA. However, you cannot set the value of fields hidden by Microsoft Dynamics GP, nor can you show these fields.

Renaming fields

The Name property (field) allows you to change the name you use in VBA code to reference the field. Note that this is not the same as the Caption property, which allows you to change the field’s caption. You can change a field’s name using the Visual Basic Properties window, and the name will change in your project. You should then recompile your project to replace any references to that field with the new name.

You’ll find it necessary to rename fields if the field’s name conflicts with a reserved word in VBA. For example, the Date field has the same name as a reserved word in VBA. You may also want to rename fields to make code more readable.

Making fields required

The Required property specifies whether the field must contain data before the user can save information in the window. To make a field required, set its Required property to True. The following example sets fields in the Customer Maintenance window to required:

Private Sub Window_BeforeOpen(OpenVisible As Boolean)
	Name.Required = True
	ShortName.Required = False
	StatementName.Required = True
End Sub

Microsoft Dynamics GP will prompt the user to enter data in a required field if the field is empty when the user attempts to save the record. Fields marked as required by VBA are displayed with the same caption styles as fields marked as required by the accounting system.

You cannot set the Required property to False for fields already marked as required by Microsoft Dynamics GP. These are fields necessary for the accounting system to store the record properly.


Documentation Feedback