Using Data from Web Services


Before you can consume a web service, you must download and install the Microsoft Office 2003 Web Services Toolkit. Further, the toolkit requires that you have at least one product from Microsoft Office 2003 installed on your machine. As of this book’s writing Microsoft has not indicated whether a newer 2007 version will be released or not. At the time of this book’s writing, the easiest way to obtain the toolkit is to go to www.microsoft.com/downloads/details.aspx?familyid=&displaylang=en

Once you get the toolkit installed, the example in this section will work with Access 2007. If you cannot install the Toolkit, move on to the next section.

If you cannot locate the toolkit at the preceding link, you can go to the downloads area of Microsoft.com: www.microsoft.com/downloads. Then, select the Office and Home Application Download Category in the left navigation pane and then run the following search: Select Office System for Product/Technology and type Web Services Toolkit in the Keywords text box. Click the Search button. You will see a link to the Microsoft Office 2003 Web Services Toolkit 2.01 or later.

You can then follow the instructions on the page to download and run the setup program or just launch the setup directly from the Web site. Follow the onscreen instructions to complete the installation.

After you have the Web Services Toolkit installed, you can use web services from your Access applications.

Try It Out-Adding a Reference to a Web Service

image from book

Now, let’s confirm that the Web Services Toolkit installation was successful and then add a reference to your Access project to an existing web service.

  1. Confirm that the Web Services Toolkit installation was successful. To do so, open the Visual Basic Editor and select Tools to confirm that you have a Web Services References option, as shown in Figure 8-1.

    image from book
    Figure 8-1

  2. Select Tools image from book Web Services References. Select the WebServiceURL option, and type the following URL into the box: http://terraserver-usa.com/TerraService2.asmx. Click the Search button. A screen similar to that shown in Figure 8-2 will be displayed.

    image from book
    Figure 8-2

    Tip 

    You will need a connection to the Internet, or the web services examples shown here will not function.

  3. Select the TerraService web services in the upper-right pane so that a check box appears. Also, expand the list so you can see the procedures available, including the procedures shown in Figure 8-3.

    image from book
    Figure 8-3

  4. Select the Add button to add a reference to the Terra Web Service to your form.

  5. When you return to the Visual Basic Editor, you can see that several class modules were added to your project, as shown in Figure 8-4.

    image from book
    Figure 8-4

How It Works

To add a reference to a web service, you first selected the Tools image from book Web Services References option. Because you’re planning to use a service located at http://terraserver-usa.com/TerraService2.asmx, you selected the Web Service URL option and specified the Terra Server path in the URL field of Figure 8-2. You also could have searched for available web services using certain keywords or specified the location of another web service.

You then selected the Terra Server web service and clicked the Add button. Several class modules were added to the database to make the procedures you saw in Figure 8-3 available to your Access project.

You can add a reference to any web service that is available on your network over the Internet, or on your local computer, by navigating to its location. For example, Microsoft, Amazon, Google, and others have created web services that you can use. More information about these web services can be found at http://msdn.microsoft.com/webservices/building/livewebservices/. Many of these web services, although they are free, require that you obtain an ID. Other companies may charge for use of web services they offer.

Now that you have a reference to the Terra Server web service, you can consume one of its procedures from within your application.

image from book

Try It Out-Consuming the GetPlaceFacts Method

image from book

In this example, you will call one of the procedures in the Terra Server web service. The web service procedure you will call accepts latitude and longitude values and returns the city that is located at those coordinates.

  1. Add the following procedure to your class module:

      Sub TestWebService() 'declare a new instance of the web service Dim ts As clsws_TerraService Set ts = New clsws_TerraService 'declare a structure to hold the latitude and longitude values Dim objLonLatPt As New struct_LonLatPt 'declare a variable to store the result from the web service Dim strResult As String 'assign the latitude and longitude values objLonLatPt.Lat = "37.7875671" objLonLatPt.Lon = "-122.4276" 'Call the web service to return the place for that latitude 'and longitude strResult = ts.wsm_ConvertLonLatPtToNearestPlace(objLonLatPt) 'display the result MsgBox "The city at that latitude and longitude is: " & strResult End Sub 

  1. Run the procedure from the Immediate Window by typing TestWebService and pressing Enter. You should receive a message similar to the one in Figure 8-5.

    image from book
    Figure 8-5

How It Works

To call the web service from your application, you created the TestWebService procedure:

 Sub TestWebService()

You declare a new instance of the web service, just as you would declare any other object:

 'declare a new instance of the web service Dim ts As clsws_TerraService Set ts = New clsws_TerraService

You declare a structure variable to store the latitude and longitude values. You created the structure definition in the code that was generated when adding the web service, so all you had to do was declare the variable:

 'declare a structure to hold the latitude and longitude values Dim objLonLatPt As New struct_LonLatPt

Next, you declared a local string variable to store the result of the web service:

 'declare a variable to store the result from the web service Dim strResult As String

You also set the latitude and longitude parameters of the objLonLatPt that will be passed to the web service as parameters:

 'assign the latitude and longitude values objLonLatPt.Lat = "37.7875671" objLonLatPt.Lon = "-122.4276"

This line below actually calls the web service on the Terra Server over the Internet and returns the result into the strResult variable. If you do not have an Internet connection, this line will raise an error.

 'Call the web service to return the place for that latitude 'and longitude strResult = ts.wsm_ConvertLonLatPtToNearestPlace(objLonLatPt)

As you typed the line of code, you should have noticed that the tooltip displayed to show you what parameters the web service required. Figure 8-6 shows an example of this.

image from book
Figure 8-6

The last line of code displays the result that was retrieved from the web service:

 'display the result MsgBox "The city at that latitude and longitude is: " & strResult End Sub

When running the procedure from the Immediate Window, you receive a dialog box like Figure 8-5 that indicates that San Francisco, California, USA is located at those coordinates. It is really amazing that you just executed a remote procedure that was located on someone else’s computer and used the result in the application. Hopefully, you realize that it is almost as easy to call a web service as it is to call any other procedure. The only difference is that you have to locate and add a reference to the web service and familiarize yourself with the parameters it expects.

image from book




Beginning Access 2007 VBA
Beginning Access 2007 VBA
ISBN: 0470046848
EAN: 2147483647
Year: 2004
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net