Building a Commercial e-Statement Application in VB 6 – Login Form in VB6 using SQL
During our last post, we created Modules and Menu for the e-statement application. Today we are going to create the login form in vb6 using sql for our application which will have two user types:
The Operator(s) will have no security rights to access the Application Configurations module. This module will be restricted only to the system Administrator(s). In this post we are going to add the test users directly in the database.
ATTENTION: For you to understand this post you need to look at the previous posts if you haven`t done so already.
In Summary, we are going to do the following in this article
- Add two users into the Users table
- Create a Login Form
- Connect the Login Form to the SQL Server database
- Authenticate users
- Check if logged in user is an Administrator or Operator and give access to the correct system module(s)
Without wasting any more time lets login to the Microsoft SQL Server with the ‘sa’ username and password.
Add two users into the Users table
Go to the “SmartMail” database expand on the Tables and right click the Users table and choose “Edit Top 200 Rows”.
Add two usernames as below:
The “Admin” username has been given the admin rights on the “isAdmin” column.
Once that is done we can now create the Login Form and start coding.
Create a Login Form
Open the SmartMail project and add a new Form.
Add the following to the form
- One Data combo
Name the Data combo as “dcUser”. This will have the drop down list of all the usernames in the database
- One Textbox
Name the Textbox as “txtPass” , Caption leave it empty
- Two Command buttons
Name one as “cmdLog” and Caption “Login”. The second one name it as “cmdCancel” and caption “Cancel”
- Three Label
These three labels will have the following Caption: one is for the “Username” second “Password” and third “Please select your username and enter your password in the space provided below.”
See the Login Form below
I have added an Image of a padlock and an extra label at the bottom to make the Login Form more attractive.
Once you have the controls on the form its time to code.
Form Load Event
Double click your Login form and add the below code:
'eStatement Application Development 'www.smarttechdiary.com Private Sub Form_Load() 'Get usernames from the Users table bind_dc "SELECT * FROM Users", "Username", dcUser, "ID" End Sub
Double click on the Login button and add the following code:
'eStatement Application Development 'www.smarttechdiary.com Private Sub cmdLog_Click() 'Verify If dcUser.Text = "" Then dcUser.SetFocus: Exit Sub If txtPass.Text = "" Then txtPass.SetFocus: Exit Sub Dim strPass As String Dim isDisabled As String 'Check if use has been disabled isDisabled = getValueAt("SELECT IsDisabled FROM Users WHERE ID='" & dcUser.BoundText & "'", "IsDisabled") If isDisabled = "True" Then MsgBox "Sorry Your Username Has Been Disabled,Please Contact The System Administrator", vbCritical, "Login Failure" Exit Sub End If strPass = getValueAt("SELECT Username,Password FROM Users WHERE ID='" & dcUser.BoundText & "'", "Password") If LCase(txtPass.Text) = LCase(strPass) Then With CurrUser .USER_NAME = dcUser.Text .USER_PK = dcUser.BoundText .USER_ISADMIN = getValueAt("SELECT ID,IsAdmin FROM Users WHERE ID=" & dcUser.BoundText, "IsAdmin") End With Unload Me Else MsgBox "Invalid password.Please try again!", vbExclamation txtPass.SetFocus LoginTrials = LoginTrials + 1 If LoginTrials = 3 Then MsgBox "You Have Exceeded Your Login Trials!The System Will Now Exit", vbCritical, "Exceeded Login Trials" End End If End If strPass = vbNullString End Sub
The Cacel button closed the Login form when a user chooses not to Login to the e-Statement system
Double click on the Cancel button and add the following code:
'eStatement Application Development 'www.smarttechdiary.com Private Sub Form_Unload(Cancel As Integer) Unload Me End Sub
Add the following form to your form to complete this login system
'eStatement Application Development 'www.smarttechdiary.com Option Explicit Dim LoginTrials As Byte Private Sub cmdCancel_Click() MAIN.CloseMe = True Unload Me End Sub Private Sub txtPass_Change() txtPass.SelStart = Len(txtPass.Text) End Sub Private Sub txtPass_GotFocus() HLText txtPass End Sub
Test Login Form in VB6 Using SQL
If you have been following each step from the beginning of this series, you should have a working Login form by now.
Click on run to run your application and Login using your username and password.
Mine is working nicely , if you any problem and need help you can contact me and I will try my best to help you.
I hope you have enjoyed this article, if you have suggestions please leave your comments below. Until next time do have a nice week. Please share with your friends.