Friday, March 21, 2014

Add Edit Delete Records in Visual Basic 6 and MS Access

This tutorial teaches you how to add edit and delete records using Visual Basic 6 and MS Access. 

====================
CODES FOR MODULE 1 
====================
Option Explicit

Public con As ADODB.connection
Public ado As ADODB.Recordset
Public useradd As Boolean, edituser As Boolean
____________________________________________________________________________
Public Sub connection()
Set con = New ADODB.connection
con.CursorLocation = adUseClient
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " & App.Path & "\Database1.mdb"
con.Open
End Sub

====================
CODES FOR FRMMAIN 
====================
Option Explicit
________________________________________________________________
Private Sub cmdadd_Click()
useradd = True
edituser = False
Load frmadduser
frmadduser.Show
frmadduser.Caption = "Add User"
frmmain.Enabled = False
End Sub
________________________________________________________________
Private Sub cmddelete_Click()
Dim res As VbMsgBoxResult
With ado
    If .BOF And .EOF = True Then
        MsgBox "Empty Database!", vbExclamation, "Error"
    Exit Sub
    Else
    res = MsgBox("Are you sure you want to delete " & vbCrLf & vbCrLf & "Username: " & ado!user_name, vbYesNo + vbQuestion, "confirm")
        If res = vbYes Then
        .Delete
        .Requery
        Set DataGrid1.DataSource = ado
        Else
        Exit Sub
        End If
        End If
        End With
End Sub
_______________________________________________________________
Private Sub cmdedit_Click()
With ado
    If .BOF And .EOF = True Then
        MsgBox "Empty Database!", vbExclamation, "Error"
    Exit Sub
    Else
        edituser = True
        useradd = False
        frmmain.Enabled = False
        Load frmadduser
        frmadduser.Show
        frmadduser.Caption = "Edit User"
        End If
        End With
End Sub
_______________________________________________________________
Private Sub Form_Load()
Module1.connection
On Error Resume Next
Set ado = New ADODB.Recordset
ado.Open "Select * From tbluser", con, adOpenStatic, adLockPessimistic
Set DataGrid1.DataSource = ado
End Sub

======================
CODES FOR FRMADDUSER
======================

Option Explicit
Dim modeval As Boolean
_____________________________________________________________________________
Private Sub cmdcancel_Click()
frmmain.Enabled = True
Unload Me
End Sub
_____________________________________________________________________________
Private Sub cmdsave_Click()
Dim resp As VbMsgBoxResult
    If txtuser.Text = "" Or txtpass.Text = "" Or txtpass2.Text = "" Then
    MsgBox "Do not leave empty text field(s).", vbExclamation, "Error"
    Exit Sub
Else
    If txtpass.Text <> txtpass2.Text Then
    MsgBox "Password does not matched.", vbExclamation, "Error"
    Exit Sub
Else
    Call validate_user
    If modeval = False Then
save:
    Dim res As VbMsgBoxResult
    res = MsgBox("Do you want to save this record?", vbYesNo + vbQuestion, "save?")
    If res = vbYes Then
        If useradd = True And edituser = False Then
            ado.AddNew
edit:
    ado!user_name = txtuser.Text
    ado!password = txtpass.Text
    ado!password2 = txtpass2.Text
    If useradd = True And edituser = False Then
        ado.Update
        ado.Requery
    ElseIf edituser = True And useradd = False Then
        ado.UpdateBatch adAffectCurrent
    End If
        frmmain.Enabled = True
        Load frmmain
        frmmain.Show
        Unload Me
    ElseIf edituser = True And useradd = False Then
        GoTo edit:
    End If
    Else
        Exit Sub
    End If
    ElseIf modeval = True Then
        If txtval.Text = txtuser.Text Then
    GoTo save:
    Else
    MsgBox "Duplicate entries are not allowed." & vbCrLf & vbCrLf & "user_name" & "''" & txtuser.Text & "'' already exists.", vbExclamation, "Error"
    txtuser.SetFocus
    End If
    End If
    End If
    End If
End Sub
_______________________________________________________________________________
Private Sub Form_Load()
    If edituser = True And useradd = False Then
        txtuser.Text = ado!user_name
        txtpass.Text = ado!password
        txtval.Text = ado!user_name
    Else
        Exit Sub
    End If
End Sub
_______________________________________________________________________________
Private Sub validate_user()
Dim rs As New ADODB.Recordset
rs.Open "Select * From tbluser Where user_name = '" & txtuser.Text & "'", con, adOpenStatic, adLockReadOnly
    If rs.RecordCount < 1 Then
    modeval = False
    Exit Sub
Else
    modeval = True
End If
    Set rs = Nothing
End Sub

2 comments: