Adding unique data using ADODC in vb6

This tutorial will allow you to save and store data in access database and trapping the duplication of records.

Step 1:
Assuming we already have a database with a Table Name ACCOUNT and data field name of Username & Password, Data Type is Text.

Step 2:
Open your VB Application and go to Project > References, a dialog box will appear check the Microsof ActiveX Data Objects 2.0 Library and click Ok.

Step 3:
Go to Projects > Components, then search for Microsoft ADO Data Control 6.0 (OLEDB) and Microsoft DataGrid Control 6.0 (OLEDB) and click Apply then Ok.

Step 4:
Go to Form1, Add a 2 Labels with a caption of Username and Password, 1 Command Button with a caption of Save and name of cmdSave , Add 2 TextBox with a name of txtUsername and txtPassword, then put a ADO Data Control and DataGrid.

Step 5:
Put the following code in code editor.

Option Explicit

Private Sub cmdSave_Click()
On Error Resume Next
Adodc1.Recordset.MoveFirst
Adodc1.Recordset.Find "Username like '" & txtUsername.Text & "'"
If Adodc1.Recordset.EOF = True Then
    Adodc1.Recordset.AddNew
    Adodc1.Recordset!UserName = txtUsername.Text
    Adodc1.Recordset!Password = txtPassword.Text
    Adodc1.Recordset.Update
Else
    MsgBox "Username already exist", vbExclamation, "Warning"
End If
End Sub

Private Sub Form_Load()
        On Error Resume Next
        Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\harvey\Documents\Database.mdb;Persist Security Info=False"
        Adodc1.RecordSource = "Select  * from Account"
        Set DataGrid1.DataSource = Adodc1
End Sub
If there is duplicate of data, a message box will appear.

Enjoy! :)

Adding unique data using ADODC in vb6 Adding unique data using ADODC in vb6 Reviewed by code-dev on 9:47 PM Rating: 5

No comments:

Powered by Blogger.