Thursday, 20 September 2012

Database connectivity with MS Access in VB 6.0


Database connection with VB 6.0
VB provides a rich set of of database connectivety.Most of work which is needed done by aotomatically.There’re some control which provides database connectivety.Here i’m going to implement it using ADODB.
ADODB provides connection class.which handle insert ADODB,update,delete operation efficiently.
Ø       Make an objet of ADODB,Connection
Ø       Then call open function which takes connection string as argument
Ø       Connection string comprise database enzine name and your database path.I have supply relative path my database is in current folder.


Maked by Raj kumar

Controls :
    ( Three label,,Three Moniter,Three  textbox )

Coding

Dim con As New ADODB.Connection

Private Sub btnDelete_Click()
    If txtRoll.Text = "" Then
        MsgBox "Please enter roll no."
        txtRoll.SetFocus
    Else
        If MsgBox("Are you sure to delete this record", vbYesNo, "Girfa") = vbYes Then
            con.BeginTrans
            con.Execute "delete from stu where roll=" & txtRoll.Text
            con.CommitTrans
            txtRoll.Text = ""
            MsgBox "Record deleted"
        End If
        
    End If
End Sub

Private Sub btnSave_Click()
    If txtRoll.Text = "" Or txtName.Text = "" Or txtCity.Text = "" Then
        MsgBox "Please Fill alll field"
    Else
        con.BeginTrans
        con.Execute "insert into stu values(" & txtRoll.Text & ",'" & txtName.Text & "','" & txtCity.Text & "')"
        con.CommitTrans
        MsgBox "Saved", , "Girfa"
        
    End If
End Sub

Private Sub btnUpdate_Click()
    If txtRoll.Text = "" Or txtName.Text = "" Or txtCity.Text = "" Then
        MsgBox "Please enter all field value"
    Else
        con.BeginTrans
        con.Execute "update stu set name='" & txtName.Text & "',city='" & txtCity.Text & "' where roll=" & txtRoll.Text
        con.CommitTrans
        MsgBox "Record has been updated", , "Girfa"
    End If
End Sub

Private Sub Form_Load()
    con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=bsw.mdb;Persist Security Info=False"
End Sub



No comments:

Post a Comment