Code Examples

The main purpose of these code examples is to highlight and explain particular sections of the code used to access BEA TUXEDO servers. This should help to familiarize you with the process of integrating the sample code Active Expert generates into your Visual Basic applications.

The full code in the sample application files is fully annotated to explain what each section of code accomplishes, and how and where the sample code Active Expert generates is used. Two types of comments appear throughout the sample application code:

Keep in mind that the sample application code is presented as an example of how you can use Active Expert API, and does not necessarily imply application design recommendations. It is likely that you will find many ways to leverage the generated sample code to fit with your own design techniques, preferences, and the requirements of your particular application.

Note: This section assumes that you are familiar with the Active Expert Application Builder graphical user interface (GUI). If you need help on using the GUI, refer to the help on Main Window and Tasks.

This section includes explanation of the following functionality in the sample application:

The code examples shown here are taken directly from the sample BANKAPP project forms. You can also see code for the dialogs in the BANKAPP project by choosing View Code on the selected form in Visual Basic (Figure 5-13 shows an example of this), or by opening the files with a text editor.

Figure 5-13 Viewing Code on a BANKAPP Dialog in Visual Basic

For the purposes of understanding how we got the generated sample code for the examples in this chapter, make sure that you have the following sample code options set to on in the Active Expert Application Builder:

To do this choose, Tools->Options from the Active Expert Application Builder menu, and click on the Sample Code options tab. Click the exception handling and transaction object options so that checkmarks show beside them, and click OK to save your changes.

Now, to see the example code in the Active Expert Application Builder, simply select the appropriate object in the Workstation Objects main window view and click on the Declarations, Usage, or Cleanup tabs. In this way, you can follow along with the code examples using both the Active Expert Application Builder sample code displays and the full code text files in Visual Basic.

Note: Unless otherwise noted in the following sections, assume that the Active Expert sample code is using in synchronous mode. (For information on how to switch between synchronous and asynchronous sample code modes in the Active Expert, refer to help on Using Synchronous and Asynchronous Modes.)

Global Declarations

The file Global.bas contains declarations and routines that are used globally by the other forms in the project.

We begin by defining BEA Session and Transaction objects and constants for exception handing. The global declarations below have been synthesized from the Active Expert sample declaration code for the BANKAPP module. We place them in the "global" space so that they are available to all forms.

Option Explicit
Global Session As Object
Global Transaction As Object
Global TransException As Variant
Global Const NoException = 0
Global Const SystemException = 1
Global Const UserException = 2

After this we add various other declarations we want to be global to the application. For example, to keep track of whether the Session object is active across all forms, the following object is declared:

Global sessionActive As Boolean

Also, objects are declared for field edit routines and functions are declared to edit the input to various text boxes in forms within the project. You can view all global declarations for the sample application in the file Global.bas.

Logon

The Logon dialog presents a logon window to the user, edits input data, creates the Session object for the entire application, and then attempts to log on to the BEA TUXEDO application configured in /WS. This functionality relies on the standard BANKAPP servers. You can find the code for the Logon Dialog in the file LogonDialog.frm.

Declare Variables for BEA TUXEDO Logon

We begin by declaring the variables needed for a BEA TUXEDO logon via Session.logon. This code is copied directly from the Active Expert sample declaration code for the BANKAPP module as shown in Figure 5-14. (To see this code in the Active Expert, select the BANKAPP module in the Workstation Objects main window view and click on the Declarations tab.)

Const SecAuthSuccess = 1
Const SecAuthFailure = 2
Dim UserData(0) As Byte
Dim ret As Integer
Figure 5-14 Copying BEA TUXEDO Logon Variables from Active Expert to Visual Basic Logon Form

Note that the sample code for the BANKAPP module in the Active Expert also includes a declaration for the Session object and the BACKOFFICE object which we have not included here. That is because we included the Session object in our global declarations ("Global Declarations") and BACKOFFICE in the file BackOfficeDialog.frm where we describe the BACKOFFICE interface.

Create the BEA TUXEDO Session Object

Next, we create the Session object with global scope for the entire application during Form_Load(). The line that creates the session object is copied directly from the Active Expert usage code for the BANKAPP module. (To see this code in the Active Expert, select the BANKAPP module in the Workstation Objects main window view and click on the Usage tab.)

Private Sub Form_Load()
Set Session = CreateObject("Tobj.Session")
End Sub

Log on to the BEA TUXEDO Application

When the user selects the Logon button on the Logon dialog, the cmdLogon_Click function performs all actions necessary to log on to the BEA TUXEDO server.

