Using Server Scriptlets in Visual Basic

Once you have written a Server Scriptlet you have, inadvertently, written a COM component. Hence, you can call it through any tool that supports COM components. Visual Basic is certainly the first in line. Our Date Calculator module must be instantiated in Visual Basic code using the CreateObject function.

Dim g_sctDateCalc As Object
Set g_sctDateCalc = CreateObject("DateCalculator.Scriptlet")

The following code shows how to create such an object. From now on, the g_sctDateCalc variable can be used to invoke the methods and properties of the scriptlet following the usual object-oriented syntax.

Notice that unlike the DHTML Scriptlets you don't require additional ActiveX control to exploit Server Scriptlets. The figure shows a sample program you can download from our Web site. The file is called vbsct.zip. It uses the Date Calculator Server Scriptlet to make date additions and subtractions. Once you download the file you actually find two files—a project file and a form. The source code of the .frm form file is the following:

Dim g_sctDateCalc As Object

Private Sub cmdAdd_Click()
  txtDate2.Text = g_sctDateCalc.Add(txtDate1.Text, Val(txtDays.Text))
End Sub

Private Sub cmdDate_Click()
  g_sctDateCalc.Separator = txtSep.Text
  MsgBox g_sctDateCalc.DateAsDDMMYYYY("")  ' Pay attention here!!!
End Sub

Private Sub cmdDiff_Click()
  txtDays.Text = g_sctDateCalc.Diff(txtDate2.Text, txtDate1.Text)
End Sub

Private Sub cmdGreater_Click()
  b = g_sctDateCalc.IsGreaterThan(txtDate2.Text, txtDate1.Text)
  If b Then
    MsgBox "<" + txtDate2.Text + "> is greater than <" + txtDate1.Text + ">"
  Else
    MsgBox "<" + txtDate1.Text + "> is greater than <" + txtDate2.Text + ">"
  End If
End Sub

Private Sub Form_Load()
  Set g_sctDateCalc = CreateObject("DateCalculator.Scriptlet")
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Set g_sctDateCalc = Nothing
End Sub

Notice that the scriptlet is instantiated during the form load and released while unloading. The sample program adds the number of days to the first date field and displays the resulting date in the second field. It also subtracts the first date field from the second and places the result, in days, in the correspondent edit box. Furthermore, it compares the two dates and displays the current date—formatted as dd/mm/yyyy—using the specified separator.

In the source above there's a comment in the correspondence of the line:

MsgBox g_sctDateCalc.DateAsDDMMYYYY("")  ' Pay attention here!!!

If you look back at the source code for this method you will notice that it doesn't take any parameter as input. So why exactly are we passing an empty string in the Visual Basic call? I must confess that the reason for this is beyond me, however, if you call it omitting that "additional" argument what you get is

instead of the following (what we'd like to have):

The same didn't occur in our previous HTML test page.

This is a known bug in the first beta version. It will be fixed in the second beta release of the package.

© 1997 by Wrox Press. All rights reserved.