Tuesday, September 9, 2014

How to Create a Simple Login Form in VB6 Using MS Access Database



 
In every database system, a very important function which contributes to the security of the application is the login form. Using a login facility, we can filter or prevent the unauthorized access to our application.

In this simple tutorial you will learn how to create a dynamic login form using VB6 and Microsoft Access Database. For connectivity, we will be using Microsoft ADO. I will assume that you already have your MS Access database, if not start a new database. Follow the schema of our ‘user’ table below: 

Table Name: user 
Field Name                       Data Type                          Attribute                             Value 
username                           Text                                       Field Size                           15
password                           Text                                       Field Size                            8
fullname                              Text                                       Field Size                           100

Next step would be the adding of new form to your Project. Please see description below:

Graphical User Interface

Controls and their Attributes 

Label     Control Type                                      Attribute                       Value 
1            TextBox                                            Name                              txtUser
2            TextBox                                            Name                              txtPasswd
                                                                        PasswordChar              *
3             Adodc                                             Name                              Adodc1
                                                                        CommandType              1 – adCmdText
                                                                        RecordSource               SELECT * FROM User
4              CommandButton                          Name                              cmdLogin
5              Label                                             Caption                           User name:
6              Label                                             Caption                           Password:

Next, let us setup your connection using Adodc control. Please follow the steps below: 

  1. Right click on your Adodc1 control.
  2. Select ADODC Properties.  
  3. Toggle ‘Use Connection String’ option box and click Build.  
  4. Under Connection tab,  type or browse your MS Access database  and set the credential if there is any, otherwise leave the credential box as is.  
  5. Finally, check your connection by clicking Test Connection button. If you have successfully set the connection, you will receive a message box saying “Test Connection Succeeded.”.  
  6. Click OK. 
  7. Lastly, simply copy and paste the code below to your code window:


Source Code: 

Private Sub cmdLogin_Click()
    Dim user As String
    Dim passwd As String
    Dim result As Integer
    Dim sql As String

    user = txtUser.Text     'fetch the username from the box

    passwd = txtPasswd.Text 'fetch the password from the box

    'sql query below

    sql = "SELECT User.username, User.password, User.fullname " _

    & "From [user] " _

    & "WHERE (((User.username)='" & user & "') AND " _

    & "((User.password)='" & passwd & "'))"



    Adodc1.RecordSource = sql   'run query

    Adodc1.Refresh  'refresh recordset

   

    result = Adodc1.Recordset.RecordCount   'count the number of query result

   

    If result > 0 Then 'if result is greater than zero, it means that the user is valid

       

       

       

        Unload Me   'unload login form

    Else

        MsgBox "User name or password is incorrect!", vbExclamation

    End If

   

End Sub



Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

    If UnloadMode = 0 Then  'X button was clicked

        End

    End If

End Sub

No comments:

Post a Comment