27 Juli 2010

Program Vibi: Membuat NEW, ADD, UPDATE, EDIT dan DELET

27 Juli 2010
Setelah kita mempelajari materi ini, itu dan ini. Sekarang kita buad program komplitnya, membuat program input data, edit data, update data yang telah di edit, dan mendelet data yang kita inputkan..

Program akan seperti ini


Codingnya seperti ini:
1. General

Public cn As ADODB.Connection
Dim rsTblMhs As ADODB.Recordset
Dim rs As ADODB.Recordset
Dim strsql As String


2. Public Sub_Koneksi

Public Sub koneksi()
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=DatabaseMhs;Data Source=TOSHIBA-PC"
End Sub


3. Sub Kode_Otomatis

Set rs = New ADODB.Recordset
strsql = "Select max(NIM)as NIM from TblMhs"
rs.CursorLocation = adUseClient
rs.Open strsql, cn, adOpenDynamic, adLockOptimistic

If Not rs!NIM = "" Then
KODE = Mid(rs!NIM, 6, 9)
NoUrut = CLng(KODE)
NoUrut = NoUrut + 1
TxtNIM.Text = "080010" + Format(NoUrut, "000")
Else
NoUrut = 1
TxtNIM.Text = "080010" + Format(NoUrut, "000")
End If
End Sub


4. Cmd Add

Private Sub CmdAdd_Click()
If TxtNIM.Text = "" Then
MsgBox "NIM belum diisi", vbInformation, "PESAN"
TxtNIM.SetFocus
ElseIf txtNama.Text = "" Then
MsgBox "NAMA belum diisi", vbInformation, "PESAN"
txtNama.SetFocus
ElseIf TxtAlamat.Text = "" Then
MsgBox "ALAMAT belum diisi", vbInformation, "PESAN"
TxtAlamat.SetFocus
Else
rsTblMhs.AddNew
rsTblMhs("NIM") = TxtNIM.Text
rsTblMhs("NAMA") = txtNama.Text
rsTblMhs("ALAMAT") = TxtAlamat.Text
rsTblMhs.Update
End If
Set DataGrid1.DataSource = rsTblMhs
TxtNIM.Text = ""
txtNama.Text = ""
TxtAlamat.Text = ""
CmdAdd.Enabled = False
CmdUpdate.Enabled = True
CmdNew.Enabled = True
CmdDelete.Enabled = True
End Sub


5. Cmd Delet

Private Sub CmdDelete_Click()
TxtNIM.Locked = False
txtNama.Locked = False
TxtAlamat.Locked = False
TxtNIM.Text = rsTblMhs("NIM")
txtNama.Text = rsTblMhs("NAMA")
TxtAlamat.Text = rsTblMhs("ALAMAT")
If MsgBox("apakah yakin ingin dihapus?", vbYesNo, "KONFIRMASI") = vbYes Then
Set rsTblMhs = cn.Execute("Delete from TblMhs where NIM='" & TxtNIM.Text & " ' ")
MsgBox "Data telah dihapus", vbInformation, "PESAN"
End If
'untuk me-refresh datagrid setelah delete data
If cn.State = adStateOpen Then
setrsTblMhs = New ADODB.Recordset
rsTblMhs.CursorLocation = adUseClient
strsql = "select * from TblMhs"
rsTblMhs.CursorLocation = adUseClient
rsTblMhs.Open strsql, cn, adOpenDynamic, adLockOptimistic
Set DataGrid1.DataSource = rsTblMhs
End If
End Sub


6.Cmd Edit

Private Sub CmdEdit_Click()
DataGrid1.Enabled = True
txtNama.Locked = False
TxtAlamat.Locked = False

TxtNIM.Text = rsTblMhs("NIM")
txtNama.Text = rsTblMhs("NAMA")
TxtAlamat.Text = rsTblMhs("ALAMAT")
CmdUpdate.Enabled = True
End Sub


7.Cmd New

Private Sub CmdNew_Click()
CmdAdd.Enabled = True
CmdNew.Enabled = False
TxtNIM.Locked = False
txtNama.Locked = False
TxtAlamat.Locked = False
kode_otomatis
End Sub


8. cmd Update

Private Sub CmdUpdate_Click()
If rsTblMhs.RecordCount > 0 Then
rsTblMhs.MoveFirst
rsTblMhs.Find "NIM='" & TxtNIM.Text & "'"
If Not rsTblMhs.EOF Then
Set rsTblMhs = cn.Execute("update TblMhs SET NAMA ='" & Trim(txtNama.Text) & "' Where NIM='" & TxtNIM.Text & "'")
Set rsTblMhs = cn.Execute("update TblMhs SET ALAMAT ='" & Trim(TxtAlamat.Text) & "' Where NIM='" & TxtNIM.Text & "'")
Else
MsgBox "Tidak Ada NIM tersebut", vbInformation, "Pesan"
End If
End If
MsgBox "Data Sudah Di ubah", vbInformation, "Pesan"
rsTblMhs.CursorLocation = adUseClient
strsql = "select*from TblMhs"
rsTblMhs.Open strsql, cn, adOpenDynamic, adLockOptimistic
Set DataGrid1.DataSource = rsTblMhs
End Sub


9. Form Load

Private Sub Form_Load()
TxtNIM.Text = ""
txtNama.Text = ""
TxtAlamat.Text = ""
koneksi
Set rsTblMhs = New ADODB.Recordset
rsTblMhs.CursorLocation = adUseClient
strsql = "select*from TblMhs"
rsTblMhs.Open strsql, cn, adOpenDynamic, adLockOptimistic
Set DataGrid1.DataSource = rsTblMhs

CmdAdd.Enabled = False
CmdUpdate.Enabled = False
CmdEdit.Enabled = True

CmdDelete.Enabled = True
TxtNIM.Locked = True
txtNama.Locked = True
TxtAlamat.Locked = True
End Sub


10. Sub Refresh

Private Sub Refresh_Click()
Set DataGrid1.DataSource = Nothing
DataGrid1.Refresh
End Sub


Program selengkapnya bisa di download di sini, Judul projectnya emang membuat kode otomatis, tapi isinya udah lengkap dari kode otomatis, new, add, edit, update, dan delete.. heheheeh. saya malas untuk save as ulang jadi saya rombak dari program sebelumnya ajah.


Selamat belajar

1 comments:

Sayuthie Muhammad mengatakan...

wow...mendingan belajara disini ajah :g:

Poskan Komentar

sampaikan komentar dengan sopan dan bertanggung jawab :)