Sample code in Visual Basic

I extracted this little function from an old version of Conejo Systems' CaseTrack® application. The function establishes a user connection to a backend database by sending a set of parameters to a "query packager" (a middle-tier module that essentially acts as a proxy for a DBMS or other data provider).

Why VB?

Choosing the "best" language for an application is not always easy. At the time, I decided to use Visual Basic for two main reasons:

· The CaseTrack application is essentially a data-entry program that demands stability, high performance, and fine control over the details of user interaction. When the application was first implemented back in the 1990s, the only way to achieve these goals was to use a compiled language such as VB or C++ to build a customized visual interface.

· VB6's built-in "knowledge" of COM made it a better tool for testing and using COM components.

Although I have never cared much for Basic as a programming language, I liked VB6's IDE better than that of Visual C++ for building and testing an application's visual interface.

Now that Microsoft has pulled VB together with C# and C++ into Visual Studio .NET, I see no compelling reason to use VB for new software development. As for CaseTrack, it was long ago rewritten in C#.

'
' ConnectStartSession
'
Private Function ConnectStartSession(ByVal sUID$, ByVal sPwd$) As Boolean
   
    Dim oRS As ADOR.Recordset
    Dim qpp() As QPParam
    Dim qpe As QPErr
 
    On Error GoTo erhCSS
 
    ' disconnect any previous session
5   Disconnect

    ' create a new QPSession object
10  Set oQPS = New QPSession

    ' use the specified userID and password to get a
    '  session handle from the database server
20  QPPParam0 qpp, "UID", sUID
22  QPPParam qpp, "B", FormatPwdChars(sPwd)
24  QPPParam qpp, "Machine", CkWI.sMachine
30  Set oRS = oQPS.DoSnapshot("CkSessionStart", qpp, qpe)

100 If qpe.nErrNum Then Err.Raise qpe.nErrNum

    ' save session info
110 qpsi.hCkS = oRS.Fields("hCkS").Value
120 qpsi.sCkUID = sUID

resCSS:
    ' set this function's return value
900 Set oRS = Nothing
910 ConnectStartSession = (qpsi.hCkS <> 0)
    Exit Function

' ------------
erhCSS:
    If Not DefQPError(qpe) Then _
      DefCkError sModuleName, "ConnectStartSession", Erl

    ResetSessionInfo
    Resume resCSS 

End Function
    

Some comments on the code

The QPSession object implements the query packager. Line 10 creates an instance of this object that persists for the lifetime of the VB module that contains this function.

Lines 20-30 set up a data structure (qpp) with a list of parameters that are passed to the query packager by the call to DoSnapshot.

Lines 100-120 copy the session parameters returned by the query packager into global variables.

The error handler (at label erhCSS) calls two functions, DefQPError and DefCkError, that format data extracted from VB's error object and display a message to the user of the application.

The line numbers are used only by the error handler to indicate the exact location of runtime errors.

This page updated Friday, October 21, 2005 at 22:39 UTC.

This page is valid XHTML and uses valid CSS.