Hack #34. Play Videos in Access Forms

Deliver your message the multimedia way with the Windows Media Player.

Here's a really neat way to spice up your Access applications: play movies! Although this might seem a little too entertaining for "real" business use, consider that movies are one of the best vehicles for delivering information. You can incorporate movies into your database design in several ways. You can relate movie clips to data records, in which case the associated movie runs when a record is displayed. You can also have an unrelated movie play on demand (requiring a button click or some other way to initiate the movie to play).

To play movies you need to incorporate a control that can handle movie files. A number of these are available, most notably Windows Media Player, which is what this hack uses.

Putting the Player on the Form

First, you must add Windows Media Player to the form. Because this isn't a standard control, you must access it using the More Controls button on the toolbox, as shown in Figure 4-24.

Clicking the More Controls button displays a lengthy list of controls and libraries. Scroll down to find Windows Media Player, as shown in Figure 4-25.

After you click the control in the list, draw it on the form. Figure 4-26 shows a form in which Windows Media Player, a listbox, and a command button have been inserted. In this configuration, the listbox displays a list of movies from which to select; clicking the button plays the selected movie.

In Figure 4-26, the listbox is populated with paths to .mpg movie files. The listbox has two columns. The first column is the bound column, which holds the paths to the movie files. Its width is set to zero, so it isn't displayed to the user. Instead, the second column, which contains friendly names for the movies, is displayed. When the user has selected a movie, she simply presses the command button to start the movie. This effectively is a simple playlist. Figure 4-27 shows the form in View mode before playing a movie.

Looking for more controls

Figure 4-24. Looking for more controls

Selecting Windows Media Player

Figure 4-25. Selecting Windows Media Player

Playing a Movie

So, just how does a movie play? Actually, it's quite simple: the path to a movie file is handed to Windows Media Player's URL property and the movie starts playing automatically. This example shows the button's code; it takes the path from the listbox and hands it to the player:

  Private Sub cmdPlayMovie_Click()
   If Not IsNull(Me.listMovies) Then
    Me.WMPlayer.URL = Me.listMovies
Form design with Windows Media Player

Figure 4-26. Form design with Windows Media Player

Selecting a movie

Figure 4-27. Selecting a movie

		Else
		 MsgBox "First select a movie"
		End If
   End Sub

Starting, stopping, fast-forwarding, rewinding, and pausing are functions built into the player. These functions are available through the buttons on the player itself. This allows the user to work the movie in any needed fashion.

Windows Media Player has many events you can hook into. A little thought and creativity will go a long way toward integrating movies into your applications. This hack shows the basic way to implement a movie, but you can code around and work with the player in myriad ways.

Get Access Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.