Example 1 for Week 8 - Step by Step


This program will demonstrate the use of Dynamic Arrays and how to use the NewIndex and ItemData properties on List and Combo Boxes when the Sorted property is True. This example also uses Multiple Forms, User Defined Types and demonstrates data entry error prevention techniques.

Part 1 - Housekeeping / Overhead

Save the Project in your NEW Project directory

Add the Second Form

Click on the Save Icon in the Tool Bar.


Part 2 - Building the Example Application

The example uses two forms

 

Details of the example:

Main form will contain a list box that will display a list of the Vendors sorted by name. It will be empty to start and must have the Sorted Property set to True.
The Vendor form will be used to display vendor information and to allow the adding of a new vendor information.

The Vendor information will be stored in a Dynamic Array of UDTs

Type udtVendor
    strID As String
    strName As String
    sngTotal As Single
End Type

Public udtVendors() As udtVendor

These definitions will be placed in a Module so that they are available to either form.

Clicking on the Add button will bring up an Empty Vendor form in "Add" mode. This is accomplished by setting a Boolean Variable to True.

Public blnAdd As Boolean

Private Sub cmdAdd Click()
    blnAdd = True
    frmVendor.Show vbModal

End Sub

Double Clicking on the ListBox will bring the Vendor form up in "Display" mode

Private Sub lstVendors DblClick()
    blnAdd = False
    frmVendor.Show vbModal

End Sub

The difference here is what happens when the Vendor form gets control. Form Load will determine what to display based on the value in the Boolean variable. This is detailed below.

Private Sub AddVendor()
    Dim udtTemp As udtVendor

    frmMain.lstVendors.AddItem txtName.Text
    frmMain.lstVendors.ItemData(frmMain.lstVendors.NewIndex) =  _
                       frmMain.lstVendors.ListCount - 1

    ReDim Preserve udtVendors(frmMain.lstVendors.ListCount)

    udtTemp.strID = txtID.Text
    udtTemp.strName = txtName.Text
    udtTemp.sngTotal = 0

    udtVendors(frmMain.lstVendors.ListCount - 1) = udtTemp

End Sub

The Boolean Variable that is used to indicate Add or Display drives what happens in Form Load

Private Sub Form Load()
    If frmMain.blnAdd = True Then
       lblLabelAmount.Visible = False
       lblAmount.Visible = False
    Else
       lblLabelAmount.Visible = True
       lblAmount.Visible = True
    End If

    If frmMain.blnAdd Then
       cmdReturn.Enabled = False
    Else
       txtID.Text = udtVendors(GetIndex()).strID
       txtName.Text = udtVendors(GetIndex()).strName
       lblAmount.Caption = FormatCurrency(udtVendors(GetIndex()).sngTotal, 2)
       txtID.Locked = True
       txtName.Locked = True
    End If

End Sub

Add Mode displays a blank screen and disables the Return button until all fields are entered. Display Mode will display the information stored in the array that is associated with the selected ListBox entry. The GetIndex function looks at the ItemData property to determine what slot in the array contains the associated information.

The reason that this is necessary is that when the Sorted property is True, the Physical position in the ListBox does not reflect the Physical location in the Array

The ItemData property was set when the Vendor was added to the ListBox and the Array

Public Function GetIndex() As Integer
    Dim intIndex As Integer

    intIndex = frmMain.lstVendors.ItemData(frmMain.lstVendors.ListIndex)

    GetIndex = intIndex

End Function

The function works well here because the reference to the Index is so long and difficult to read.


Other Event Handlers

The code above provides the features necessary to do the job, but there are some additional features of this application that keep the data clean.

Private Sub SetReturn()
    If Not txtID.Text = "" Then

    Else
       cmdReturn.Enabled = False
       Exit Sub
    End If

    If Not txtName.Text = "" Then

    Else
       cmdReturn.Enabled = False
       Exit Sub
    End If

    cmdReturn.Enabled = True

End Sub

Call this subroutine from each of the TextBox Change events. The user cannot click on Return until there is data in the Vendor Number and Vendor Name.

Not "Bullet Proof" by any means, but a good technique to build into your applications.


Part Two

Updating transaction totals is only a matter of updating the proper UDT Array entry. Error prevention code forces a vendor to be selected from the ListBox and there must be an amount in the Amount TextBox.

Private Sub txtAmount Change()
    If txtAmount.Text = "" Then
       cmdTrans.Enabled = False
    Else
       cmdTrans.Enabled = True
    End If

    If lstVendors.ListIndex = -1 Then
       cmdTrans.Enabled = False
    End If

End Sub

Anytime there is an amount entered into the Amount field, check to see if there is a Vendor selected. This is the only way that the Transaction button will be enabled as it is disabled when the program starts. Additional edits are coded in the Click event and updating the Total Sales is no different that maintaining any type of accumulator.

Private Sub cmdTrans Click()
    Dim sngAmount As Single

    If txtAmount.Text = "" Then
        MsgBox "You must enter a transaction amount"
        Exit Sub
    End If

    If Not IsNumeric(txtAmount.Text) Then
        MsgBox "You must enter a number in the transaction amount field"
        Exit Sub
    End If

    sngAmount = CSng(txtAmount.Text)

    udtVendors(GetIndex()).sngTotal =  _
            udtVendors(GetIndex()).sngTotal + sngAmount

    txtAmount.Text = ""
    lstVendors.ListIndex = -1

End Sub

CSng is a conversion function. Resetting the screen information completes the process.


Back to Week 8