Example for Week 4 - Step by Step

 

Part 1 - Housekeeping / Overhead

Project and Form Property Standards

Save the Project in your NEW Project directory


Did you remember the comment block and Option Explicit?

'
'    Name
'    Class and Meeting Time
'    Brief Description of this Program
'
Option Explicit


Part 2 - Building the Example Application

This program will maintain a series of counts and display a summary of those counts. The program must edit the user input and display appropriate error messages when the input is not acceptable.

Build the User Interface

Use the Format Menu Option to correctly size and align the controls on the form. Make sure that the tab indexes are set correctly. How about the keyboard interface for the two text boxes, the set of option buttons, the check boxes, and the four command buttons?

Example

Text Boxes:
txtName
txtAge
Command Buttons:
cmdEnter
cmdClear
cmdSummary
cmdExit
Module Level Variables:
m_intMale
m_intFemale
m_intUnder21
m_intOver21
m_intSports
m_intMovies
m_intMusic
Option Buttons:
optMale
optFemale
Check Boxes:
chkSports
chkMovies
chkMusic

Done with the user interface?

Run the program: Click on Run | Start With Full Compile

Test the Tab Sequence and the Keyboard Interface one more time.

You have completed the user interface


Build the Event Handlers

cmdClear
Turn off all the check boxes, set the option button to Male, blank the name and age and position the cursor to the name field

cmdExit
End the program

cmdSummary
Build a message that will display the counters maintained for each of the categories.

cmdEnter

Edits:
Name must contain a value
Age must contain a value, it must be numeric, and it must be greater than zero and less that 100

Calculations:
Maintain counts for each of the categories described by the Module Level variables
  Male and Female
  Under 21 and 21 or Over
  Interests of:
    Sports
    Movies
    Music

After the numbers have been accumulated, call the cmdClear Event Handler Code

Form_Load
Initialize all the Module Level Variables to zero and call the cmdClear Event Handler Code

Private Sub cmdEnter_Click()
' Edit the name
If txtName.Text = "" Then
    MsgBox "You must enter a name", vbCritical + vbOKOnly, "Error in the name"
    txtName.SetFocus
    Exit Sub
End If

' Edit the age
If IsNumeric(txtAge.Text) Then
Else
    MsgBox "You must enter an age", vbCritical + vbOKOnly, "Error in the age"
    txtAge.SetFocus
    Exit Sub
End If

If Val(txtAge.Text) < 1 Then
    MsgBox "The age must be larger than zero", vbCritical + vbOKOnly, "Age out of range"
    txtAge.SetFocus
    Exit Sub
End If

If Val(txtAge.Text) > 99 Then
    MsgBox "The age must be less than 100", vbCritical + vbOKOnly, "Age out of range"
    txtAge.SetFocus
    Exit Sub
End If


' Done with edits
' Accumulate totals

If Val(txtAge.Text) > 20 Then
    m_intOver21 = m_intOver21 + 1
Else
    m_intUnder21 = m_intUnder21 + 1
End If

If optMale.Value Then
    m_intMale = m_intMale + 1
Else
    m_intFemale = m_intFemale + 1
End If

If chkSports.Value = vbChecked Then
    m_intSports = m_intSports + 1
End If

If chkMusic.Value = vbChecked Then
   m_intMusic = m_intMusic + 1
End If

If chkMovies.Value = vbChecked Then
    m_intMovies = m_intMovies + 1
End If

cmdClear_Click

End Sub

 

Private Sub cmdSummary_Click()
Dim strMessage As String

strMessage = ""
strMessage = strMessage & "Over 21 " & vbTab & vbTab & _
                    FormatNumber(m_intOver21, 0) & vbCrLf
strMessage = strMessage & "Under 21 " & vbTab & vbTab & _
                    FormatNumber(m_intUnder21, 0) & vbCrLf

strMessage = strMessage & vbCrLf

strMessage = strMessage & "Male " & vbTab & vbTab & _
                    FormatNumber(m_intMale, 0) & vbCrLf
strMessage = strMessage & "Female " & vbTab & vbTab & _
                    FormatNumber(m_intFemale, 0) & vbCrLf

strMessage = strMessage & vbCrLf

strMessage = strMessage & "Interests: " & vbCrLf

strMessage = strMessage & "Sports " & vbTab & vbTab & _
                    FormatNumber(m_intSports, 0) & vbCrLf
strMessage = strMessage & "Movies " & vbTab & vbTab & _
                    FormatNumber(m_intMovies, 0) & vbCrLf
strMessage = strMessage & "Music " & vbTab & vbTab & _
                    FormatNumber(m_intMusic, 0) & vbCrLf

MsgBox strMessage, vbInformation + vbOKOnly, "Summary Counts"


End Sub

Private Sub cmdClear_Click()
    chkSports.Value = vbUnchecked
    chkMusic.Value = vbUnchecked
    chkMovies.Value = vbUnchecked

    optMale.Value = True

    txtName.Text = ""
    txtAge.Text = ""
    txtName.SetFocus

End Sub

Finish up the testing and be sure to save the project.


A couple of thoughts before this example is complete.

You should have a better understanding of the basic edits and of how to maintain accumulators and counters.
The edit procedure involves input validation and exiting the event handler if an error is detected. Continue with the process if all of the input is clean.
You should also understand that Event Handlers can be invoked from anywhere in the program simply by calling the Event Handler Procedure.


Back to Week 4