A simple Visual Basic Login Form

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
Hello, and welcome to my simple login form tutorial. This is situated on the fact that you are using Visual Basic 6.0

1. Starting off you are going to need to open up Visual Basic 6.0 Development window, and create a standard .exe file

2. Now you need to drag onto the form the correct command property.

These are as follows:

2 Command Buttons
2 Text Boxes
2 Labels

Set the following properties for the different controls.

Command Button 1
Name: cmdLogin
Caption: Login

Command Button 2
Name: cmdClose
Caption: Close Program

Text Box 1
Name: txtUsername
Text: Leave this empty

Text Box 2
Name: txtPassword
Text: Leave this empty
PasswordChar: * (Reccommended so that the text appears as stars, added security.)

Label 1
Caption: Username:

Label 2
Caption: Password

Now that you have the correct controls on the screen then we can get down to the coding!

In order to access the code of the program, right click anywhere on the form and click on view code.

Code:
'Global Variables
Dim Username As String                       'Username Variable
Dim Password As String                       'Password Variable
 
Private Sub cmdLogin_Click()
 
Username = "TestUser1"              'What the Username Variable is
Password = "TestUser1"              'What the Password Variable is
 
'If Statement Starting Here
'It States that if the username is = to text box and password is = password text box, then it should login, else display error message!
If Username = txtUsername And Password = txtPassword Then
 
           frmLoginSuccess.Show    'Shows the Login Success Form
           frmLoginScreen.Hide       'Hides the Login Screen
 
Else
 
'A Message box that displays you have entered the wrong username and password
           
MsgBox ("You have entered the wrong username and password")
 
End If
 
End Sub
 
Private Sub cmdClose_Click()
 
Unload Me      'Closes the program
 
End Sub

This is a basic piece of code that checks that what is etnered in the username box, and what is entered in the password box, is equal to what the username and password variables have been set at.

3. Now you need to name the forms. Click the form and go to the name property, and call the form frmLoginScreen.

4. Now create a new form. The way to do this is to right click in the project explorer area, and click on new, then select form. Now call this form frmLoginSuccess.

5. Now run the program and see if it works. In theory, when you have entered the correct username and password, it should hide the Login Form and show the Login Success form.

Well that is it!

Test It, and if there are any problems, then simply post here or PM me.

--
Zenax
 
Top