Export (0) Print
Expand All
Expand Minimize

Essentials of Creating Web-Based Applications

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
 

Wes Kim
Microsoft Corporation

April 2001

Applies to:
   Microsoft® Access 2002

Summary: Describes how to develop solutions based on data access pages in Microsoft Access 2002. (Part one of a three-part series.) (9 printed pages)

Contents

Introduction
What Is the Microsoft Script Editor?
VBScript Versus Visual Basic and VBA
VBScript Versus JScript
The Script Editor Object Browser
Creating an Event Handler
Manipulating an Object from a Script
Debugging and Error Handling

Introduction

With data access pages in Microsoft® Access, you can create Web views of your database in minutes. When you want to customize your page beyond what the data access page designer allows, you can add scripts that perform sophisticated actions based on the data currently being shown, use transitions to create multimedia effects, or tap into the power of the Microsoft Office Spreadsheet, Chart, or PivotTable® Web Components.

However, there are differences between developing form- and report-based applications and developing Web-based applications. This series of articles is designed for experienced Access developers who have mastered forms and reports and now want to use data access pages to create solutions for the Web.

In addition to knowledge about Access form and report development, this series assumes that you are comfortable with one or more of the following languages: Microsoft Visual Basic®, Visual Basic for Applications (VBA), Microsoft Visual Basic Scripting Edition (VBScript), or Microsoft JScript®. It will also help if you are familiar with HTML and Dynamic HTML (DHTML).

This series is divided into three parts:

  • Part one is an introduction to the Microsoft Script Editor and a comparison of scripting languages.
  • Part two will be an introduction to the Microsoft Office Data Source control and its methods, properties, and events.
  • Part three will be a survey of ten of the most common programming tasks related to forms and reports and how one would accomplish them in the world of data access pages.

What Is the Microsoft Script Editor?

The Microsoft Script Editor is an HTML and script-editing system and debugger. You can use the Script Editor to edit any data access page that you create in Access. To open the Script Editor, simply right-click any object on a data access page, and then click Microsoft Script Editor on the shortcut menu. The Script Editor will start and display the HTML source code for your page.

The standard windows of the Script Editor are as follows:

WindowDescription
ToolboxShows the HTML-intrinsic objects that you can insert in your document through drag and drop.
Document OutlineShows an outline of the HTML tags in your document.
CodeShows syntax-colored HTML for your document. You can select from the available scriptable objects and their events in the dropdown lists at the top of the Code window.
Project ExplorerShows all of the data access pages you have open in the Script Editor.
PropertiesShows properties for the document or currently-selected object.

VBScript Versus Visual Basic and VBA

VBScript is very similar to Visual Basic and VBA, but there are some important differences. This is particularly important if you copy code from Visual Basic or VBA to your data access page. For example:

  • There are no object constants in VBScript. In VBA, objects can have associated constants. These aren't available in VBScript. Whenever you need an object constant value, you must either use a literal value or you must declare a constant yourself with the appropriate value (by using Const name = value). To find an object constant value, look in the Microsoft Script Editor Object Browser (see below for more on using the Object Browser). The Office Web Components contain a constants object that can be used for this purpose (for example, chartN.constants.chChartTypePie has the value 18).
  • Dim statements are implicitly typed as Variants. An error occurs if you attempt to strongly type a Dim statement.
  • Arguments in Sub and Function procedure definitions are implicitly typed as Variants. An error occurs if you attempt to strongly type such arguments.
  • Many of the built-in functions of VBA are not available in VBScript. To find the functions that are available, consult the VBScript Language Reference at the Microsoft Windows Script Technologies Web site.

A good guideline to follow is that VBScript is a strict subset of VBA. If something doesn't work in VBA, it won't work in VBScript. However, not all things that work in VBA will work in VBScript.

VBScript Versus JScript

If you're used to using VBA when you're creating an Access application, then you'll probably want to use VBScript in your data access page. VBScript is the preferred language for several reasons:

  • The Visual Basic family of languages was designed with the goal of being easy to understand.
  • VBScript is a subset of VBA, so knowledge of Visual Basic or VBA can be immediately applied to VBScript.
  • The Visual Basic family of products has over three million developers and hundreds of books have been written about Visual Basic.
  • The ability to format dates, numbers, and currency is built into the VBScript language.
  • VBScript has better Component Object Model (COM) integration.
  • VBScript is case-insensitive which eliminates errors caused by incorrect capitalization.

If you want to use JScript instead of VBScript, you need to change the defaultClientScript property setting in the Script Editor document Properties window from VBScript to JavaScript. If you use JScript, remember that it is case-sensitive with respect to keywords.

The Script Editor Object Browser

The Script Editor Object Browser is similar to the Visual Basic Editor Object Browser.

