In one of the tutorial here in visual basic, we have learned how to create a simple login system without using a database. This time a database will be incorporated in the login system which means the combination of username and password will be retrieved from the record of the database.
Although the two systems are similar but I prefer and I believe most of us would prefer to use a database in a login system. So, what do you think is the advantage of having a database in a login system?
A simple question should be responded by a simple answer. A database makes your system or application more dynamic because we can later on add, change and even remove records from database without changing the actual source code of our program.
That’s why we will be developing a login system with database.
Let’s start the tutorial:
We will create and design our database.
1. We must first create a MS Access database and name it as Data.mdb.
2. Create a new table and name it as tblUser.
3. Save the table and insert the following field:
Field name – Data Type
ID – Autonumber
myUsername – Text
The table should look like the image below:
4. We will be posting also a tutorial on user registration but for now we will insert a record directly in our table. Fill in only the myUsername and myPassword since ID is AutonNumber it will automatically assign a number to the record. For the myUsername, insert admin and same with the myPassword. After you have inserted the value, your table will be like this:
5. Close the table.
After we have created our database, let us proceed in our visual basic program.
We will skip the procedures on how to connect visual basic 6 to ms access, but if you forgot that, kindly visit the tutorial on how to connect vb6 to ms access.
1. Open your visual basic 6 program and create a new project, select Standard EXE.
2. Let’s add a form to our project and name it as LoginFrm.
3. Create a form that will look like the image below.
We need two Labels, two TextBoxs and a Command Button. The caption of the first Label is username and the second is password. Name first textbox as txtusername and the second as txtpassword and lastly name the command button as cmdOK with a caption of OK.
4. We are halfway done; we will now create a procedure or function for login. Kindly double click the command button and insert the following code.
If rs.State = adStateOpen Then rs.Close sql = " Select * From tbluser Where myusername='" & txtusername.Text & "'" rs.Open sql, conn If (rs.EOF And rs.BOF) Then MsgBox ("The system could not log you on.Make sure your username and password is correct.") Else If rs.Fields(2).Value = txtpassword.Text And rs.Fields(1).Value = txtusername.Text Then MsgBox "Access Granted!", vbInformation, "" Else MsgBox ("Incorrect password") End If End If
The code above will compare the combination of username and password entered by the user in the textbox from the record of the database. If it matches the username and password a message will appear (Access Granted) else you will be prompted an error message indication that the username and password entered is not the same with the record in our database.
5. To test if our project is working kindly press F5.
Hope you enjoy our simple login system without database tutorial.
Your comments and suggestions are highly appreciated.
More Visual Basic 6 Tutorials to come.
Thank you for visiting inettutor.com
The source code is available for download as your reference.