Minggu, 29 Januari 2012

Postingan jawaban no 2 (Sri Rahma Idayani)

Untuk kasus lihat disini kasus

module :
Public DB As New ADODB.Connection
Public RS As New ADODB.Recordset
Sub openDB()
    If DB.State = adStateOpen Then DB.Close
    DB.CursorLocation = adUseClient
    DB.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\apache\mysql\data\test1\test.mdb;Persist Security Info=False"
End Sub

form barang :

Private Sub Command1_Click()
    Adodc1.Refresh
End Sub

Private Sub Form_Load()
    Call bersih
    openDB
End Sub
Sub bersih()
    kode.Text = ""
    nama.Text = ""
    harga.Text = ""
        Cmdproses(1).Caption = "simpan"
    rubahCMD True, False, False, False
   
End Sub
Private Sub Cmdproses_Click(Index As Integer)
    Select Case Index
    Case 0
        Call bersih
        kode.SetFocus
    Case 1
   
        If Cmdproses(1).Caption = "simpan" Then
            prosesDB 1
        Else
            prosesDB 2
        End If
       
    Case 2
        x = MsgBox("yakin!", vbYesNo)
        If x = vbYes Then prosesDB 3
       
    Case 3
        Call bersih
        kode.SetFocus
    Case 4
        Unload Me
    End Select
   
End Sub
Sub rubahCMD(L0 As Boolean, L1 As Boolean, L2 As Boolean, L3 As Boolean)
    Cmdproses(0).Enabled = L0
    Cmdproses(1).Enabled = L1
    Cmdproses(2).Enabled = L2
    Cmdproses(3).Enabled = L3
   
End Sub
Sub prosesDB(log As Byte)
    Select Case log
    Case 1
        SQL = "INSERT INTO barang(kode,nama,harga)" & _
            "values('" & kode.Text & "','" & _
                         nama.Text & "','" & _
                         harga.Text & "')"
    Case 2
        SQL = "update barang set nama='" & nama.Text & "'," & _
            "harga='" & harga.Text & "' " & _
            "where kode='" & kode.Text & "'"
    Case 3
        SQL = "delete from barang where kode='" & kode.Text & "'"
    End Select
    DB.Execute SQL, adCmdText
    Adodc1.Refresh
    Call bersih
    kode.SetFocus
       
End Sub
Sub showDB()
    With RS
        On Error Resume Next
        kode.Text = RS!kode
        nama = !nama
        harga = !harga
    End With
    Cmdproses(1).Caption = "edit"
   
End Sub
Private Sub kode_keypress(keyascii As Integer)
    If keyascii = 13 Then
        If kode.Text = "" Then Exit Sub
        SQL = "select * from barang " & _
            "where kode='" & kode.Text & "'"
        If RS.State = adStateOpen Then RS.Close
        RS.Open SQL, DB, adopendymic, adLockOptimistic
        If RS.RecordCount <> 0 Then
            Call showDB
           
            rubahCMD False, True, True, True
        Else
            x = kode
            Call bersih
            kode = x
            rubahCMD False, True, False, True
            Cmdproses(1).Caption = "simpan"
        End If
        nama.SetFocus
    End If
   
End Sub

Hasil program:

Tidak ada komentar:

Posting Komentar