Online Book Reader

Home Category

Access Cookbook - Ken Getz [252]

By Root 1956 0
the Office 11 type libraries in each example. You'll need to modify the instructions to set a reference to the correct type library in each case, based on the version of Office you have installed. Besides these version numbering differences, all the examples should behave the same, no matter which version of Office you're using.

12.1. Play an Embedded Sound File from Within an Application


Problem


Your application stores WAV files as OLE objects within a table, and you'd like to be able to play them on demand. You know that users can double-click on the icon in a form to play the sounds, but you'd like some control over this. Is there a way to play one of these embedded sounds when you need to?

Solution


Access gives you substantial control over the use of OLE objects. Using the Action property of the control that's displaying the OLE object, you can tell the object to activate itself, copy itself to or paste itself from the Windows clipboard, update its data, and close or delete itself. The Action property can be used for bound or unbound OLE objects and graphs, too. You can also call up the Insert Object or Paste Special dialog to place data into the control. This solution uses a bound OLE field, but it works just as well with an unbound object on a form.

Load and run frmOLE (shown in Figure 12-1) from 12-01.MDB. This is a continuous form, pulling the data from the table tblOLE. If you click on an Activate button, the form activates that OLE object, which is stored in the OLEObject field of the table. The sample table includes a few WAV files, one Microsoft Graph object, and a MIDI file. Clicking on the Activate button will either play the sound or activate Microsoft Graph so you can edit the tiny graph object. Click on the Insert button to call up the Insert Object dialog, which allows you to insert any OLE object you like into the table. Click on the Open button to open the object in its own editing window rather than activating it in place.

Figure 12-1. frmOLE allows you to play or insert OLE objects

Follow these simple steps to create such a form:

Create a new table or modify an existing table, adding a column (named OLEObject in the sample) with its Data Type set to OLE object. Note that you cannot index on an OLE field, and therefore it can't be your primary key for the table.

Create a new form. To emulate the sample form, the only property you need to set is the DefaultView property. Set it to Continuous Forms so that you'll see multiple rows at the same time. This isn't necessary, but it will make your form look like the sample.

Create a bound OLE object (the cactus picture with the XYZ across the top on the toolbar) on the form. The code in this example is based on a control named objOLE; adjust the code appropriately if you name your control something else. The sample form includes the description field from tblOLE as a text box, but this isn't used in the sample code.

Add three buttons: cmdOpen, cmdActivate, and cmdInsert, with captions Open, Activate, and Insert, respectively. Attach the following code to the Activate button's Click event (see the Preface for more information on creating event procedures):

Private Sub cmdActivate_Click( )

Dim ctl As Control

On Error Resume Next

Set ctl = Me.objOLE

ctl.Verb = acOLEVerbPrimary

ctl.Action = acOLEActivate

Err.Clear

End Sub

Attach the following code to the Insert button's Click event:

Private Sub cmdInsert_Click( )

On Error Resume Next

Me.objOLE.Action = acOLEInsertObjDlg

Err.Clear

End Sub

Attach the following code to the Open button's Click event:

Private Sub cmdOpen_Click( )

Dim ctl As Control

On Error Resume Next

Set ctl = Me.objOLE

' Open, rather than just activate in place.

ctl.Verb = acOLEVerbOpen

ctl.Action = acOLEActivate

Err.Clear

End Sub

Save your form and run it. When you click on the Insert button, you'll see the Insert Object dialog (Figure 12-2). This dialog allows you to create a new object or to insert one from an existing file. Once you make your choice, Access places the object

Return Main Page Previous Page Next Page

®Online Book Reader