Access Cookbook - Ken Getz [322]
If you haven't yet done so, download and install the Microsoft Office 2003 Web Services Toolkit.
Start Access 2003 and create an unbound form named frmPaceCalculator.
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
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.
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
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
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.
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
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
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.
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