Before you can incorporate an ActiveX control in your application, you must perform three steps:
Install the ActiveX control.
Register the control.
Add the control to a form.
When you buy an ActiveX control, it generally has an installation program that copies the OCX file to your Windows system directory. The name of this directory can vary depending on whether you're running Windows 2003, Windows 98, Windows NT, or Windows 2000, and what you named your Windows directory during your operating system installation.
After you have installed the control, you're ready to register it with Access. Often a control is automatically registered during installation, which is true of the Calendar OCX included with Access. OCX controls are registered in the HKEY_LOCAL_MACHINE\SOFTWARE class in the Windows registry (see Figure 21.1). In the figure, the Image List control, registered as ImageListCtrl, is selected.
If an ActiveX control isn't registered, you can register it by using the ActiveX Controls dialog box. To open this dialog box, choose Tools, ActiveX Controls (see Figure 21.2).
The ActiveX Controls dialog box lists all the ActiveX controls currently registered in Access. To add an ActiveX control to the list, click Register. This opens the Add ActiveX Control dialog box (see Figure 21.3).
Make sure that you're pointing to the directory containing the OCX you want to register. The control you're registering must already be installed; if it hasn't been installed, it won't be on the list. Select the OCX you want to register, and click OK. You then return to the ActiveX Controls dialog box, and the control you selected appears on the list of registered controls. You're ready to include the control on a form.
If you no longer plan to use an ActiveX control, you should use the Unregister button, located in the ActiveX Control dialog, which removes the registry entries for controls you don't use.
After you have registered an ActiveX control, you can include it on your forms. You do this in one of two ways:
Select the ActiveX control from the toolbox by clicking the More Controls icon.
Choose ActiveX Control from the Insert menu when you're in Form or Report Design view.
The More Controls tool contains all the ActiveX controls registered by your system. This includes ActiveX controls that are part of Excel, Visual Basic, and any other application that uses ActiveX controls. Some of these controls won't work properly with Access. To determine which controls you can safely include in your application, read the Access Readme file or contact the vendor of the ActiveX control. The More Controls menu is shown in Figure 21.4.
The menu for the More Controls tool shows all the ActiveX controls installed on the system. You can also use the Insert menu to select an ActiveX control from the Insert ActiveX Control dialog box (see Figure 21.5). After you select a control from the Select an ActiveX Control list box, Access places the control on the form. You can move the control around the form and size it as needed.
After you have placed an ActiveX control on a form, the control is ready to operate in its default format. If you insert the Calendar OCX control in a form and run the form, it looks like Figure 21.6.
The Calendar control can display all the months of the year, along with the corresponding days for each particular month. So far, you haven't set any properties for the calendar, nor have you written code to respond to any of the calendar's events. Setting an ActiveX control's properties, executing an ActiveX control's methods, and responding to an ActiveX control's events are covered in the following sections.
When you insert an ActiveX control on a form, Access automatically creates a reference to the control's Type Library that appears in the References dialog box (see Figure 21.7). To invoke the References dialog box, choose Tools, References with the Visual Basic Editor active. Note that the full path to the control is stored in this dialog box. For example, Figure 21.7 shows that the Calendar OCX is stored in D:\Program Files\Microsoft Offce\Office. If the OCX is moved, Visual Basic for Applications (VBA) might not be able to resolve the reference. If this happens, you must open the References dialog box and manually remove the check from the reference marked as missing and set a reference to the ActiveX control in its new location.
If you're distributing an application with ActiveX controls, the application might or might not work without problems. Access does its best to try to resolve references to ActiveX controls. If the controls are in the Windows\System directory or the directory that Access is installed in, Access can automatically resolve the references, even if the application is installed in a different directory on the user's machine than it was on your machine.
Remember, not only do ActiveX controls need to be referenced, but they also need to be registered in the Windows registry. If you use the Microsoft Office 2003 Package and Deployment Wizard to distribute your application, the OCXs are automatically registered when the user installs your application. If you don't use the Microsoft Office 2003 Package and Deployment Wizard to distribute your application, you must write code to register the ActiveX control, or the user will have to manually register it.