To open the Object Browser, click Object Browser on the Standard toolbar or press CTRL+ALT+J. The first time you open the Object Browser, you'll need to choose which object libraries appear in it. To do this, in the Browse dropdown list, select Selected Components, and then click Customize.

Next, click Add and select the object libraries you want to appear in the Object Browser. At a minimum, you'll want to select the following:

  • Microsoft Office XP Web Components
  • Microsoft ActiveX Data Objects 2.5 Library

You may also find the following libraries useful if you need to automate these applications from a data access page:

  • Microsoft Access 10.0 Object Library
  • Microsoft Outlook 10.0 Object Library
    Caution
       
    You must carefully use the information provided by the Script Editor Object Browser and the auto list members box that appears when you type a period (.) in an expression. The Script Editor does not indicate that some functions can return either a single object or an array of objects. In such cases, the Script Editor will either show the return type as a generic object or as a single object, even though the return type could be an array of objects.

Creating an Event Handler

VBScript

Follow these steps to create a button and add a VBScript event handler:

  1. In Access, create or open a data access page in Design view.
  2. Click Command Button in the toolbox, and then click on your data access page where you want the button to appear. Click Cancel if the Command Button Wizard appears (the wizard is useful when you want to use one of the predefined button types, but it's not necessary for this example).
  3. On the Tools menu, point to Macro, and then click Microsoft Script Editor.
  4. Select the new button from the Object dropdown list at the top left of the Code window.
  5. From the Event dropdown list, select the onclick event. The Script Editor will insert an event handler for the event (or scroll to the handler if it already exists).
  6. You should see the following code:
    <script language=vbscript for=Command0 event=onclick>
    <!--
    
    
    -->
    </script>
    
  7. Add new code to the handler. For example:
    <script language=vbscript for=Command0 event=onclick>
    <!--
    MsgBox "here"
    -->
    </script>
    
    
  8. On the View menu, click Designer (or press SHIFT+F7) to return to the data access page designer, and then, in the Page view, click Page View.
  9. Click the command button. The code you inserted should run; in the case above, a message box should appear.
    Caution
       
    The version of the Script Editor included with Access does not insert arguments for event handlers. This means that VBScript handlers for events that take arguments will not be created properly and your handler will not be called as expected. To fix this problem, you'll need to add the arguments by hand. For example, if you insert a handler for the Current event of the Data Source control, you'll see:
    <SCRIPT for=MSODSC event=Current language=vbscript>
    
    

    Since the Current event takes an argument that provides a DSCEventInfo structure, you'll need to edit the handler so it reads:

    <SCRIPT for=MSODSC event=Current(info) language=vbscript>
    
    

JScript

To create a JScript event handler, you first need to change the defaultClientScript property setting in the Script Editor document Properties window from VBScript to JavaScript. The steps for a JScript event handler are the same as for a VBScript event handler above, except for the appearance of the code in steps six and seven:

  1. You should see the following code:
    <SCRIPT id=clientEventHandlersJS language=javascript>
    <!--
    
    function Command0_onclick() {
    
    }
    
    //-->
    </SCRIPT>
    
    
  2. Add new code to the handler. For example:
    <SCRIPT id=clientEventHandlersJS language=javascript>
    <!--
    
    function Command0_onclick() {
        alert("here")
    }
    
    //-->
    </SCRIPT>
    
    

Manipulating an Object from a Script

Follow these simple steps to add a field and create some code that manipulates the field.

  1. In Access, on the View menu, click Design View.
  2. Drag a field from the Field List window to the fixed-positioning section grid. If the Layout Wizard dialog box appears, click Cancel.
  3. On the Tools menu, point to Macro, and then click Microsoft Script Editor.
  4. Change the handler you inserted above to:
    <script language=vbscript for=Command0 event=onclick>
    <!--
    fieldname.Value = somevalue
    -->
    </script>
    
    

    In this case, substitute your field name and a desired value where fieldname and somevalue appear. For example:

    <script language=vbscript for=Command0 event=onclick>
    <!--
    CityName.Value = "Kalamazoo"
    -->
    </script>
    
    
    Note
       
    Unlike Visual Basic and VBA, VBScript doesn't support the concept of default properties, which is why the Value property is named explicitly in the code above.
  5. On the View menu, click Designer (or press SHIFT+F7) to return to the data access page designer, and then, in the Page view, click Page View.
  6. Click the command button and notice that the field changes. If you close the data access page and view the table, you'll see that the value has changed there as well.
    Important You don't need to save your page before switching between the Script Editor and Access. Any changes you make in either the Script Editor or Access will automatically appear in the other application when you switch between them. However, if you're viewing them side-by-side, you'll need to activate each application in order for the changes you've made in the other application to appear.

Debugging and Error Handling

To debug your scripts, you need to turn on the debugging options in the Script Editor.

Note
   
The Web Debugging tools are not installed by default. The first time you view the Debug menu, the only available option will be Install Web Debugging. Select this option, and then close and restart the Script Editor to enable the Web Debugging tools.

On the Tools menu, click Options, click Debugging, and then click JIT Settings. In the Program Types list, select Script, and then check the Enable Attach and Enable JIT debugging check boxes. Once you've done this, any error in an executing script will bring up a dialog box asking if you want to debug; when the dialog box appears, click Yes to switch to the Script Editor Debugger.

To use breakpoints, you must run your page in Internet Explorer by clicking Start on the Script Editor's Standard toolbar. You can set breakpoints before or after you start Internet Explorer. Make sure that you save the page before clicking Start so that the latest copy of your page will be available to Internet Explorer (unlike Access, Internet Explorer doesn't automatically synchronize with the copy you're editing in the Script Editor).

Since the Script Editor is a separate application from Internet Explorer, the debugger isn't as tightly integrated with the editor as the VBA debugger and editor are. For example, the Immediate and Watch windows aren't active unless you're stopped at a breakpoint or an error. This means, for example, that you can't put your windows side-by-side, set up a watch variable and see it change while your script is executing unless you break into the debugger and single-step through the script.

Note
   
Don't forget to close the debugger when you're done. Otherwise, you'll get a proliferation of open debugger windows, because each entry into the debugger opens up a new copy of the Script Editor.
Caution
   
Clicking Stop in the Script Editor will not stop an executing script—it only stops debugging. If the page is running in Access, it does the same thing as the Start button at that point. If you were stopped at an error, you can stop execution by clicking Stop, and then clicking No in the dialog box in Access. If you click Stop when Internet Explorer is running the page, Internet Explorer (not just the script) will terminate.

Creating a Debug Log

In VBA, it's common to use the MsgBox function to provide information for debugging purposes. In data access pages, you must be careful about doing so. Because the Microsoft Office Data Source control is running in a separate thread from Internet Explorer, using the MsgBox function can change the order in which events occur. A better option would be to create a debug log that records information while the program is running. Later, you can inspect the log to determine why the program didn't work as expected. When your page is running in Internet Explorer, it's easy to create a debug log in a separate window. The following script creates two routines that you can call from your code to provide debug output.

<SCRIPT language=vbscript>
Dim debugWindow

Sub StartDebug
    Dim id      ' Deliberately unassigned variable.
    Set debugWindow = window.open("about:blank", id, _
        "menubar=no,status=no,toolbar=no," & _
        "resizable=yes,scrollbars=yes," & _
        "left=20,top=20,width=400,height=300")
    debugWindow.document.open("text/plain")
    debugWindow.document.clear
    debugWindow.document.bgColor = "FFFFC0"  ' Lt yellow.
    debugWindow.document.writeln("Debug Log for " & _
        document.title)
End Sub

Sub SendDebug(str)
    debugWindow.document.writeln (str)
End Sub
</SCRIPT>

Once you've defined these procedures, you can use them in your code:

StartDebug
...
SendDebug "debug text"

If you want to use HTML code in your debug output, replace the "text/plain" on the document.open with "text/html" and make sure that you pass valid HTML to the SendDebug routine.

Using the Watch Window

Because the object model can sometimes be hard to figure out and some portions of the object model are available only at run time (so they're not shown in the Object Browser), the Watch window in the Script Editor Debugger can be a very useful tool. To use it, set a breakpoint in an event handler, and then launch Internet Explorer from the Script Editor. When the breakpoint is triggered, type in the names of the variables you want to inspect.

Error Handling

One way to make sure that your Web pages work better is to use VBScript error handling for defensive programming. To turn VBScript error handling on, use:

On Error Resume Next

To turn VBScript error handling off, use:

On Error Goto 0

After an error has occurred, the Err object may be queried to determine if an error occurred. The Err object will be valid until an Err.Clear statement is executed or another error occurs. For example:

On Error Resume Next
. . .
If (Err.Number <> 0) Then
    . . .
    Err.Clear
End If
On Error Goto 0

Another common way to use error handling is to assume a default value or action if an error occurs. For example, the following code sets value to be x/y or 0 if an error occurs (for example, a division error if y is 0).

On Error Resume Next

' We assume here that the values of x and y have already been defined.
value = x/y

If (Err.Number <> 0) Then
    value = 0
    MsgBox "The following error has occurred:" & vbCRLF _
        & "Number: " & Err.Number & vbCRLF _
        & "Description: " & Err.Description
End If

On Error GoTo 0

Note
   
The On Error Resume Next statement applies only to the current script handler. When the handler returns, normal error handling is resumed.

Next in this series: The Microsoft Office Data Source control

Show:
© 2014 Microsoft