The Visual Basic Code

To see the Visual Basic code within the Excel file, from the Tools menu, select the Macro menu, then click Visual Basic Editor. This opens up a Visual Basic Editor; select Sheet1 in the Project Explorer to see the source code.

The code must be familiar to you if you read through the last Visual Basic example. On loading the Excel file, the InitializeLC() function is performed. It instantiates ServiceFactory, and creates an instance of the Service exceldemo.

Public Service As DSCOMDRIVERLib.Service
Sub InitializeLC()
Dim f As New DSCOMDRIVERLib.ServiceFactory
On Error GoTo iError
If (Service Is Nothing) Then
Set Service = f.createService("exceldemo", "", Nothing, Nothing)
End If
MsgBox ("GridServer connection established.")
GoTo iEnd
iError: MsgBox ("GridServer Service not found: " & "(" & Err.Description & ".)")
iEnd:
End Sub

The Visual Basic function that executes the calculation on GridServer is called runLiveCluster. It calls the Java function valueTrade of the Service asynchronously using the Submit function in COMDriver, sequentially from the first to the last row. The function takes one string argument: all the values on the row of the deal, concatenated into one string, delimited by spaces.

The code correctly assumes that Invocation IDs returned by the submit function are sequential. The invocation ID of the last call is written to the variable result. This value is important for indexing the results later on.

For rwIndex = begRow To endRow
Dim a As String
a = Cells(rwIndex, 2).Value & " " & Cells(rwIndex, 3).Value & " " _
& "0.05 " & Cells(rwIndex, 4).Value & " " & Cells(rwIndex, 5).Value & " " _
& Cells(rwIndex, 6).Value & " " & Cells(rwIndex, 7).Value & " " & sims
Dim result
result = Service.submit("valueTrade", a, Nothing)
Next rwIndex

Since submit is an asynchronous call, the results are collected separately. The code calls the collectNext function repeatedly until the number of results collected is equal to the number of rows. Alternatively, it can use Service.InvocationCount, to determine if there are still pending results. See the previous example for an implementation of this.

The row number is calculated by offsetting the invocation IDs by the variable result:

'   now collect
Dim count As Integer
While count < endRow - begRow + 1
Dim data As ServiceData
Set data = Service.collectNext(100)
If Not (data Is Nothing) Then
Cells(data.ID + begRow - (result - (endRow - begRow)), 7).Value = data.data
count = count + 1
End If
Call updateProgress(count * 100# / (endRow - begRow + 1))
Wend