Rabu, 04 Mei 2016

Add, Update and Delete Records

Introduction

Here I am performing simple save, delete and update operations in a Windows Forms Application using visual basic 6.0


Private function doactionform(ByVal iTag As String)
     Select Case iTag
         Case "New"
                    sSql = " INSERT INTO Users(LoginId, Name, Group, Password)" & _
                            " VALUES(" & _
                            " '" & fStringBoxToDB(TxtLogin) & "', " & _
                            " '" & fStringBoxToDB(TxtUsername) & "', " & _
                            " '" & fStringBoxToDB(CmbGroup) & "', " & _
                            " '" & fStringBoxToDB(Txtpwd) & "'" & _
                            ")"
          Case "Edit"
                    sSql = " UPDATE Users" & _
                            " SET" & _
                                " Name = '" & fStringBoxToDB(TxtUsername) & "', " & _
                                " Group = '" & fStringBoxToDB(CmbGroup) & "', " & _
                                " Password = '" & fStringBoxToDB(Txtpwd) & "' " & _
                            " WHERE
LoginId = '" & Trim(TxtLogin) & "'"
         Case "Delete"

                    sSql = " DELETE FROM Users"
                             " WHERE
LoginId ='" & Trim(TxtLogin) & "'"
     End Select
     odal.ExecQuery strcon, sSql

End function


Public Function ExecQuery(ByVal strcn As String, _
                    ByVal strqry As String) As Boolean

Dim oConn       As ADODB.Connection
Dim lErrNo      As Long
Dim sErrDesc    As String

    ExecQuery = False

    On Error GoTo ErrorHandler

    Set oConn = New ADODB.Connection
    oConn.Open strcn
    oConn.BeginTrans
    oConn.Execute strqry
    oConn.CommitTrans
    oConn.Close
    Set oConn = Nothing

    ExecQuery = True
    Exit Function

ErrorHandler:
    lErrNo = Err.Number
    sErrDesc = Err.Description
    MsgBox lErrNo & " " & sErrDesc
End Function


EmoticonEmoticon