Showing posts with label ADO. Show all posts
Showing posts with label ADO. Show all posts

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

Saturday, August 30, 2014

Setting Up the ADO Data Control

To create an ADO Data Control that exposes a Recordset in your application, at the minimum you need to do the following:
  • Specify a Connection by filling in the ConnectionString property.
  • Specify how to derive a Recordset by setting the RecordSource property (which is a complex property requiring its own dialog box to set up).
The detailed steps are as follows:

STEP BY STEP
Creating an ADO Data Control


  1. Add the Microsoft ADO DataControl 6.0 (OLEDB) from the Project, Components menu dialog box. The ADO Data Control icon should now appear in the VB toolbox.
Adding the Microsoft ADO Data Control to your project's components.
    2.  Place an instance of the ADO Data Control on the form.
 
Placing an instance of the ADO Data Control on a form.


3. Change the control's Name and Caption from their default values. (The Caption is for information only, so you can set it to whatever you think will be most informative for the user.) 

4. Set the ConnectionString property using steps 5–9.

5. Click the ellipsis next to the ConnectionString property in the ADO Data Control's Properties window to bring up the Property Page dialog box for this property, as shown below.



The first and only Property Page dialog box for the ADO Data Control's ConnectionString property.


6. As Source of Connection, choose one of the following three options:
  • Ese Data Link File. If you choose this option, you will be able to click the Browse button to specify an existing *.UDL file).
  • Use ODBC Data Source Name. If you choose this option, you will be able to choose an existing ODBC DSN from the drop-down list, or you can create a new DSN by clicking the New button.
  • Use Connection String. If you choose this option, you will be able to click the Build button to bring up the Data Link Properties tabbed dialog box. 


The following steps assume that you have chosen this option. 
 

On the Provider tab of the Data Link Properties tabbed dialog box, choose an OLE DB data provider, such as Microsoft Jet 3.51 OLE DB

Choosing a provider for the ADO Data Control's ConnectionString property.


7. The Connection tab of the Data Link Properties tabbed dialog box will vary in appearance, depending on the provider specified in the preceding step. In the case if the Microsoft Jet 3.51 OLE DB, you are prompted to choose an Access data file and set some security options.



Setting up connection information for the Jet provider
   

8.  Click OK to accept the ConnectionString options you have built.

9. Still in the ADO Data Control's Properties window, navigate to the RecordSource property and click the ellipsis button.

10. On the RecordSource tab (see Figure 8.18) of the resulting Property Page dialog box, choose the CommandType (adCmdUnknown, adCmdText, adCmdTable, adCmdStoredProc).




The Property Page dialog box for an ADO Data Control's RecordSource property.

  

11. Complete the dialog box appropriately for the CommandType that you chose:
  • If you chose adCmdText, fill in the text of a valid Select statement in the Command Text field



A valid Select statement as command text for the RecordSource property.
  •  If you chose adCmdTable or adCmdStoredProc, fill in the appropriate table or stored procedure     name in the Table or Stored Procedure Name drop-down list.
A table or stored procedure name for the RecordSource property.
12. Click OK to end the RecordSource dialog box.