The following code executes the BEA TUXEDO logon and checks for exceptions upon logon. If the logon fails, an alert message is posted. This code is synthesized from the Active Expert sample usage code for the BANKAPP module. (To see the generic logon code in the Active Expert, select the BANKAPP module in the Workstation Objects main window view and click on the Usage tab.)

Note: For the session logon in the sample application we have added in our own variables for user name, client, name, and user data. Also, if the logon is successful we have added code that posts a logon confirmation message and enables the sample application menus.

Private Sub cmdLogon_Click()
ret = Session.logon(Trim(txtUserName.Text), Trim(txtClientName.Text), _
Trim(txtAppPassword.Text), Trim(txtUserPassword.Text), UserData)
If ret = SecAuthFailure Then
MsgBox "Logon failed", _
(vbOKOnly + vbInformation + vbDefaultButton1), _
Else
MsgBox "Logon successful", _
(vbOKOnly + vbInformation + vbDefaultButton1), _
"Logon"

Create the Transaction Object from the Session Object

After successfully logging on, we create the Transaction object from the Session object. We do this once at the beginning of the application with global scope so that the Transaction object is available to all modules from a single reference point.

The code for creating the Transaction object and setting the timeout is copied from the Active Expert sample usage code for the BANKAPP module.

        Set Transaction =          Session.get_current_context.resolve_initial_references("TransactionCurrent")
Transaction.set_timeout 500
Unload LogonDialog
End If
End Sub

OLE Automation Using a Synchronous Interface (Back Office Dialog: Open Account)

The Back Office dialog is a tabbed dialog that allows a user to submit Open Account and Close Account transactions to the BANKAPP servers. These transactions can be found in the BACKOFFICE interface within the BANKAPP module.

You can view the code for the Back Office dialog in the file BackOfficeDialog.frm. (Note that a single form is used to present both the Open Account and Close Account transactions within the one dialog.)

Within the BACKOFFICE interface, the Open Account and Close Account operations are examples of how to use operations with a synchronous OLE automation interface. For the purposes of this example, we focus on the declarations for the interface and its operations, and the code used for the Open Account operation (OPEN_ACCT)-but you can just as easily consider the Close Account (CLOSE_ACCT) code in this context.

(For information on how to switch between synchronous and asynchronous sample code modes in the Active Expert, refer to help on Using Synchronous and Asynchronous Modes.)

Declare the Interface as an Object

BACKOFFICE is an Automation Server interface type, so it is declared as an object. The code to declare BACKOFFICE as an object is copied directly from the Active Expert sample declarations code for the BACKOFFICE interface. (To see this code in the Active Expert, select the BACKOFFICE interface in the Workstation Objects main window view and click on the Declarations tab. To ensure that BACKOFFICE is set to Automation Server, you can choose the menu bar option View->Properties in the Active Expert while the BACKOFFICE interface object selected in the Workstation Objects view.)

Dim BACKOFFICE As Object

Create the BackOffice Object

Next, we create the BACKOFFICE interface object during Form_Load(). The operations OPEN_ACCT and CLOSE_ACCT on this object will be called to invoke the BEA TUXEDO service of the same name. The line that creates the BANKAPP object was copied directly from the Active Expert usage code for the BACKOFFICE interface.

Private Sub Form_Load()
Set BACKOFFICE = CreateObject("BANKAPP.BACKOFFICE")
End Sub

We also need to destroy the BACKOFFICE object when the form unloads so that our application will exit properly.

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

Declare Parameters Used in the Operation

The following code declares all parameters used in the OPEN_ACCT operation. This code is copied directly from the Active Expert sample declarations code for the OPEN_ACCT operation.

Dim lBRANCH_ID As Long
Dim sSAMOUNT As String
Dim bACCT_TYPE As Byte
Dim sLAST_NAME As String
Dim sFIRST_NAME As String
Dim bMID_INIT As Byte
Dim sSSN As String
Dim sADDRESS As String
Dim sPHONE As String
Dim bOPEN_CR As Byte
Dim lACCOUNT_ID As Long
Dim sSBALANCE As String
Dim sSTATLIN As String

Note: Keep in mind that for the full sample application code for BackOfficeDialog.Frm, we made sure that the list of parameter declarations included those needed for CLOSE_ACCT as well. For the rest of this example, we do not mention CLOSE_ACCT again but you can refer to the sample application code to see similar code for CLOSE_ACCT.

Declare Exception Parameters

All operations return as their last parameter an exception object. For OPEN_ACCT, the exception structure is declared as OPEN_ACCTException. This code is copied directly from the Active Expert sample declarations code for the OPEN_ACCT exception parameter.

Dim OPEN_ACCTException As Variant

Edit Open Account Input Data

The cmdApply_Click function selects the Do_OpenAccount function when the user clicks on the Apply button for the Open Account dialog. Initially, the Do_OpenAccount function edits all input fields on the form and returns a message if an error is found.

