Mesran

Hanya ingin berbagi kebaikan dan mendapatkan kebaikan yang lain.

VB Database Lanjut : Form Mahasiswa

Berikut ini program lengkap dari Form Mahasiswa
Listing Modul1.Bas

Public Db As New ADODB.Connection
Public RS As New ADODB.Recordset
Public SQL As String

Sub OPENDB()
    If Db.State = adStateOpen Then Db.Close
    Db.CursorLocation = adUseClient
    Db.Open "Provider=MSDASQL.1;Persist Security Info=False;Data Source=KRS"
End Sub

Sub Center(f As Form)
    f.Move (Screen.Width - f.Width) / 2, (Screen.Height - f.Height) / 4
End Sub

Listing Program frmMahasiswa

Sub Hapus()
    NPM.Enabled = True
    NPM.Text = ""
    NAMA.Text = ""
    TAHUN.Text = ""
    JENJANG.Text = ""
    JURUSAN.Text = ""
    
    Call RubahCMD(True, False, False, False)
    CmdProses(1).Caption = "&Simpan"
End Sub

Sub ProsesDB(Log As Byte)
   Select Case Log
        Case 0
            SQL = "INSERT INTO Mahasiswa(NPM, Nama, TAHUN, JENJANG, JURUSAN)" & _
                " values('" & NPM.Text & _
                "','" & NAMA.Text & _
                "','" & TAHUN.Text & _
                "','" & JENJANG.Text & _
                "','" & JURUSAN.Text & "')"
        Case 1
            SQL = "UPDATE Mahasiswa SET Nama ='" & NAMA.Text & "'," & _
                  " TAHUN = '" & TAHUN.Text & "'," & _
                  " JENJANG = '" & JENJANG.Text & "'," & _
                  " JURUSAN = '" & JURUSAN.Text & "' " & _
                  " where NPM ='" & NPM.Text & "'"
        Case 2
            SQL = "DELETE FROM Mahasiswa WHERE NPM='" & NPM.Text & "'"
    End Select
    MsgBox "Pemorosesan RECORD Database telah berhasil...!", vbInformation, "Data Mahasiswa"
    Db.Execute SQL, adCmdTable
    Call Hapus
    Adodc1.Refresh
    NPM.SetFocus
End Sub

Sub TampilMahasiswa()
    On Error Resume Next
    NPM.Text = RS!NPM
    NAMA.Text = RS!NAMA
    TAHUN.Text = RS!TAHUN
    JENJANG.Text = RS!JENJANG
    JURUSAN.Text = RS!JURUSAN
    
    Call RubahCMD(False, True, True, True)
    CmdProses(1).Caption = "&Edit"
    NPM.Enabled = False
End Sub

Private Sub CmdProses_Click(Index As Integer)
Select Case Index
    Case 0
        Call Hapus
        NPM.SetFocus
    Case 1
        If CmdProses(1).Caption = "&Simpan" Then
            Call ProsesDB(0)
        Else
            Call ProsesDB(1)
        End If
    Case 2
        x = MsgBox("Yakin RECORD Mahasiswa Akan Dihapus...!", vbQuestion + vbYesNo, "Mahasiswa")
        If x = vbYes Then ProsesDB 2
    Case 3
        Call Hapus
        NPM.SetFocus
    Case 4
        Unload Me
    End Select
End Sub

Private Sub Form_Load()
    Call OPENDB
    Call Hapus
    
    JURUSAN.AddItem "Teknik Informatika"
    JURUSAN.AddItem "Manajemen Informatika"
    JENJANG.AddItem "D3"
    JENJANG.AddItem "S1"
End Sub

Private Sub NPM_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        If NPM.Text = "" Then
            MsgBox "Masukkan NPM Mahasiswa !", vbInformation, "Mahasiswa"
            NPM.SetFocus
            Exit Sub
        End If
        SQL = "SELECT * FROM Mahasiswa WHERE NPM='" & NPM.Text & "'"
        If RS.State = adStateOpen Then RS.Close
        RS.Open SQL, Db, adOpenDynamic, adLockOptimistic
        If RS.RecordCount <> 0 Then
            TampilMahasiswa
        Else
            x = NPM.Text
            Call Hapus
            NPM.Text = x
            Call RubahCMD(False, True, False, True)
            CmdProses(1).Caption = "&Simpan"
        End If
        NAMA.SetFocus
    End If
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

Filed under: Pemrograman VB, Perkuliahan, ,

2 Responses

  1. ardian syahputra says:

    mantap yah pak.,,,

Terima kasih telah memberikan komentar anda.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives Per Tahun

%d bloggers like this: