Access Cookbook - Ken Getz [253]
Figure 12-2. The Insert Object dialog
Discussion
The Action property for OLE objects in Access is different from almost any other property, in that setting its value causes an action to take place. Normally, properties describe characteristics of an object, and methods cause actions to take place. In this case, however, when you set the Action property to the constant acOLEActivate, Access activates the control at the time you set the property. If you set the Action property to the constant acOLEInsertObjDlg, Access displays the modal Insert Object dialog at the time you change the property. By changing the OLE control's Action property, the code tells Access what action to take at that point. By changing the Verb property from acOleVerbPrimary (to activate the object) to acOleVerbOpen, you control how the object is opened: in place, or in its own window.
Table 12-1 lists the values that you're likely to use for the Action property. Others are available, but this list will get you started. For more information, see the online help topics on the Action and Verb properties.
Table 12-1. Possible values of the Action property
Constant
Value
Description
acOLECopy
4
Same as choosing the Edit Copy menu item. Copies the OLE object onto the Windows clipboard.
acOLEPaste
5
Same as choosing the Edit Paste menu item. Pastes the OLE object from the Windows clipboard into your control.
acOLEUpdate
6
Retrieves the most current data for the OLE object from the application that created it and displays it as a graphic.
acOLEActivate
7
Same as double-clicking the control. You must set the control's Verb property before you can use this Action.
acOLEClose
9
Closes the OLE object and ends the active connection with the application that provided the object.
acOLEInsertObjDlg
14
Displays the Insert Object modal dialog, allowing the user to insert an object.
This technique works just as well for unbound objects on forms. For example, if you have an embedded Word document, you could use code to activate the OLE object (named Embedded0 in the following example), set its first paragraph to bold, and then close the object:
Dim objWord As Object
' Activate the OLE object, using the primary verb.
Me.Embedded0.Verb = acOLEVerbPrimary
Me.Embedded0.Action = acOLEActivate
Set objWord = Me.Embedded0.Object.Application.WordBasic
objWord.StartOfDocument
objWord.ParaDown 1, 1
objWord.Bold 1
Set objWord = Nothing
' Close the OLE object.
Me.Embedded0.Action = acOLEClose
By the way, if you need to play a WAV file but don't want to embed an OLE object or use OLE at all, you can use the Windows API sndPlaySound function to do your work. (This function is aliased as acb_apiSndPlaySound in 12-01.MDB.) Just insert the following declarations and constants in a form's module:
Private Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Private Const SND_SYNC = &H0
Private Const SND_ASYNC = &H1
Private Const SND_NODEFAULT = &H2
Private Const SND_LOOP = &H8
Private Const SND_NOSTOP = &H10
Table 12-2 describes the possible flag values for the sndPlaySound function call.
Table 12-2. Possible values for the intFlags parameter to sndPlaySound
Constant
Value
Description
SND_SYNC
0
Plays the sound synchronously and does not return from the function until the sound ends.
SND_ASYNC
1
Plays the sound asynchronously and returns from the function immediately after beginning the sound. To terminate a sound once it's started, call acb_apiSndPlaySound, passing vbNullChar as the first parameter.
SND_NODEFAULT
2
Doesn't play the default sound if the requested sound can't be found.
SND_LOOP
8
The sound continues to