Before anything else, I will assume that you have previous
knowledge with regards to constructing basic SQL commands using MS Access. This
tutorial will demonstrate how to create a login form in Visual Basic 6.0 using
MS Access as database engine.
To get us started, we need to create a new table to our
database. Name it ‘Users’, with the following structure:
FIELD NAME
|
DATA
TYPE
|
ATTRIBUTES
|
ID
|
AutoNumber
|
|
Username
|
Text
|
Field Size: 15
|
Password
|
Text
|
Field Size: 255 | Input Mask:*
|
The structure is pretty straight forward. The ID field is
Auto Incremented and will serve as the Primary Key. The Username is of type
Text with the length of 15 characters long. Finally, the Password field 255 in
length.
It is time to populate the table. We are going to start with
two users only for this tutorial. Insert the following records:
ID
|
Username
|
Password
|
1
|
alex
|
Kulot01@
|
2
|
john
|
Whitewolf69
|
Now our table is ready. We only have two authorized login
credentials which of Alex and John with their corresponding password.
Next, we will design our login form in VB6 IDE. Create a
form with similar objects in it as shown below:
Form Layout
Control
|
Property
|
Value
|
Form
|
Name
Caption
BorderStyle
Height
Width
|
Form1
Login
1 – Fixed Single
3135
4845
|
Label1
|
Caption
|
User:
|
Label2
|
Caption
|
Password:
|
Text1
|
Name
Text
|
txtUser
|
Text2
|
Name
Text
PasswordChar
|
txtPassword
*
|
Command1
|
Name
Caption
|
cmdCancel
Cancel
|
Command2
|
Name
Caption
|
cmdLogin
Login
|
Controls, Properties
and Values Matrix
Source Code
Form1.frm
Option
Explicit
Private Sub
cmdCancel_Click()
Unload Me
End Sub
Private Sub
cmdLogin_Click()
Dim rs As New Recordset
Dim user As String
Dim password As String
Dim sql As String
user = txtUser.Text
password = txtPassword.Text
sql = "SELECT * FROM Users WHERE
Username = '" & user & "'"
Debug.Print sql
rs.Open sql, con, adOpenDynamic,
adLockOptimistic
If rs.State Then
While Not rs.EOF
If rs!password = password Then
'Load your main form because
the access has been authorized
Unload Me
Exit Sub
End If
rs.MoveNext
Wend
End If
MsgBox "Invalid Username or
Password!", vbExclamation, "Authentication"
rs.Close
Set rs = Nothing
End Sub
NOTE: Upon running the application,
the first form to load should be the Login form.