www.hitmill.com
link to hitmill.com home page
 

 

Visual Basic Tutorial 2

The following tutorial (code sample) was written using the Visual Basic 6.0 IDE (Integrated Development Environment). This program demonstrates the use of two different decision structures: The If.. Then statement and the Select Case decision structure. Below, Select Case has been nested inside of the If_Then decision structure to also demonstrate nesting of decision structures. The Cancel and Default properties are introduced (see directions). The blnDisplay Boolean variable is carried over from the first tutorial. The WeekDay(Now) function is introducted below.
  The use of the Boolean variable prevents repetitive printing in the run mode by cmdDisplay. The statement "If Not blnDisplayed Then" means that if the form does not have printing on it already then, the commands will be executed by cmdDisplay. "If Not blnDisplayed" means the same as "blnDisplayed = False", (or not displayed yet).
  WeekDay(Now) is a fun little function that can determine the day of the week, the date, and the time from the users PC computer. By using WeekDay(Now) you are able to print different messages for different days of the week. Below, WeekDay(Now) is used with the Select Case decision structure but if you want to change it and put it into an If_Then decision structure, the format would be:
If WeekDay(Now) = vbSunday Then
  frmDayOfWeek.Print "This is the Sunday message"
If WeekDay(Now) = vbMonday Then
  frmDayOfWeek.Print "This is the Monday message"
and so on...
 


 
DIRECTIONS (CONT.)
 
3.)  Name the third command button cmdExit. Make the caption:
E&xit. ALT + x will access this button (if the x is underlined using the ampersand.)
I also want this key accessible by pressing the ESC key (Escape key, upper left on keyboard). To do this I have to set the Cancel property to True. I do this as soon as I have set the Caption property. At run time, touching the ESC key will now unload the form, exiting this program.
 
DIRECTIONS:
 
Use the sketch at the left to approximate the positioning of 3 command buttons on a form but leave enough space on the form for the printing to display.
 
1.)  Name the first command button cmdDisplay and give it a caption:  D&isplay.
The ampersand (&) will make the letter i underlined and then the button can be accessed by used a keyboard combination of ALT+ i. Now set the Default property of cmdDisplay to true. This makes the cmdDisplay button also work by touching the ENTER key.
 
2.) Name the second command button cmdClear. The Caption property will be set to: C&lear. With the letter l underlined it is accessible by using Alt + l, (the letter el).

 

 
' ***********************************************
' This program includes Boolean variable,
' If... Then statement
' Select Case decision structure
' Example of nested decision structure
' Cancel property of cmdExit button (accessible by ESC key)
' Default property of cmdDisplay button (accessible by ENTER key)
' Copyright The HiTMilL/cg /October 20, 1999/vb6
' ***********************************************
 
Option Explicit  'Requires explicit variable declaration in the module
       'The IDE features of Auto Quick Info, Auto Data Tips, Auto Indent,
       'and Tab Width (auto settings) are available when Option Explicit
       'is stated. Declaring variables also simplifies debugging of programs.
 
Private blnDisplayed As Boolean  'Dimension the variable as Boolean
 
Private Sub cmdClear_Click()
  If blnDisplayed Then    'same as a true value for blnDisplayed (it has printed)
  frmDayOfWeek.Cls        'clears the form of any background printing
  blnDisplayed = False    'programmer resets the boolean to false
                          'This allows printing in next sub.
  End If
End Sub
 
Private Sub cmdDisplay_Click()
  If Not blnDisplayed Then   'if not displayed yet, will print once:
    Select Case Weekday(Now) 'Begins new nested decision structure
            'Will print according to what day of week it is
            'WeekDay(Now) can read day, date, time from user computer
    Case vbSunday
      frmDayOfWeek.Print ""   'This makes a blank line
      frmDayOfWeek.Print "Eat at Joe's"
      frmDayOfWeek.Print "New Location:"
      frmDayOfWeek.Print "Main and Second St."
    Case vbMonday
      frmDayOfWeek.Print "Eat at Bill's"
    Case vbTuesday
      frmDayOfWeek.Print "Eat Salads at Stephanie's"
    Case vbWednesday
      frmDayOfWeek.Print "Eat at Cheryl's"
    Case vbThursday
      frmDayOfWeek.Print ""   'Another blank line
      frmDayOfWeek.Print "BBQ at Skip's today!"
      frmDayOfWeek.Print "Ribs, Hamburgers, Salads,"
      frmDayOfWeek.Print "Tri-Tip, and BBQ Beef Sandwiches"
      frmDayOfWeek.Print "Southern Fried Chicken!"
      frmDayOfWeek.Print "BRING A FRIEND!"
      frmDayOfWeek.Print "Lunch is served 11:00 until 2:30PM"
    Case vbFriday
      frmDayOfWeek.Print "Try the fish at Shelly's"
    Case vbSaturday
      frmDayOfWeek.Print "Pizza night at Gail's!"
    End Select      'Must end the "inner" Select decision structure
  blnDisplayed = True   'Tells program it has been displayed once
  End If   'Must end the "outer" If.. Then decision structure
End Sub
 
Private Sub cmdExit_Click()
   Unload Me   'Unloads the form in a single document interface (SDI) program
   'This will be done differently in a multiple document interface (MDI) program
End Sub
 

Back to the Primers and Tutorials Page
 


webmaster@hitmill.com
 
Updated -

© hitmill.com
All Rights Reserved