If...Then...Else...End If Statement in VB6 - Sample Program 1

Friday, August 14, 2009 ·

Visual Basic program application usually use If..Then..Else..End If statement. In this posting I would like to write a sample of simple program in VB6 which use If..Then..Else..End If statement with 2 conditions or more.
The syntax of the statement is the following:

  • With 2 conditions:

    If Condition-1 Then
    Statement-1
    Else
    Statement-2
    End If


  • With more than 2 conditions:

    If Condition-1 Then
    Statement-1
    Else
    If Condition-2 Then
    Statement-2
    Else
    If Condition-3 Then
    Statement-3
    Else
    Statement-4
    End If
    End If
    End If


    Note: If the conditions are too much it is not effective if using If..Then..Else..End If statement, however it is better using Select Case..End Select statement.
    The scenario of the program is as below:
  • This is a part of Registration Form that accept the Name and Gender information
  • The first data should be filled in is a Name
  • The next data is a gender code
  • The value that can be filled are just M and F (M for Male, and F for Female)
  • If user input another M and F a message appears on screen
  • When user input M for the Gender cell program will automatically display "Male", and when user input F for the Gender a word "Female" will be displayed automatically
  • After both Name and Gender cell filled with valid data, command button "Register" will Enable
  • When user click command button "Register" a greeting will appear on the screen
    Your program coding will look like the folling list:

    Private Sub cmdRegister_Click()
    'recheck data validity
    If txtName.Text = "" Then
    MsgBox "You must fill in the Name first"
    Exit Sub
    End If
    If txtGender.Text = "" Then
    MsgBox "You must fill in the Gender code"
    Exit Sub
    End If
    If txtGender.Text <> "M" And txtGender.Text <> "F" Then
    MsgBox "Just 'M' or 'F' for the Gender code "
    Exit Sub
    End If
    'display greeting
    lblGreeting.Caption = "Welcome " & IIf(txtGender.Text = "M", "Mr. ", "Mrs. ") & _
    Trim(txtName.Text) & ". Thank you for joining our club."
    End Sub

    Private Sub Form_Load()
    lblGender.Caption = ""
    lblGreeting.Caption = ""
    End Sub

    Private Sub txtGender_Change()
    If txtGender.Text <> "M" And txtGender.Text <> "F" Then
    MsgBox "Invalid value, you should fill in 'M' or 'F'"
    Else
    If txtGender.Text = "M" Then
    lblGender.Caption = "Male"
    Else
    lblGender.Caption = "Female"
    End If
    End If

    If txtName.Text <> "" Then
    cmdRegister.Enabled = True
    Else
    cmdRegister.Enabled = False
    End If
    End Sub

    Private Sub txtName_Change()
    If txtName.Text = "" Then
    cmdRegister.Enabled = False
    Else
    If txtGender.Text = "" Then
    cmdRegister.Enabled = False
    Else
    cmdRegister.Enabled = True
    End If
    End If
    End Sub


    Please look at below screen shot as your illustration:
    Sample VB6 Program If Then Else End If


  • 0 comments:

    Post a Comment