Paul McFedries' Web Home


VBA Unleashed cover Visual Basic for Applications Unleashed

Chapter 21—Web Page Programming: ActiveX and VBScript

Go To VBA Unleashed Home Page Go To Top Go To Prev Go To Next

Working with the Script Wizard

By themselves, ActiveX controls add an entirely new level of interaction to Web pages: animation, multimedia, games, even full-fledged applications. However, there is yet another level of interaction to be explored: communication between the controls themselves.

For example, you might want to keep one or more controls invisible until a video clip finishes playing. When the clip is done, you could display the controls. Similarly, you might want to keep a group of controls disabled until the user activates a particular check box or option button.

Another example involves validating form data before processing it. For instance, you might want to make sure that a particular field wasn't left blank, or you might want to check that a numeric field doesn't contain letters or other nonnumeric characters. One of the big problems with using HTML forms is that you usually have no way of validating the user's data before it gets to the server. The script can (and should) scour the submitted data for errors, but it can only report any problems to the user by displaying a separate page. This means that the user must go back to the form, make the necessary corrections, and submit the form all over again. It would be faster and more efficient if the form could somehow check the data before it's shipped out to the server.

For all of these situations and many more, Web page scripts are invaluable. A Web page script is a section of programming code inserted into a page by means of the <SCRIPT> tag. The tag specifies the script language you're using—which will either be VBScript (which I discuss later in this chapter) or JavaScript.

The idea is that you create procedures that run when a particular event occurs in conjunction with a particular object. For example, you can designate a procedure that will run when a video clip finishes playing, or a check box is activated, or a form's submit button is clicked.

You now know more than enough VBA to handle VBScript without any trouble. However, in the same way that using an application's macro recorder can save coding time, so can the ActiveX Control Pad's Script Wizard. As long as your needs aren't too sophisticated, you can use the Script Wizard to add simple script functionality to your Web pages. More than likely, however, you'll need to tweak the VBScript code by hand to set it up just the way you want.

As an example, let's see how you can use the Script Wizard to make one control respond to something that happens in another control. Begin by inserting two ActiveX controls into a Web page:

Microsoft Forms 2.0 CheckBox: Enter Button Activator in the Caption property and False in the Value property.

Microsoft Forms 2.0 CommandButton: Enter Can't Click Me in the Caption property and select 0-False in the Enabled property.

Figure 21.10 shows the resulting HTML file (with a couple of extra <P> tags thrown in to separate the controls).

Screen shot of the ActiveX Control Pad
Figure 21.10.
An HTML page with an ActiveX check box and command button.

With your controls in place, crank up the Script Wizard by selecting Tools | Script Wizard. The top half of the Script Wizard window is divided into two sections, as shown in Figure 21.11:

Select an Event: This section displays a list of the objects associated with the Web page. Each of these objects has one or more events associated with it, such as Click (the user clicks the object) and Change (the user changes the object's value). To see these events, click the plus sign (+) beside the object.

Insert Actions: This section also displays a list of the page objects. In this case, though, each object has a list of the actions that can be performed on the object. Most of these actions involve changing the object's properties.
Screen shot of the Script Wizard
Figure 21.11.
The Script Wizard: A quick and dirty method of creating VBScript code.

You'll insert two actions:

  1. Open the CheckBox1 branch in the Select an Event list and highlight the Click event.
  2. Open the CommandButton1 branch in the Insert Action list and highlight Enabled.
  3. Click Insert Action. The Script Wizard displays the dialog box shown in Figure 21.12, which prompts you for a new value. In other words, you should decide what value you want for Enabled when the user clicks the check box.

    Screen shot of a command button dialog box
    Figure 21.12.
    Script Wizard will often prompt you for a new value.

  4. Since you set up the command button with Enabled as False, activate True and click OK.
  5. In the Insert Action list, highlight Caption and click Insert Action. This time, the Script Wizard prompts you to enter a text string. This will be the new caption after the user clicks the check box.
  6. Type Click Me and click OK.

You should now see two actions in the bottom pane, as shown in Figure 21.13. Click OK to return to the ActiveX Control Pad window. As you can see in Figure 21.14, the Script Wizard has added a <SCRIPT> tag and inserted the VBScript programming code that corresponds to the actions you selected. (Note that if you need to make changes to your script, you can click the Edit Script button to the left of the script.) Save your work and load the page into Internet Explorer. When you click the check box object that you added to the page, the command button will become active and the caption will change to "Click Me." Refresh the page to try it again.

Screen shot of the Script Wizard
Figure 21.13.
Inserted actions appear in the bottom pane of the Script Wizard window.

Screen shot of the Script Wizard
Figure 21.14.
The Script Wizard adds the programming code that corresponds to the actions you inserted.

NOTE: SWITCHING SCRIPTING LANGUAGES
The Script Wizard writes VBScript code by default. If you prefer to work with JavaScript, select Tools | Options | Script to display the Script Options dialog box. Activate the JavaScript option and click OK.

Go To VBA Unleashed Home Page Go To Top Go To Prev Go To Next


Copyright © 1995-2008 Paul McFedries and Logophilia Limited