Load the Input Parameters

If the user input data passes the validation checks, we need to load the input parameters in preparation for the Open Account transaction call to the BANKAPP server.

The following code is synthesized from the Active Expert sample usage code for the OPEN_ACCT operation. Some code is basically unchanged, while other code is modified to accept data from a different source. For example, lBRANCH_ID gets its value from the cboBranch combo box instead of directly from a text control.

Note that bACCT_TYPE is set in the functions optChecking_Click() and optSavings_Click().

lBRANCH_ID = cboBranch.ListIndex + 1
bOPEN_CR = Asc("N")
sSAMOUNT = txtInitialDeposit.Text
sLAST_NAME = txtLastName.Text
sFIRST_NAME = txtFirstName.Text
bMID_INIT = Asc(txtMiddleInitial.Text)
sSSN = txtSSN.Text
sADDRESS = txtStreet.Text & txtCityStateZip.Text
sPHONE = txtPhone.Text

Submit the Open Account Transaction to the BANKAPP Server

Now we can submit the Open Account transaction to the BANKAPP servers using a synchronous call to the OPEN_ACCT service. This is done via an invocation of the method OPEN_ACCT on the BACKOFFICE object (Automation Server style interface). This code is copied directly from the Active Expert sample usage code for the OPEN_ACCT operation.

BACKOFFICE.OPEN_ACCT lBRANCH_ID, sSAMOUNT, bACCT_TYPE, _
sLAST_NAME, sFIRST_NAME, bMID_INIT, sSSN, _
sADDRESS, sPHONE, bOPEN_CR, lACCOUNT_ID, _
sSBALANCE, sSTATLIN, OPEN_ACCTException

Check for Exceptions

When the call to the OPEN_ACCT service returns, we must check for exceptions by looking at values in the OPEN_ACCTException object. The following code was patterned after the operation OPEN_ACCT usage code and the OPEN_ACCTException usage code. The usage code does most of the exception processing by calling another function. In the sample, instead of calling another function, the exception is handled with in-line code (both methods of exception processing are valid). Also, the ResultDialog form (ResultDialog.frm) is used to format the resulting message instead of MsgBox.

The attribute EX_majorCode in OPEN_ACCTException will tell us what type of exception occurred, if any. The attribute EX_minorCode will tell us more detail on the exception. To translate an EX_minorCode to a meaningful message, call Session.get_error_text. When an exception has occurred, parameters returned on exception are within the exception object. Thus, OPEN_ACCTException.STATLIN contains the valid STATLIN value when an exception occurs-not sSTATLIN.

 Select Case (OPEN_ACCTException.EX_majorCode)
Case NoException:
ResultDialog.lblMessage.Caption = "Open Account transaction successful"
ResultDialog.lblSubMessage1.Caption = "Account Number:"
ResultDialog.lblSubData1.Caption = lACCOUNT_ID
ResultDialog.lblSubMessage2.Caption = "Account Balance:"
ResultDialog.lblSubData2.Caption = sSBALANCE
ResultDialog.Show 1
Case SystemException:
ResultDialog.lblMessage.Caption = "Sys exception on BACKOFFICE.OPEN_ACCT"
ResultDialog.lblSubMessage1.Caption = "Minor code:"
ResultDialog.lblSubData1.Caption = OPEN_ACCTException.EX_minorCode
ResultDialog.lblSubMessage2.Caption = "Error text:"
ResultDialog.lblSubData2.Caption =
Session.get_error_text(OPEN_ACCTException.EX_minorCode)
ResultDialog.Show 1
Case UserException:
ResultDialog.lblMessage.Caption = "Open Account transaction unsuccessful"
ResultDialog.lblSubMessage1.Caption = "Message:"
ResultDialog.lblSubData1.Caption = OPEN_ACCTException.STATLIN
ResultDialog.Show 1
End Select

ActiveX Control Using a Synchronous Interface (Teller Dialog: Balance Inquiry)

The Teller dialog is a tabbed dialog that allows the user to submit Balance Inquiry, Deposit, Withdrawal, and Transfer transactions to the BANKAPP servers. These transactions can be found in the TELLER interface within the BANKAPP module.

You can view the code for the Teller dialog in the file TellerDialog.frm. (Note that a single form is used to present the Balance, Inquiry, Deposit, Withdrawal, and Transfer transactions all within one dialog.)

Figure 5-15 shows the Visual Basic form for the BANKAPP Teller dialogs (Balance Inquiry, Deposit, Withdrawal, and Transfer). Since Teller uses an ActiveX Control (.OCX) Visual Basic interface, the Teller form includes an ActiveX Control which is hidden at run-time.

Figure 5-15 Teller Dialogs - Balance Inquiry, Deposit, Withdrawal and Transfer (TellerDialog.frm)

The Balance Inquiry operation (INQUIRY) provides an example of how to use a synchronous ActiveX Control interface.

(For information on how to switch between synchronous and asynchronous sample code modes in the Active Expert, refer to help on Using Synchronous and Asynchronous Modes.)

Declare Parameters Used in the Operation

We need to declare all parameters used in the INQUIRY operation.

The following code declares all parameters used in the INQUIRY operation. This code is copied directly from the Active Expert sample declarations code for the INQUIRY operation.

Dim lACCOUNT_ID As Long
Dim sSBALANCE As String

Note: Keep in mind that for the full sample application code for TellerDialog.frm, we made sure that the list of parameter declarations also included those needed for DEPOSIT and WITHDRAWAL as well as additional variables needed to process a Transfer transaction.

Declare Exception Parameters

All operations return as their last parameter an exception object. For INQUIRY, the exception structure is declared as INQUIRYException. This code is copied directly from the Active Expert sample declarations code for the INQUIRY exception parameter.

Dim INQUIRYException As Variant

Edit Balance Inquiry Input Data

The cmdApply_Click function selects the Do_BalanceInquiry function when the user clicks on the Apply button for the Balance Inquiry dialog. Initially, the Do_BalanceInquiry function edits all input fields on the form and returns a message if an error is found.

Load the Input Parameters

If the user input data passes the validation checks, we need to load the input parameters in preparation for our synchronous call to the Inquiry service in the BANKAPP server.

The following code is synthesized from the Active Expert sample usage code for the INQUIRY operation.

lACCOUNT_ID = txtAccountNumber.Text

Submit the Balance Inquiry Transaction to the BANKAPP Server

Now we can submit the Inquiry transaction to the BANKAPP servers using a synchronous call to the INQUIRY service. This is done via an invocation of the method INQUIRY on the ActiveX control object called BANKAPPTELLEROCX1.

Note: This ActiveX control was created with the Active Expert Application Builder, registered to this project, and then placed on the TellerDialog form (as shown in Figure 5-15). For information on how to create an ActiveX control in Active Expert, refer to help on Setting OLE Automation Options.

The following code is copied directly from the Active Expert sample usage code for the INQUIRY operation.

BANKAPPTELLEROCX1.INQUIRY lACCOUNT_ID, sSBALANCE, INQUIRYException

Check for Exceptions

The attribute EX_majorCode in INQUIRYException will tell us what type of exception occurred, if any. The attribute EX_minorCode will tell us more detail on the exception. To translate an EX_minorCode to a meaningful message, call Session.get_error_text. When an exception has occurred, parameters returned on exception are within the exception object. Thus, INQUIRYException.STATLIN contains the valid STATLIN value when an exception occurs.

The following code was synthesized from the INQUIRY operation usage code and the Inquiry exception usage code.

Select Case (INQUIRYException.EX_majorCode)
Case NoException:
ResultDialog.lblMessage.Caption = "Balance Inquiry
transaction successful"
ResultDialog.lblSubMessage1.Caption = "Account Balance:"
ResultDialog.lblSubData1.Caption = sSBALANCE
ResultDialog.Show 1
Case SystemException:
ResultDialog.lblMessage.Caption = "Sys exception on BANKAPPTELLEROCX1.INQUIRY"
ResultDialog.lblSubMessage1.Caption = "Minor code:"
ResultDialog.lblSubData1.Caption = INQUIRYException.EX_minorCode
ResultDialog.lblSubMessage2.Caption = "Error text:"
ResultDialog.lblSubData2.Caption = Session.get_error_text(INQUIRYException.EX_minorCode)
ResultDialog.Show 1
Case UserException:
ResultDialog.lblMessage.Caption = "Balance Inquiry
transaction unsuccessful"
ResultDialog.lblSubMessage1.Caption = "Message:"
ResultDialog.lblSubData1.Caption = INQUIRYException.STATLIN
ResultDialog.Show 1
End Select

ActiveX Control Using an Asynchronous Interface (Teller Dialog: Deposit)

The Do_Deposit function in the file TellerDialog.frm provides an example of how to use an asynchronous ActiveX interface. This function performs an asynchronous call to the DEPOSIT service via the DEPOSITAsync operation in the TELLER interface.

For information on how to switch between the synchronous and asynchronous sample code modes in the Active Expert, refer to help on Using Synchronous and Asynchronous Modes.

Declare Parameters Used in the Operation

At the beginning of the TellerDialog.frm file, we need to declare all parameters used in the operation DEPOSITAsync.

The following code shows all parameters used in the DEPOSITAsync operation. This code is copied directly from the Active Expert sample declarations code for the DEPOSITAsync operation.

Dim DEPOSITReply As Object
Dim lACCOUNT_ID As Long
Dim sSAMOUNT As String

The DEPOSITReply object will act as a handle for a specific DEPOSITAsync call.

Declare Exception Parameters

All operations return as their last parameter an exception object. For DEPOSIT, the exception structure is declared as DEPOSITException. This code is copied directly from the Active Expert sample declarations code for the DEPOSIT exception parameter.

Dim DEPOSITException As Variant

Edit Deposit Input Data

The cmdApply_Click function selects the Do_Deposit function when the user clicks on the Apply button for the Deposit dialog. Initially, the Do_Deposit function edits all input fields on the form and returns a message if an error is found.

Load Input Parameters

If the user input data passes the validation checks, we need to load the input parameters in preparation for our asynchronous call to the DEPOSIT service in the BANKAPP server.

The following code is synthesized from the Active Expert sample usage code for the DEPOSIT operation.

lACCOUNT_ID = txtDepositAccount.Text
sSAMOUNT = txtDepositAmount.Text

Submit the Deposit Transaction to the BANKAPP Server

Now we can submit the Deposit transaction to the BANKAPP servers using an asynchronous call to the DEPOSIT service. This is done via a call of the method DEPOSITAsync on the ActiveX control object called BANKAPPTELLEROCX1.

Note: This ActiveX control was created with the Active Expert Application Builder, registered to this project, and then placed on the TellerDialog form in Visual Basic (as shown in Figure 5-15). For information on how to create an ActiveX control in Active Expert, refer to help on Setting OLE Automation Options.

Note that is not necessary to call DEPOSIT asynchronously-it is being done here simply as an example of how to use this part of the Active Expert application programming interface (API).

The following code calls the service DEPOSIT. The DEPOSITReply object will be the handle for this particular asynchronous request. It will be used later to match up the corresponding reply. This code is copied directly from the Active Expert sample usage code for the operation DEPOSITAsync.

Set DEPOSITReply = BANKAPPTELLEROCX1.DEPOSITAsync(lACCOUNT_ID, sSAMOUNT, DEPOSITException)

Check for Exceptions

Now we check for exceptions on the call to the service. This only verifies that the call to the DEPOSIT service was made. It does not check for exceptions that occur when the actual DEPOSIT service is executed. This is done in the following OLE event function BANKAPPTELLEROCX1_DEPOSITAsyncDone. This code was synthesized from the Active Expert sample usage code for the operation DEPOSITAsync.

    Select Case (DEPOSITException.EX_majorCode)
Case NoException:
Case UserException:
Case SystemException:
ResultDialog.lblMessage.Caption = "Sys exception on BANKAPPTELLEROCX1.DEPOSITAsync"
ResultDialog.lblSubMessage1.Caption = "Minor code:"
ResultDialog.lblSubData1.Caption = DEPOSITException.EX_minorCode
ResultDialog.lblSubMessage2.Caption = "Error text:"
ResultDialog.lblSubData2.Caption = Session.get_error_text(DEPOSITException.EX_minorCode)
ResultDialog.Show 1
End Select

Process the Reply

Next, we need to handle the reply to the BANKAPPTELLEROCX1.DEPOSITAsync service call we made in the section above ("Submit the Deposit Transaction to the BANKAPP Server"). There are several ways to handle an asynchronous reply-for a discussion of all of these approaches, refer to the section Asynchronous Invocations in the help on the API.

For the purposes of this example, we are using the OLE Automation Event Interface to handle the asynchronous reply. The event function BANKAPPTELLEROCX1_DEPOSITAsyncDone is called when the reply for the service call BANKAPPTELLEROCX1.DEPOSITAsync has been received.

If you are looking at the sample code in TellerDialog.frm, you can find the event function right after the Do_Deposit function.

Private Sub BANKAPPTELLEROCX1_DEPOSITAsyncDone(ByVal reply As Variant)
If reply Is DEPOSITReply Then
reply.get_DEPOSIT_Reply sSBALANCE, sSTATLIN, DEPOSITException
Select Case (DEPOSITException.EX_majorCode)
Case NoException:
ResultDialog.lblMessage.Caption = "Deposit transaction successful"
ResultDialog.lblSubMessage1.Caption = "Account Balance:"
ResultDialog.lblSubData1.Caption = sSBALANCE
ResultDialog.Show 1
Case SystemException:
ResultDialog.lblMessage.Caption = "Sys exception on
BANKAPPTELLEROCX1.get_DEPOSIT_Reply"
ResultDialog.lblSubMessage1.Caption = "Minor code:"
ResultDialog.lblSubData1.Caption = DEPOSITException.EX_minorCode
ResultDialog.lblSubMessage2.Caption = "Error text:"
ResultDialog.lblSubData2.Caption =
Session.get_error_text(DEPOSITException.EX_minorCode)
ResultDialog.Show 1
Case UserException:
ResultDialog.lblMessage.Caption = "Deposit transaction unsuccessful"
ResultDialog.lblSubMessage1.Caption = "Message:"
ResultDialog.lblSubData1.Caption = DEPOSITException.STATLIN
ResultDialog.Show 1
End Select
End If
End Sub

At the beginning of this function, we need to check to be sure that the reply we are receiving is from the BANKAPPTELLEROCX1.DEPOSITAsync service call we made in the section above ("Submit the Deposit Transaction to the BANKAPP Server"). Theoretically, we could have multiple BANKAPPTELLEROCX1.DEPOSITAsync calls outstanding-so this is a good programming practice.

Once we have determined that we have the correct reply, we need to extract the output parameters and exception from the reply object. Note that the parameters sSBALANCE and sSTATLIN have valid values when no exception has occurred. When an exception has occurred, the returned parameters are within the exception object DEPOSITException.

The rest of the code in this function checks for exceptions in the usual way.

This code is synthesized from the Active Expert sample usage code for the operation Get_DEPOSIT_Reply in the TELLERReply interface (with Active Expert set to asynchronous mode).

Transactions (Teller Dialog: Transfer)

In the file TellerDialog.frm, the Do_Transfer function provides an example of how to use transactions in the Active Expert API.

Note: This function performs calls to the WITHDRAWAL and DEPOSIT services within the scope of a single transaction. From an application perspective, using a transaction is necessary to ensure that either both services succeed or neither succeed. Without transaction protection, the BANKAPP database could be corrupted by a failed transfer transaction.

Declare Parameters and Variables

Since the Do_Transfer function uses both the WITHDRAWAL and DEPOSIT operations, we need to make sure parameters are declared for those operations. The following code is synthesized from the Active Expert declarations code for the WITHDRAWAL and DEPOSIT operations.

Dim lACCOUNT_ID As Long
Dim sSBALANCE As String
Dim sSAMOUNT As String
Dim sSTATLIN As String

We need to declare the following additional variables required to process a Transfer transaction.

Dim lACCOUNT_IDWith As Long
Dim lACCOUNT_IDDep As Long
Dim sSBALANCEWith As String
Dim sSBALANCEDep As String

Edit Transfer Input Data

The cmdApply_Click function selects the Do_Transfer function when the user clicks on the Apply button for the Transfer dialog. Initially, the Do_Transfer function edits all input fields on the form and returns a message if an error is found.

Make Calls to the WITHDRAWAL and DEPOSIT Services

The transfer transaction will be performed by making calls to the WITHDRAWAL service, and then the DEPOSIT service. These calls must be contained within a transaction to keep the database consistent in the event of an error with either the WITHDRAWAL or DEPOSIT.

We begin the transaction by invoking the begin method on the Transaction object that was created at logon. This code is copied directly from the Active Expert sample usage code for the WITHDRAWAL operation (with Sample Code options for exception handling and transaction object code turned on).

To see this code in the Active Expert, first make sure that the options Generate code for Transaction object and Generate exception handling code are both set on. (To do this, choose Tools->Options from the Active Expert menus and click on the Sample Code tab. For more information, refer to help on Working with Sample Code.) Then select either the WITHDRAWAL or DEPOSIT object in the Workstation Objects view and click on the Usage tab.

Transaction.begin TransException

Check for Exceptions

Now we check for exceptions on the Transaction.begin by calling the function HndlTransEx with a marker string. If a fatal exception was found, then exit this function. We are using a function to handle Transaction exceptions to simplify the code when we have to do a Transaction.rollback. If we do not use this function, then we have to code nested error handling, which is tedious to say the least.

HndlTransEx "During Transaction.begin in Transfer"
If FatalError Then Exit Sub

Load the Input Parameters for WITHDRAWAL

Next, we need to load the input parameters in preparation for our synchronous call to the WITHDRAWAL service in the BANKAPP server.

This code is synthesized from the Active Expert sample usage code for the WITHDRAWAL operation.

lACCOUNT_IDWith = txtTranWithAccount.Text
sSAMOUNT = txtTranAmount.Text

Submit the Withdrawal Transaction to the BANKAPP Servers

Now we can submit the Withdrawal transaction to the BANKAPP servers using a synchronous call to the WITHDRAWAL service. This is done via a synchronous call of the method WITHDRAWAL on the ActiveX control object called BANKAPPTELLEROCX1.

Note: This ActiveX control was created with the Active Expert Application Builder, registered to this project, and then placed on the TellerDialog form (as shown in Figure 5-15). For information on how to create an ActiveX control in Active Expert, refer to help on Setting OLE Automation Options.

The following code calls the service WITHDRAWAL. This code is copied from the Active Expert sample usage code for the WITHDRAWAL operation with a small modification for a different lACCOUNT_ID.

BANKAPPTELLEROCX1.WITHDRAWAL lACCOUNT_IDWith, sSAMOUNT, _
sSBALANCEWith, sSTATLIN, WITHDRAWALException

Check for Exceptions on WITHDRAWAL

When the above call returns, we must check for exceptions by looking at values in the WITHDRAWALException object. The following code was patterned after the Active Expert sample usage code for the exception parameter in the WITHDRAWAL operation. Instead of calling another function, the WITHDRAWAL exception is handled with in-line code. If we have a WITHDRAWAL exception, we have to rollback the active transaction and check for exceptions on the rollback. To keep the code more readable, the exception processing on the rollback is placed in a separate function (HndlTransEx). (The section "Exception Processing on the Rollback" shows the code for the HndlTransEx function.)

The attribute EX_majorCode in WITHDRAWALException will tell us what type of exception occurred, if any. The attribute EX_minorCode will tell us more detail on the exception. To translate an EX_minorCode to a meaningful message, call Session.get_error_text. When an exception has occurred, parameters returned on exception are within the exception object. Thus, WITHDRAWALException.STATLIN contains the valid STATLIN value when an exception occurs-not sSTATLIN.

 Select Case (WITHDRAWALException.EX_majorCode)
Case NoException:
Case SystemException:
'*** Rollback active transaction
Transaction.Rollback TransException
HndlTransEx "During rollback in Transfer - WITHDRAWAL"
'*** Rollback done; display msg
ResultDialog.lblMessage.Caption = _
"Sys exception on BANKAPPTELLEROCX1.WITHDRAWAL in Transfer"
ResultDialog.lblSubMessage1.Caption = "Minor code:"
ResultDialog.lblSubData1.Caption = WITHDRAWALException.EX_minorCode
ResultDialog.lblSubMessage2.Caption = "Error text:"
ResultDialog.lblSubData2.Caption = Session.get_error_text(WITHDRAWALException.EX_minorCode)
ResultDialog.Show 1
Exit Sub
Case UserException:
'*** Rollback active transaction
Transaction.Rollback TransException
HndlTransEx "During rollback in Transfer - WITHDRAWAL"
'*** Rollback done; display msg
ResultDialog.lblMessage.Caption = _
"Transfer transaction unsuccessful"
ResultDialog.lblSubMessage1.Caption = "Problem with withdrawal:"
ResultDialog.lblSubData1.Caption = WITHDRAWALException.STATLIN
ResultDialog.Show 1
Exit Sub
End Select

Exception Processing on the Rollback

The following example shows the code for the HndlTransEx function, which handles the exception processing on the rollback. HndlTransEx is patterned after the operation WITHDRAWAL exception usage code.

Private Sub HndlTransEx(Msg As String)
Select Case (TransException.EX_majorCode)
Case NoException:
Case SystemException:
ResultDialog.lblMessage.Caption = "Sys exception: " + Msg
ResultDialog.lblSubMessage1.Caption = "Minor code:"
ResultDialog.lblSubData1.Caption =
TransException.EX_minorCode
ResultDialog.lblSubMessage2.Caption = "Error text:"
ResultDialog.lblSubData2.Caption =
Session.get_error_text(TransException.EX_minorCode)
ResultDialog.Show 1
FatalError = True
Case UserException:
ResultDialog.lblMessage.Caption = "User exception: " + Msg
ResultDialog.lblSubMessage1.Caption = "Minor code:"
ResultDialog.lblSubData1.Caption =
TransException.EX_minorCode
ResultDialog.lblSubMessage2.Caption = "Error text:"
ResultDialog.lblSubData2.Caption =
Session.get_error_text(TransException.EX_minorCode)
ResultDialog.Show 1
FatalError = True
End Select
End Sub

Load the Input Parameters for DEPOSIT

Next, we need to load the input parameters in preparation for our synchronous call to the DEPOSIT service in the BANKAPP server.

The following code is synthesized from the Active Expert sample usage code for the DEPOSIT operation.

lACCOUNT_IDDep = txtTranDepositAccount.Text
sSAMOUNT = txtTranAmount.Text

Submit the Deposit Transaction to the BANKAPP Servers

