Visual Programming

  Visual Basic Tutorial

Creating a Login Dialog

Objectives

This guide  will help you to: -  

  • Use a login dialog in your application

Introduction

Creating a Login Dialog

       Show the Main Application Form

       Check for Username Too

Help Desk Exercise


Introduction

If you need to restrict access to your application so that only certain users can run it, then a login dialog may be what you need. Many applications are meant to be run only by authenticated users.  Login dialogs are commonly used to restrict access, although there are other methods too.


Creating a Login Dialog

You can create you own login dialog by adding a VB form and adding the appropriate textboxes and buttons.  However, there is a slightly easier way.  You can get VB to do most of the work for you. 

 

Open up your VB project.

 

Click on Add Form in the Project menu.  Select Log in Dialog.

 
 

VB should then create a form similar to the one in the picture.

 

Make sure you change it to the 'Default Start-up Form' in Project>>Properties.

 

 

If you you go to the coding window by double-clicking the form, you will see that VB has also written some code for you.

There are two main parts to this code.  There is the code that executes when the user clicks on the Cancel button.  See below.  Don't worry about what it all means yet.

Then there is the code that executes when the user clicks on the OK button:-

Run your login box dialog and play with it for a bit.  Notice that you can enter any username you choose but the password you have to enter is the word password.  I.e. This is from the line:-

   If txtPassword = "password" Then

You will see a few problems we need to correct.

  1. The Cancel button works fine but the OK button just hides the Login dialog.  It doesn't display the main application form.

  2. Only the password is checked, the code does not check to see if the username is correct too.

First of all, let's fix it so that if the user inputs the correct password, then the login dialog is closed and the main application form is loaded.

Show the Main Application Form

Let's change the code as follows (don't type the line numbers in):-

1

 

2

 

3

 

4

5

6

 

7

 

8

9

10

11

12

13

14

15

16

 

17

 

18

19

20

21
 

Option Explicit

Public LoginSucceeded As Boolean

Private Sub cmdCancel_Click()
   'set to false 'to denote a failed login
   LoginSucceeded = False
   Unload Me
End Sub

Private Sub cmdOK_Click()
   'check for correct password
   If txtPassword = "password" Then
      LoginSucceeded = True
      Unload Me
   Else
      MsgBox "Invalid Password, try again!", , "Login"
      txtPassword.SetFocus
      SendKeys "{Home}+{End}"
   End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
   'when form unloads check for correct password
   If LoginSucceeded = True Then
      frmTroubleShooting.Show ' show the main application form
   End If
End Sub
 

First check line 19.  You might not have a frmTroubleShooting to show.  Change this name to the name of the from you want to show next.

Here is an explanation of the code:-

Line 2:    Public LoginSucceeded As Boolean

This line declares a Boolean variable called LoginSucceeded.  If this variable has a value of false it means that the Login information the user typed in was incorrect.  If it has a value of true it means that the Login information the user typed in was correct.

~~~~~~ user clicks on the Cancel button ~~~~~~

Line 3:    Private Sub cmdCancel_Click()

This block of code from  Private Sub cmdCancel_Click() to End Sub runs if the user clicks on the Cancel button.

Line 4:    LoginSucceeded = False

This line sets the LoginSucceeded  variable to false because the user clicked cancel.

Line 5:    Unload Me

Since the user clicked the Cancel button, may as well unload the form.

Line 17:    Private Sub Form_Unload(Cancel As Integer)

This block of code from  Private Sub Form_Unload(Cancel As Integer) to End Sub runs if the form is unloaded.

Line 18:    If LoginSucceeded = True Then

Check to see if the variable LoginSucceeded has a value of true or false.  Since the user clicked on the Cancel button it will have a value of false.  Therefore the next line that loads the main application form will not be executed.

~~~~~~ user clicks on the OK button ~~~~~~

Line 7:    Private Sub cmdOK_Click()

This block of code from  Private Sub cmdOK_Click() to End Sub runs if the user clicks the OK button.

Line 8:       If txtPassword = "password" Then
                       LoginSucceeded = True
                       Unload Me

This first line checks to see if the text the user entered in the txtPassword textbox matches the word password.  If it does match, the next two lines are executed.  The LoginSucceeded  variable is set to true because the user typed in the correct password.  The Login Dialog is unloaded.  Then Line 17 will be run as the form is unloaded.

Line 10:         Else
                           MsgBox "Invalid Password, try again!", , "Login"

If the user entered an incorrect password, a message box is displayed telling them.
 

Although this may seem like a lot of code to understand, a good way to try an understand is to put different break points in your code and to step through each line one at a time.

Check for Username Too

Now we need to fix the code so it also checks for a username.

Change you line of code...

  If txtPassword = "password" Then

to

  If txtPassword = "password" AND txtUserName = "user" Then
 

Now it will check both the username and password match.

~Try the activity~

Activity A

Change the line of code...

  If txtPassword = "password" AND txtUserName = "user" Then

...to a different username and password.

 


Help Desk Exercise

First make sure that your login dialog works properly and your main troubleshooting form is displayed if the user types in the correct username and password.

~Try the activity~

Activity B

What if you need your login dialog to accept different usernames and passwords.

Here is a hint on how to do that:-

If (txtPassword = "password" AND txtUserName = "user") Or (txtPassword = "password2" AND txtUserName = "user2") Then

This code should all be on one line by the way.

 


Fini
 

 

 

   

  Unit Information

Assessment

Syllabus

Scheme of Work

Notes &Tutorials

Assignments

Quizzes

Books & Things

Links

ADR 2002