Simple VB Code with Strings Booleans and Integers

In this tutorial we are going to learn to use strings, Booleans, and Integers to check for more specific features before events can happen. An Integer is a type of variable that can only contain numbers. A string is a variable that can text and numbers. Strings must be enclosed in quotation marks when setting them. A Boolean is a variable that is either true of false. In order to use these variables in our applications we need to “dimensionalize” (Dim in VB) them to be ready. By using dim we tell the application what the name of it is and what type of variable it is. The naming convention should be the same as the controls in our last tutorials (three letter abbreviations, followed by a descriptive name), such as strString ,intInteger, and booBoolean. Dim need to be made directly under the Class set in the code. Puttin them here allows all Sub routines to be able to call them. If you were to dim on during a Sub it wont only be able to be used during that particular sub. The ones that I am Dim’ing are shown below:

Public Class frmLogin
    Dim strUser As String
    Dim strPassword As String
    Dim intAttempts As String
    Dim booLogin As Boolean

As shown above, you can see that we are going to use two strings (User and Password). We also have an integer (Attempts) and a Boolean (Login). What we are going to do is make a login page that after trying to log in three times pops a message saying that the maximum number of attempts has been reached and the application will now be terminated. If you log in correctly you will get a login successful message box. Note: If you are planning on making an application that authenticates people – this is NOT the method to do that as we are going to put the password directly in the code. This way anyone can open the .exe in a resource editor/hex editor and plainly see the password.

Next, we need to set the string equal to what we want the password and username to be. We will set this on form load. To get the form load code section double-click on the title bar of the form. For this tutorial we are making the username Mike and the password TeamTutorials (it will be case sensitive).

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        strPassword = "TeamTutorials"
        strUser = "Mike"
    End Sub

Next we need to lay out our form. I will have two pictures (one for the names of the controls and on for the text that are in the controls):


Now, let’s start adding out code to the form. Double click on the exit button and add the word End to it. This will kill the application when clicked:

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
    End Sub

The next thing we need to put in the code is the checking to see if the username and password entered match what we have. We also want to check to make sure the password box is not blank, and if it is alert the user to it. The username can be blank.

(Please note that I did this in a way that will make you understand how the If statements work in order to get this tutorial. There are many easier ways to achieve this and there are parts of the code that are un-needed. For example, the Boolean is completely useless and does not need to be there, but I wanted you to see its function.)

    Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
        If txtPassword.Text <> "" Then
            If txtUser.Text = strUser Then
                If txtPassword.Text = strPassword Then
                    booLogin = True
                    If booLogin = True Then
                        MsgBox("Succesfully logged in!", MsgBoxStyle.Information, "Welcome")
                        txtPassword.Text = ""
                        txtUser.Text = ""
                        booLogin = False
                    End If
                    MsgBox("Password is incorrect, please try again.", MsgBoxStyle.Information, "Incorrect Password!")
                    txtPassword.Text = ""
                End If
                MsgBox("Username is incorrect, plaese try again.", MsgBoxStyle.Information, "Incorrect Username!")

            End If
            MsgBox("Please enter a password!", MsgBoxStyle.Information, "Alert!")
        End If
    End Sub

The code above is actually very simple. Just the fact that there are if statement in if statements (nested if statements) makes it confusing to look at. Here is a picture to help line up the if statements and make it a little easier to understand:


We simply check to make sure the password text box is not empty, then we check to make sure the user name equals what it should and if it doesn’t give a message box. Then we check to make sure the password equals or preset one and if not give a message box. If it matches it sets our Boolean equal to false. Then we check to see if the Boolean is equal to true and give the welcome message box if it is. After we log in successfully we blank out both of the text boxes and set the Boolean back to false.

One last thing, since this is a password field, we should make it so that the password comes up as asterisks as the user types it so that other around wont be able to read what the password is. To do this we simply need to click on the text box that we want to do that to and locate the following property in the property toolbar:


and type the asterisk. You can use whatever you would like instead of the asterisk as well. Now let’s test everything. To start run the application and type the username with no password in the box and you should get the following window:


Next type anything in for the password but type the user name incorrectly and you should receive the following box:


Next type the username correctly but type the password in correctly and you should get this box instead:


Finally, type the username and password both in correctly and you should receive our final box that says this all was a success:


This concludes this tutorial. I hope this makes If statements something you can easily understand now. I hope you were able to follow it and thanks for reading.

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join the TeamTutorials mailing list and get the latest tips, tricks, and special discounts for members only.

Thanks! Please click the link in the email we just sent you to confirm.