Manipulating ActiveX Controls

An ActiveX control is an object with three distinct categories of attributes: methods, events, and properties.

A property is a control characteristic such as color, width, height, or font. You can set the properties for an ActiveX control in Visual Basic code or in the control’s property sheet. If an ActiveX control provides its own custom properties dialog box, you can also set the control’s custom properties there. To open the custom properties dialog box for an ActiveX control, click the Custom property box in the control’s property sheet, and then click the Build button next to the Custom property box, as shown in the following illustration.

Note   You can also open the custom properties dialog box for an ActiveX control by right-clicking the control, pointing to ControlName Object on the shortcut menu, and clicking Properties.

Using Methods, Events, and Properties in Code

As a developer, you can use the methods associated with an ActiveX control to manipulate that control. For example, you can use the Refresh method of the Calendar control to repaint the calendar.

Forms!EntryForm!Calendar.Refresh

You can use the events associated with a control to update the control or to synchronize the control with a record, section, or environment. For example, you can use the Click event of the Calendar control to start a procedure that retrieves the specified date and sets the value of a control on a different, synchronized form.

You can use the properties associated with a control to display or retrieve data. For example, you can use the Value property of the Calendar control to retrieve the date selected by the user.

The following example uses the Calendar control’s Click event and Value property to assign a chosen date from the Calendar control to a control on another form. The Click event occurs when the user clicks a date in the control. The code then uses the Value property to retrieve the selected date.

Private Sub Calendar_Click()
	Forms!SyncForm!DateVal = Forms!EntryForm!Calendar.Value
End Sub

To limit the dates a user can select with the Calendar control, you can use the BeforeUpdate event to examine the requested date and then either accept or reject the requested date, depending on whether it falls within the acceptable range.

Private Sub Calendar_BeforeUpdate (Cancel As Integer)
	Dim dteFirstDate As Date						' Starting date.
	Dim dteLastDate As Date						' Ending date.
	Dim dteReqDate As Date						' User's requested date.

	dteFirstDate = #1/1/90#						' Assign starting date.
	dteLastDate = #1/1/98#						' Assign ending date.

	' Retrieve requested date.
	dteReqDate = Forms!EntryForm!Calendar.Value

	If dteReqDate < dteFirstDate Then		' If user requests invalid date.
		Cancel = True								' Cancel the update.
		Exit Sub										' Exit subroutine.
	ElseIf dteReqDate > dteLastDate Then
		Cancel = True
		Exit Sub
	End If
End Sub

See Also   For information on the methods, events, and properties of the Calendar control, search the Help index for “Calendar control.”

You can access the events for an ActiveX control through the Procedure box in the Module window. In form Design view, click Code on the View menu, then click the ActiveX control in the Object box. The Procedure box lists the event procedures for the ActiveX control, as shown in the following illustration.

Note   In some cases, the events for an ActiveX control have names that are identical to Microsoft Access events. In this situation, the Microsoft Access event is assigned the original event name, and the ActiveX control event is assigned the event name concatenated with the word “Object.” For example, if the control supports an Enter event, then the Procedure box for the control contains an Enter event and an EnterObject event.

Data Binding Support

Some ActiveX controls can be bound to a single column in a form’s underlying recordset. For example, the Calendar control can be bound to a Date/Time field. By setting the control’s ControlSource property to the name of the field you want to bind the control to, you can display, edit, and update the value in a field of the table or query that is named in the form’s RecordSource property.

Note   Your form must be in Single Form view to use data-bound ActiveX controls. This means that you can’t set a form’s DefaultView property to Continuous Forms if that form contains a data-bound ActiveX control. Additionally, you can’t access ActiveX controls at all in Datasheet view.