Access Cookbook - Ken Getz [324]
Dim prxRunnerCalc As clsws_RunnerCalculator
Dim nlDS As MSXML2.IXMLDOMNodeList
Dim i As Integer
' Clear any existing items from the listbox
For i = lstSplits.ListCount - 1 To 0 Step -1
lstSplits.RemoveItem (i)
Next
' Add the headings to the list
lstSplits.ColumnWidths = "0.35"";1"""
lstSplits.ColumnHeads = True
lstSplits.AddItem ("Mile;Split")
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 GetMileSpilts method via proxy class
' This method returns a .NET DataSet which gets
' serializedd into XML.
' XML is returned by the proxy as the type
' MSXML2.IXMLDOMNodeList.
Set nlDS = prxRunnerCalc.wsm_GetMileSplits(txtDistance, _
txtHours, txtMinutes, txtSeconds)
Call ProcessSplits(nlDS)
Else
MsgBox "You must enter values for each text box.", _
vbOKOnly + vbCritical, "Splits Calculator"
End If
ExitHere:
On Error GoTo 0
DoCmd.Hourglass False
Exit Sub
HandleErr:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbOKOnly + vbCritical, "Splits Calculator"
Resume ExitHere
End Sub
Add the following code to the module to use the MSXML component to process the returned XML data and add the split values to the lstSplits listbox:
Private Sub ProcessSplits(nlDS As MSXML2.IXMLDOMNodeList)
Dim nlPace As MSXML2.IXMLDOMNodeList
Dim nodData As MSXML2.IXMLDOMNode
Dim nodRow As MSXML2.IXMLDOMNode
Dim nodField As MSXML2.IXMLDOMNode
Dim strItem As String
On Error GoTo HandleErr
' Grab the second node -- the data -- from the
' returned node list
Set nodData = nlDS.Item(1)
' Get the Pace nodes (rows)
Set nlPace = nodData.selectNodes("//MileSplits/Pace")
' For each Pace node
For Each nodRow In nlPace
' Get the child nodes of Pace, i.e., the fields
For Each nodField In nodRow.childNodes
Select Case nodField.nodeName
Case "Mile"
' Grab the Mile value
strItem = nodField.nodeTypedValue
Case "SplitString"
' Grab the SplitString value
strItem = strItem & ";" & nodField.nodeTypedValue
' Add the strItem value to the listbox
lstSplits.AddItem strItem
End Select
Next
Next
ExitHere:
On Error GoTo 0
DoCmd.Hourglass False
Exit Sub
HandleErr:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbOKOnly + vbCritical, "Process Splits"
Resume ExitHere
End Sub
Save and open the form to test it out. Enter values into each of the textboxes and click on the Calculate Splits button. The form should look similar to the one shown in Figure 17-8.
Figure 17-8. The code behind the Calculate Splits button calls the RunningCalculator service's GetMileSplits method, processes the returned serialized DataSet, and adds the splits to the listbox
Discussion
When you establish a reference to a web service using the Microsoft Office 2003 Web Services Toolkit, the toolkit, among other things, sets a reference to the Microsoft XML v 5.0 type library, which allows you to use MSXML without having to manually set a reference to the type library.
Processing the returned XML
The MSXML component contains a number of objects, properties, and methods for working with XML documents. You can find online documentation for MSXML at the following URL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/htm/xml_obj_
ixmldomnodelist_4kvo.asp
In order to create the code that processes a serialized DataSet using MSXML you need to understand the layout of the XML returned by the web service method. For .NET web services, you can obtain basic documentation about the web service and its methods by directly navigating to the web service (the asmx file) using Internet Explorer. Thus, for the RunnerCalculator service, you could obtain information about the web service at this address:
www.deeptraining.com/webservices/runnercalculator.asmx
When you do this you should see a screen that looks similar to the one shown in Figure 17-9.