Programming with OLE Controls

Programming an OLE control is very easy. You just use its events, properties, and methods as with any other control or object in Microsoft Access. The Calendar Sample control in Microsoft Access is an example of using the Calendar control. It fires the various methods of the control via a command button and works with the control's events.

There are a few points to keep in mind when working with OLE controls. First, the syntax to get to a control's methods and properties is a bit different. You need to say: "Forms!MyForm!Embedded0.Object.Method" where "Embedded0" is the name of the unbound object frame that contains the control. For example:


    Forms![Calendar Sample].Embedded0.Object.NextMethod
    Forms![Calendar Sample].Embedded0.Object.Value

You can get around this by declaring a variable of type Object and setting it to the unbound object frame's Object property. Then you can use a shorter, more Visual Basic–like syntax. For example:


    Dim Foo As Object
    Set Foo = Forms![Calendar Sample].Embedded0.Objec
    Foo.NextMethod
    Foo.Value

This limitation is not part of the OLE Control architecture; it's only part of the Microsoft Access implementation.

The second point is that even though Microsoft Access 2.0 and the OLE Control architecture don't currently support data-bound controls, you can simulate data binding if the control supports BeforeUpdate and AfterUpdate–type events on its Value property. The sample form shows how this can work by binding the value of the Calendar control to that of an unbound text box.

In the BeforeUpdate event for the Calendar, we can check the new date and reject that date if desired.


Sub CalendarFrame_BeforeUpdate (Cancel As Integer)
'Only allow dates after 1992. This allows us to demonstrate 
'the "data binding"-like stuff you can do with BeforeUpdate &
'AfterUpdate
If (Calendar.Value <= #12/31/92#) Then
    MsgBox "Only allowed dates after 12/31/92"
    Cancel = 1
End If
End Sub

During the AfterUpdate event we set the unbound text box to the same value as the Calendar.


Sub CalendarFrame_AfterUpdate ()
'Update unbound field with value of Calendar control
Me!CalDate = Calendar.Value
End Sub