Database dan table yang akan digunakan dalam aplikasi ini sama dengan bahasan Akses Database MySQL menggunakan Delphi jadi saya tidak perlu membahas lagi cara pembuatan database dan tablenya kalau udah lupa buka kembali deh.....
Langsung saja, jalankan Aplikasi Visual Basic 6.0
Ikuti langkah berikut :
1. Pada dialog New Project, pilih VB Enterprise Edition Controls - Open
2. Tambahkan ferensi project dengan cara klik Menu Project - References...
3. Pilih 'Microsoft ActiveX Data Objects 2.8 Library' Pada dialog References - OK
4.Selanjutnya pada form project yang baru, tambahkan object dan atur propertiesnya seperti table berikut :
Component
|
Properties
|
Value
|
Label
|
Caption
|
Kode Jurusan
|
Label
|
Caption
|
Nama Jurusan
|
TextBox
|
Name
MaxLength
Text
|
Txtkd
5
|
TextBox
|
Name
MaxLength
Text
|
Txtnm
30
|
CommandButton
|
Caption
|
&Baru
|
CommandButton
|
Caption
|
&Simpan
|
CommandButton
|
Caption
|
&Hapus
|
CommandButton
|
Caption
|
&Keluar
|
DataGrid
|
Name
|
DataGrid1
|
buat tampilannya seperti berikut :
5. Deklarasikan 2 variable di area General dengan namaDim MyDB As New ADODB.Connection
Dim rs_jur As New ADODB.Recordset
6. Isikan procedure pada Load milik Form
Private Sub Form_Load()
MyDB.Open "Driver={MySQL ODBC 5.1 Driver}; Server=localhost; Port=3306; Database=latihan; UID=root; PWD=ichwan"
If MyDB.State <= 0 Then
MsgBox "Connection ke MySQL Gagal...!"
Return
End If
End Sub
MyDB.Open "Driver={MySQL ODBC 5.1 Driver}; Server=localhost; Port=3306; Database=latihan; UID=root; PWD=ichwan"
If MyDB.State <= 0 Then
MsgBox "Connection ke MySQL Gagal...!"
Return
End If
End Sub
7. Buat Procedure dengan nama berikut :
Sub kosong()
txtkd.Text = ""
txtnm.Text = ""
txtkd.SetFocus
End Sub
Sub tampil()
txtkd.Text = rs_jur!kd_jur
txtnm.Text = rs_jur!nm_jur
txtkd.SetFocus
End Sub
Sub refresh_data()
If rs_jur.State > 0 Then
rs_jur.Close
End If
rs_jur.CursorLocation = adUseClient
rs_jur.Open "select * from jurusan", MyDB, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs_jur
End Sub
txtkd.Text = ""
txtnm.Text = ""
txtkd.SetFocus
End Sub
Sub tampil()
txtkd.Text = rs_jur!kd_jur
txtnm.Text = rs_jur!nm_jur
txtkd.SetFocus
End Sub
Sub refresh_data()
If rs_jur.State > 0 Then
rs_jur.Close
End If
rs_jur.CursorLocation = adUseClient
rs_jur.Open "select * from jurusan", MyDB, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs_jur
End Sub
8. Procedure Activate Form :
Private Sub Form_Activate()
refresh_data
End Sub
refresh_data
End Sub
9. Procedure OnClick pada Tombol Baru :
Private Sub Command2_Click()
kosong
End Sub
kosong
End Sub
10. Procedure OnClick pada Tombol Simpan:
Private Sub Command3_Click()
Dim rs As New ADODB.Recordset
rs.Open "Select * from jurusan where kd_jur='" & txtkd.Text & "'", MyDB, adOpenDynamic, adLockOptimistic
If rs.EOF Then
rs.AddNew
rs.Fields(0).Value = txtkd.Text
rs.Fields(1).Value = txtnm.Text
rs.Update
Else
rs.MoveFirst
rs.Fields(1).Value = txtnm.Text
rs.Update
End If
refresh_data
kosong
End Sub
11. Procedure OnClick pada Tombol Hapus:
Private Sub Command4_Click()
Dim i As Integer
i = MsgBox("Apakah Anda yakin data ini akan dihapus ?", vbYesNo, "Perhatian")
If i = 6 Then
MyDB.Execute ("delete from jurusan where kd_jur='" & txtkd.Text & "'")
End If
refresh_data
kosong
End Sub
12. Procedure OnClick pada Tombol Keluar :
Private Sub Command1_Click()
End
End Sub
13. Procedure DblClick milik DataGrid1
Private Sub DataGrid1_DblClick()
tampil
End Sub
tampil
End Sub
Jalankan program dengan cara tekan tombol F5
Berikut Source Selengkapnya :
Dim MyDB As New ADODB.Connection
Dim rs_jur As New ADODB.Recordset
Private Sub Command1_Click()
End
End Sub
Private Sub Command2_Click()
kosong
End Sub
Private Sub Command3_Click()
Dim rs As New ADODB.Recordset
rs.Open "Select * from jurusan where kd_jur='" & txtkd.Text & "'", MyDB, adOpenDynamic, adLockOptimistic
If rs.EOF Then
rs.AddNew
rs.Fields(0).Value = txtkd.Text
rs.Fields(1).Value = txtnm.Text
rs.Update
Else
rs.MoveFirst
rs.Fields(1).Value = txtnm.Text
rs.Update
End If
refresh_data
kosong
End Sub
Private Sub Command4_Click()
Dim i As Integer
i = MsgBox("Apakah Anda yakin data ini akan dihapus ?", vbYesNo, "Perhatian")
If i = 6 Then
MyDB.Execute ("delete from jurusan where kd_jur='" & txtkd.Text & "'")
End If
refresh_data
kosong
End Sub
Private Sub DataGrid1_DblClick()
tampil
End Sub
Private Sub Form_Activate()
refresh_data
End Sub
Private Sub Form_Load()
MyDB.Open "Driver={MySQL ODBC 5.1 Driver}; Server=localhost; Port=3306; Database=latihan; UID=root; PWD=ichwan"
If MyDB.State <= 0 Then
MsgBox "Connection ke MySQL Gagal...!"
Return
End If
End Sub
Sub kosong()
txtkd.Text = ""
txtnm.Text = ""
txtkd.SetFocus
End Sub
Sub tampil()
txtkd.Text = rs_jur!kd_jur
txtnm.Text = rs_jur!nm_jur
txtkd.SetFocus
End Sub
Sub refresh_data()
If rs_jur.State > 0 Then
rs_jur.Close
End If
rs_jur.CursorLocation = adUseClient
rs_jur.Open "select * from jurusan", MyDB, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs_jur
End Sub
Selamat Mencoba
Tidak ada komentar:
Posting Komentar