VB6 Source Code for Beginners

The VB6 source code samples let you learn VB6 in a practical way. The source code samples in this post are best for a beginner learner. So if you have just started learning this amazing language, download these samples. The programming concepts will become clearer when you'll learn with source code samples.

InputBox & MsgBox 1

This program shows you the simplest form of InputBox and MsgBox. A number is taken as input and the same is shown through MsgBox.


Learn about Input/Output operations: 

This dialog box takes an input
   
This dialog box displays message          
                                                   >> DOWNLOAD

Code:
Private Sub Form_Load()
    n = InputBox("Enter a number")
    MsgBox "You entered " & n
End Sub



InputBox & MsgBox 2

This is another simple example of InputBox and MsgBox.. Two numbers are taken through InputBox, and the sum is shown through the MsgBox.


Learn about Input/Output operations: 


         

    



                                                   >> DOWNLOAD

Code:
Private Sub cmdEnterValues_Click()
    a = Val(InputBox("Enter the value of a", "Input value"))
    b = Val(InputBox("Enter the value of b", "Input value"))
    MsgBox "The sum is " & a + b, vbInformation, "Output"
End Sub




Output with PictureBox

The program shows how to show messages in PictureBox. The Print and Cls methods are used in this program. 


Learn about input and output operations: 

Showing text message in picture box
             
                                                   >> DOWNLOAD
Code:
Private Sub cmdClear_Click()
    picOutput.Cls       'Cls method clears the picture box
End Sub

Private Sub cmdShow1_Click()
    picOutput.Print "Hello World !"
End Sub

Private Sub cmdShow2_Click()
    picOutput.Print "Welcome to Visual Basic !"

End Sub



If-Then statement

If-Then statement is illustrated in this program. A number is taken through InputBox and shown using the Print method if it is greater than 5.


Learn about If-blocks: Lesson 17


 If Then statement in vb

             
                                                   >> DOWNLOAD

Code:
Private Sub Form_Load()
Form1.Show
Dim a As Integer
a = InputBox("Enter the value of a", "Input")
If a > 5 Then
    Print "The number is greater than 5"
End If
End Sub





If-Else statement

This example program is almost same as the previous one. The only difference is that an Else statement has been added to the code.


Lear about If-blocks: Lesson 17


             
                                                   >> DOWNLOAD

Code:
Private Sub Form_Load()
    Form1.Show
    Dim a As Integer
    a = InputBox("Enter the value of a")
    If a > 5 Then
        Print "The number is greater than 5"
    Else
        Print "The number is less than 5"
    End If
End Sub





Largest Number

The Largest Number sample program finds out the largest number among three numbers. This program uses If-Else statements.


Learn about nested If blocks: 

Program to find the largest number among three using if else statements
             
                                                   >> DOWNLOAD
Code:
Private Sub cmdCalculate_Click()
    Dim num1 As Integer, num2 As Integer, num3 As Integer
    num1 = Val(txtNum1.Text)
    num2 = Val(txtNum2.Text)
    num3 = Val(txtNum3.Text)
   
    If num1 > num2 And num1 > num3 Then
        lblResult.Caption = num1
    ElseIf num2 > num1 And num2 > num3 Then
        lblResult.Caption = num2
    Else
        lblResult.Caption = num3
    End If
End Sub



Word Length

The Word Length sample program calculates the length of a word. The word is given as an input through InputBox and then the length is shown on the form. There's a simpler way to solve this kind of problem, but this method in the program has been applied only to teach you about the Select Case statement.


Learn about Select Case: 

              
Determines the length of a word
                                                   >> DOWNLOAD

Code:
Private Sub cmdStart_Click()
    Dim word As String, n As Integer
    word = InputBox("Enter the word", "Input")
    n = Len(word)
    

    Select Case n
    Case 0
        Print "you have not entered any word"
    Case 1
        Print "This is a 1 letter word"
    Case 2
        Print "This is a 2 letters word"
    Case 3
        Print "This is a 3 letters word"
    Case 4
        Print "This is a 4 letters word"
    Case 5
        Print "This is a 5 letters word"
    Case Else
        Print "The word contains more than 5 letters"
    End Select

End Sub





Grade Program

The sample program calculates the obtained marks and percentage of marks according to the specified grade. Select Case statements have been used in this example.


Learn about Select Case: 

Grade Calculation
             
                                                   >> DOWNLOAD

Code:

Private Sub cmdCalculate_Click()
    Dim s As String
    s = txtGrade.Text
  
    Select Case s
    Case "E"
        lblMarks.Caption = "above 90%"
        lblRemarks.Caption = "Excellent"
    Case "A+"
        lblMarks.Caption = "above 80%"
        lblRemarks.Caption = "Very Good"

    Case "A"
        lblMarks.Caption = "above 70%"
        lblRemarks.Caption = "Good"

    Case "B"
        lblMarks.Caption = "above 60%"
        lblRemarks.Caption = "Average"

    Case "C"
        lblMarks.Caption = "above 50%"
        lblRemarks.Caption = "Satisfactory"
    Case "D"
        lblMarks.Caption = "above 40%"
        lblRemarks.Caption = "Poor"
    Case "F"
        lblMarks.Caption = "above 35%"
        lblRemarks.Caption = "Fail"
    End Select

End Sub