Now we can submit the Deposit transaction to the BANKAPP servers using a synchronous call to the DEPOSIT service. This is done via a call of the method DEPOSIT on the ActiveX control object called BANKAPPTELLEROCX1. (This ActiveX control was created with the Active Expert Application Builder, registered to this project, and then placed on the TellerDialog form in Visual Basic.)

The following code calls the DEPOSIT service. This code is copied directly from the Active Expert sample usage code for the DEPOSIT operation with a small modification for a different lACCOUNT_ID.

BANKAPPTELLEROCX1.DEPOSIT lACCOUNT_IDDep, sSAMOUNT, _
sSBALANCEDep, sSTATLIN, DEPOSITException

Check for Exceptions on DEPOSIT

When the above call returns, we must check for exceptions by looking at values in the DEPOSITException object. The following code was patterned after the Active Expert sample usage code for the exception parameter in DEPOSIT operation. Instead of calling another function, the DEPOSIT exception is handled with in-line code. If we have a DEPOSIT exception, we have to rollback the active transaction and check for exceptions on the rollback. To keep the code more readable, the exception processing on the rollback is placed in a separate function (HndlTransEx). (The section "Exception Processing on the Rollback" shows the code for the HndlTransEx function.)

The attribute EX_majorCode in DEPOSITException will tell us what type of exception occurred, if any. The attribute EX_minorCode will tell us more detail on the exception. To translate an EX_minorCode to a meaningful message, call Session.get_error_text. When an exception has occurred, parameters returned on exception are within the exception object. Thus, DEPOSITException.STATLIN contains the valid STATLIN value when an exception occurs-not sSTATLIN.

Select Case (DEPOSITException.EX_majorCode)
Case NoException:
Case SystemException:
'*** Rollback active transaction
Transaction.Rollback TransException
HndlTransEx "During rollback in Transfer - DEPOSIT"
'*** Rollback done; display msg
ResultDialog.lblMessage.Caption = _
"Sys exception on BANKAPPTELLEROCX1.DEPOSIT in Transfer"
ResultDialog.lblSubMessage1.Caption = "Minor code:"
ResultDialog.lblSubData1.Caption =
DEPOSITException.EX_minorCode
ResultDialog.lblSubMessage2.Caption = "Error text:"
ResultDialog.lblSubData2.Caption =
Session.get_error_text(DEPOSITException.EX_minorCode)
ResultDialog.Show 1
Exit Sub
Case UserException:
'*** Rollback active transaction
Transaction.Rollback TransException
HndlTransEx "During rollback in Transfer - DEPOSIT"
'*** Rollback done; display msg
ResultDialog.lblMessage.Caption = _
"Transfer transaction unsuccessful"
ResultDialog.lblSubMessage1.Caption = "Problem with
deposit:"
ResultDialog.lblSubData1.Caption = DEPOSITException.STATLIN
ResultDialog.Show 1
Exit Sub
End Select

End a Successful Transaction with a "Commit"

If the DEPOSIT and WITHDRAWAL complete successfully, we need to end the transaction by invoking the commit method on the Transaction object that was created at session logon. This code is copied from the Active Expert sample usage code on the WITHDRAWAL operation (with Sample Code options for transaction object code turned on). (For more information, see the section help on Working with Sample Code.)

Transaction.commit False, TransException

Check for Exceptions on the Transaction Commit

Check for exceptions on the Transaction.commit by calling the function HndlTransEx with a marker string. If a fatal exception is found, then exit this function. We are using a function to handle Transaction exceptions to simplify the code when we have to do a Transaction.rollback.

Note: You can find the code for the HndlTransEx function in the file TellerDialog.frm by searching for Private Sub HndlTransEx(Msg As String)

HndlTransEx "During Transaction.commit in Transfer"
If FatalError Then Exit Sub

If the Transfer completes successfully, you probably want to post a confirmation dialog. (For an example of this, search the file TellerDialog.frm for the line '*** Transfer has completed successfully..so let's tell the user about it.

Logoff

You can view an example of a BEA TUXEDO session logoff in the file Main.frm.

The logoff is initiated when the user selects File->Logoff from the BANKAPP sample application menu. The following code accepts this user input:

Private Sub mnuLogoffItem_Click()

The following code logs off from the BEA TUXEDO server via the Session object. This code is copied directly from Active Expert sample cleanup code for the BANKAPP module (with Sample Code options for transaction object code turned on). (To see the logoff code in the Active Expert, select the BANKAPP module in the Workstation Objects main window view and click on the Cleanup tab.)

(For more information, see the section help on Working with Sample Code.)

Session.logoff
Set Transaction = Nothing
Set Session = Nothing

To see how we added a user confirmation for the BEA TUXEDO logoff in the sample application, refer to the file Main.frm.