So ready for your first VB6 application? Lets begin...
We will make a VB6 application to display a message to the user when the user starts the application.
We will make a VB6 application to display a message to the user when the user starts the application.
1. Start VB6
2. Double click the form to get the code window. (you will get text like below)
2. Double click the form to get the code window. (you will get text like below)
Private Sub Form_Load()
End Sub
Private Sub - It tells that this is a private subject and cannot be used by elements outside the form.
Form - The Form itself :P
Load - The Form's Load Event. This will be triggered when the form is loaded.
End Sub - This specifies where the block of code ends.
Form - The Form itself :P
Load - The Form's Load Event. This will be triggered when the form is loaded.
End Sub - This specifies where the block of code ends.
Now to Display a message we need to use a function, That's built in VB6 already. Its called MSGBOX, easy to guess - yes its a message function. This function displays the message in a box whenever its triggered. We can directly pass a text to the MSGBOX but lets add a variable first. Check the code below, the text after ' is comment, thats how we add comments in VB
Private Sub Form_Load()
Dim message as string 'Declare a string Variable
message = "Hello Visual Basic 6"
Msgbox message
End Sub
The 2nd line shows how to declare a string variable. Dim tells VB6 that you want to declare a variable, message is the name of the variable, as ______ tells VB6 that the varialble will be declared as which type. in this case we declare a string variable. The next line is pretty clear. We set the value of the String variable message that we just declared. The following line tells the msgbox function to display message variable. After you are done press F5 or the run button to test the program.
Not enough? Try the following codes as well
Private Sub Form_Load()
Dim tDate As String 'Declare a string Variable
tDate = Date 'Set Date Value
MsgBox tDate 'Display the Date
End Sub
Private Sub Form_Load()
Dim NO1, NO2 'Declare two variable to specify to numbers
Dim SUM, strSUM 'Declare variable to store the sum of the two numbers
'and strSUM to store the sum converted to string form
NO1 = 5 'Specify the numbers
NO2 = 4
SUM = NO1 + NO2 'Add and store to SUM variable
strSUM = Str(SUM) ' Convert the sum to string
MsgBox "5 + 4 = " + strSUM ' Display the sum
'Alternative
'MsgBox "5 + 4 = " + str(5+4)
End Sub
You can try other operators like * / + - Try them all...