DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 17.4 Call a Web Service from Access

17.4.1 Problem

A web service is a specially constructed component that you can access over standard web protocols. To call a web service, however, you must pass it messages encoded using Simple Object Access Protocol (SOAP). Access does not directly support the SOAP protocol. Is there any way to call a web service from Access?

17.4.2 Solution

Microsoft has released several toolkits that can be used by Microsoft Office programmers to call web services. This solution assumes you are using Access 2003 with Microsoft Office 2003 Web Services Toolkit. See the discussion section of this topic on calling web services from earlier versions of Access.

The RunnerCalculator web service contains a number of methods that provide pacing calculations for long distance running. This web service can be found at www.deeptraining.com/webservices. One of the RunnerCalculator methods, GetPaceDouble, can be used to calculate the pace in minutes per mile for a given distance and total time. Follow these steps to create an Access 2003 form that uses this web service to calculate pace for a user-entered distance and time:

  1. If you haven't yet done so, download and install the Microsoft Office 2003 Web Services Toolkit.

  2. Start Access 2003 and create an unbound form named frmPaceCalculator.

  3. Add the controls to the form listed in Table 17-1:

Table 17-1. Controls for frmPaceCalculator

Control

Name

TextBox

txtDistance

TextBox

txtHours

TextBox

txtMinutes

TextBox

txtSeconds

CommandButton

cmdCalculatePace

Label

lblPace

  1. From the VBA editor, select Tools Web Service References.... This menu item is added to the VBA editor by the Microsoft Office 2003 Web Services Toolkit.

  2. At the Microsoft Office 2003 Web Services Toolkit dialog box, select the Web Service URL radio button and enter the following address into the URL textbox:

    www.deeptraining.com/webservices/runnercalculator.asmx
  3. The RunnerCalculator service and its methods should be displayed in the SearchResults box. Check the checkbox to the left of RunnerCalculator and click the Add button at the bottom of the dialog box to add a reference to the RunnerCalculator service. The Microsoft Office 2003 Web Services Toolkit dialog box is shown in Figure 17-6.

Figure 17-6. You use the Microsoft Office 2003 Web Services Toolkit dialog box to locate a web service and set a reference to it
figs/acb2_1706.gif
  1. The toolkit adds a new class module to the Access project with the name clsws_RunnerCalculator. This class serves as a proxy for making calls to the web service. The code in this class will take care of speaking to the web service using the SOAP protocol.

  2. Attach the following code to the cmdCalculatePace button's Click event to use the proxy class to call the RunnerCalculator web service:

    Private Sub cmdCalculatePace_Click( )
        Dim prxRunnerCalc As clsws_RunnerCalculator
        Dim strResult As String
        
        On Error GoTo HandleErr
        DoCmd.Hourglass True
        
        ' Instantiate proxy class
        Set prxRunnerCalc = New clsws_RunnerCalculator
        
        If Len(txtDistance) > 0 And Len(txtHours) > 0 And _
         Len(txtMinutes) > 0 And Len(txtSeconds) > 0 Then
            ' Call GetPaceString method via proxy class
            strResult = prxRunnerCalc.wsm_GetPaceString(txtDistance, _
             txtHours, txtMinutes, txtSeconds)
            lblPace.Caption = "Average Mile Pace: " & strResult
        Else
            MsgBox "You must enter values for each text box.", _
             vbOKOnly + vbCritical, "Pace Calculator"
        End If
        
    ExitHere:
            On Error GoTo 0
            DoCmd.Hourglass False
            Exit Sub
            
    HandleErr:
        MsgBox "Error " & Err.Number & ": " & Err.Description, _
            vbOKOnly + vbCritical, "Pace Calculator"
        Resume ExitHere
    End Sub
  3. Save the form and open it in form view. Enter values into each of the textboxes and click on the Calculate Pace button. The form should look like the one shown in Figure 17-7.

Figure 17-7. When you click on the Calculate Pace button, code behind the button calls the proxy class, which calls the RunnerCalculator web service
figs/acb2_1707.gif

17.4.3 Discussion

When you set a reference to a web service using the Microsoft Office 2003 Web Services Toolkit, the toolkit creates a proxy class with the name clsws_webservice, where webservice is the name of the web service. The proxy class takes care of calling the web service using SOAP and processing the response, again using the SOAP protocol.

For every method of a web service, the Microsoft Office 2003 Web Services Toolkit creates a corresponding method of the proxy class with the name wsm_method, where method is the name of the web service method. Thus, calling the web service is as simple as instantiating the proxy class and calling the proxy method corresponding to the method in the web service.

17.4.3.1 Calling web services from earlier versions of Access

If you wish to call a web service from Access XP or an earlier version of Access the solution steps will differ from those shown here. If you're using Access 2002, you need to download and install the Microsoft Office XP Web Services Toolkit 2.0. The steps to use the Office XP toolkit are fairly similar to using the Office 2003 toolkit. If you need to call a web service from Access 2000 or an earlier version of Access, you'll need to use the Microsoft SOAP Toolkit 3.0. This toolkit is geared more towards Visual Studio 6.0 developers, but can also be used from VBA code.

17.4.3.2 Locating the toolkits

The Microsoft Office 2003 Web Services Toolkit can be found at http://www.microsoft.com/downloads/details.aspx?FamilyID=fa36018a-e1cf-48a3-9b35-169d819ecf18&DisplayLang=en.

The Microsoft Office XP Web Services Toolkit 2.0 can be found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxpwst2/html/odc_offxpwstoolkit2.asp.

The Microsoft SOAP Toolkit 3.0 can be found at http://msdn.microsoft.com/library/default.asp?URL=/downloads/list/websrv.asp.

17.4.4 See Also

Integrating XML Web Services Into Microsoft Office Solutions (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office09062001.asp).

    [ Team LiB ] Previous Section Next Section