Melanjutkan materi sebelumnya, dalam membuat aplikasi berikut kita akan bekerja dengan lebih dari satu form. Untuk lebih dapat mengefisienkan perintah program, bisa dalam projek ditambahkan sebuah modul.
Menambahkan form pada projek STMIK ANJANI
Tambahkan Modul dalam Projek STMIK ANJANI , STMIK ANJANI
Dalam Jendela Kode Modul Ketikkan Kode berikut : STMIK ANJANI STMIK ANJANI
Option Explicit STMIK ANJANI STMIK ANJANI
Public Cn As New ADODB.Connection STMIK ANJANI
Public Rs As New ADODB.Recordset STMIK ANJANI
Public Const Dbasefile = "dbnilai.mdb" STMIK ANJANI
Public Sub buka()
If Cn.State = adStateOpen Then Cn.Close STMIK ANJANI
Cn.Mode = adModeReadWrite STMIK ANJANI
Cn.Open "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + App.Path + "\" + Dbasefile STMIK ANJANI
End Sub STMIK ANJANI
Public Sub sqlQuery(SQLstr As String)STMIK ANJANI
Cn.Execute SQLstr STMIK ANJANI
End Sub STMIK ANJANI
Public Sub OpenRecordset(SQLstr As String)
If Rs.State = adStateOpen Then Rs.Close
Rs.Open SQLstr, Cn, adOpenKeyset, adLockOptimistic, adCmdText
End Sub STMIK ANJANI
Public Sub tutup()STMIK ANJANI
If Rs.State = adStateOpen Then Rs.Close STMIK ANJANI
Set Rs.ActiveConnection = Nothing STMIK ANJANI
Set Rs = Nothing STMIK ANJANI
If Cn.State = adStateOpen Then Cn.Close
Set Cn = Nothing STMIK ANJANI
End Sub STMIK ANJANI
Desain Form1 sesuai gambar berikut
Dobleklik pada form1 kemudian ketik kode program
Tetapi sebelumnya ketik kode program berikut pada jendela kode editor
STMIK ANJANI STMIK ANJANI STMIK ANJANI STMIK ANJANI
Private Sub kosong()STMIK ANJANI
Text1.Text = "" STMIK ANJANI
Text2.Text = "" STMIK ANJANI
Text3.Text = "" STMIK ANJANI
End Sub
Dobelklik pada command cari ketikkan kode program STMIK ANJANI
Private Sub Command8_Click()STMIK ANJANI
OpenRecordset "Select * from nilai where id_nilai = " & Text1.Text & ";"
If Not Rs.EOF Then
Text2.Text = Rs.Fields!nama STMIK ANJANI
Text3.Text = Rs.Fields!nilai
Else STMIK ANJANI
MsgBox "Maaf data tidak ada!", vbInformation, "Perhatian!"
End If STMIK ANJANI
End Sub STMIK ANJANI
Dobelklik pada command Edit ketikkan kode program
Private Sub Command5_Click()STMIK ANJANI
sqlQuery "Update nilai set nama ='" & Text2.Text & "', nilai = " & Text3.Text & " where id_nilai = " & Text1.Text & ";"
kosong STMIK ANJANI
End Sub STMIK ANJANI
Dobelklik pada command Tambah ketikkan kode program STMIK ANJANI
Private Sub Command6_Click()STMIK ANJANI
sqlQuery "INSERT INTO nilai values ('" & Text1.Text & "' ,'" & Text2.Text & "','" & Text3.Text & "');"
kosong STMIK ANJANI
End Sub STMIK ANJANI
Dobelklik pada command Hapus ketikkan kode program
Private Sub Command7_Click()STMIK ANJANI
sqlQuery "Delete from nilai where id_nilai = " & Text1.Text & ";"
kosong
End Sub
Sedangkan untuk command laporan
Pada Form 2 tambahkan tool komponen Listview namun sebelumnya tambahkan komponen tersebut dengan menekan Ctrl+t, pilih Microsoft Windows Comman Control 6.0 STMIK ANJANI
Sehingga pada tool box terdapat penambahan beberapa tool, STMIK ANJANI
Tambahkan tool Listview pada form2 STMIK ANJANI
Sehingga pada form seperti pada gambar STMIK ANJANI
Ketikkan kode program berikut STMIK ANJANI
Private Sub Command1_Click()STMIK ANJANI
Unload Me STMIK ANJANI
End Sub STMIK ANJANI
Private Sub Form_Load()STMIK ANJANI
Dim ch As ColumnHeader, lv As ListItem STMIK ANJANI
ListView1.View = lvwReport STMIK ANJANI
Set ch = ListView1.ColumnHeaders.Add(, , "No", 500)
Set ch = ListView1.ColumnHeaders.Add(, , "Nama", 2200)
Set ch = ListView1.ColumnHeaders.Add(, , "Nilai", 1500)
ListView1.GridLines = True
ListView1.Enabled = True
OpenRecordset "Select * from nilai;" STMIK ANJANI
If Not Rs.EOF Then STMIK ANJANI
Do Until Rs.EOF STMIK ANJANI
Set lv = ListView1.ListItems.Add(, , Rs.Fields!id_nilai)
lv.SubItems(1) = Rs.Fields!nama STMIK ANJANI
lv.SubItems(2) = Rs.Fields!nilai STMIK ANJANI
Rs.MoveNext STMIK ANJANI
Loop STMIK ANJANI
End If STMIK ANJANI
End Sub STMIK ANJANI
Jalankan Program (
Menambahkan form pada projek STMIK ANJANI
Tambahkan Modul dalam Projek STMIK ANJANI , STMIK ANJANI
Dalam Jendela Kode Modul Ketikkan Kode berikut : STMIK ANJANI STMIK ANJANI
Option Explicit STMIK ANJANI STMIK ANJANI
Public Cn As New ADODB.Connection STMIK ANJANI
Public Rs As New ADODB.Recordset STMIK ANJANI
Public Const Dbasefile = "dbnilai.mdb" STMIK ANJANI
Public Sub buka()
If Cn.State = adStateOpen Then Cn.Close STMIK ANJANI
Cn.Mode = adModeReadWrite STMIK ANJANI
Cn.Open "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + App.Path + "\" + Dbasefile STMIK ANJANI
End Sub STMIK ANJANI
Public Sub sqlQuery(SQLstr As String)STMIK ANJANI
Cn.Execute SQLstr STMIK ANJANI
End Sub STMIK ANJANI
Public Sub OpenRecordset(SQLstr As String)
If Rs.State = adStateOpen Then Rs.Close
Rs.Open SQLstr, Cn, adOpenKeyset, adLockOptimistic, adCmdText
End Sub STMIK ANJANI
Public Sub tutup()STMIK ANJANI
If Rs.State = adStateOpen Then Rs.Close STMIK ANJANI
Set Rs.ActiveConnection = Nothing STMIK ANJANI
Set Rs = Nothing STMIK ANJANI
If Cn.State = adStateOpen Then Cn.Close
Set Cn = Nothing STMIK ANJANI
End Sub STMIK ANJANI
Desain Form1 sesuai gambar berikut
Dobleklik pada form1 kemudian ketik kode program
Private Sub Form_Load()STMIK ANJANI
buka STMIK ANJANI
kosong STMIK ANJANI
End Sub STMIK ANJANI
buka STMIK ANJANI
kosong STMIK ANJANI
End Sub STMIK ANJANI
Tetapi sebelumnya ketik kode program berikut pada jendela kode editor
STMIK ANJANI STMIK ANJANI STMIK ANJANI STMIK ANJANI
Private Sub kosong()STMIK ANJANI
Text1.Text = "" STMIK ANJANI
Text2.Text = "" STMIK ANJANI
Text3.Text = "" STMIK ANJANI
End Sub
Dobelklik pada command cari ketikkan kode program STMIK ANJANI
Private Sub Command8_Click()STMIK ANJANI
OpenRecordset "Select * from nilai where id_nilai = " & Text1.Text & ";"
If Not Rs.EOF Then
Text2.Text = Rs.Fields!nama STMIK ANJANI
Text3.Text = Rs.Fields!nilai
Else STMIK ANJANI
MsgBox "Maaf data tidak ada!", vbInformation, "Perhatian!"
End If STMIK ANJANI
End Sub STMIK ANJANI
Dobelklik pada command Edit ketikkan kode program
Private Sub Command5_Click()STMIK ANJANI
sqlQuery "Update nilai set nama ='" & Text2.Text & "', nilai = " & Text3.Text & " where id_nilai = " & Text1.Text & ";"
kosong STMIK ANJANI
End Sub STMIK ANJANI
Dobelklik pada command Tambah ketikkan kode program STMIK ANJANI
Private Sub Command6_Click()STMIK ANJANI
sqlQuery "INSERT INTO nilai values ('" & Text1.Text & "' ,'" & Text2.Text & "','" & Text3.Text & "');"
kosong STMIK ANJANI
End Sub STMIK ANJANI
Dobelklik pada command Hapus ketikkan kode program
Private Sub Command7_Click()STMIK ANJANI
sqlQuery "Delete from nilai where id_nilai = " & Text1.Text & ";"
kosong
End Sub
Sedangkan untuk command laporan
Private Sub Command1_Click()STMIK ANJANI
Form2.Show STMIK ANJANI
End Sub STMIK ANJANI
Form2.Show STMIK ANJANI
End Sub STMIK ANJANI
Pada Form 2 tambahkan tool komponen Listview namun sebelumnya tambahkan komponen tersebut dengan menekan Ctrl+t, pilih Microsoft Windows Comman Control 6.0 STMIK ANJANI
Sehingga pada tool box terdapat penambahan beberapa tool, STMIK ANJANI
Tambahkan tool Listview pada form2 STMIK ANJANI
Sehingga pada form seperti pada gambar STMIK ANJANI
Ketikkan kode program berikut STMIK ANJANI
Private Sub Command1_Click()STMIK ANJANI
Unload Me STMIK ANJANI
End Sub STMIK ANJANI
Private Sub Form_Load()STMIK ANJANI
Dim ch As ColumnHeader, lv As ListItem STMIK ANJANI
ListView1.View = lvwReport STMIK ANJANI
Set ch = ListView1.ColumnHeaders.Add(, , "No", 500)
Set ch = ListView1.ColumnHeaders.Add(, , "Nama", 2200)
Set ch = ListView1.ColumnHeaders.Add(, , "Nilai", 1500)
ListView1.GridLines = True
ListView1.Enabled = True
OpenRecordset "Select * from nilai;" STMIK ANJANI
If Not Rs.EOF Then STMIK ANJANI
Do Until Rs.EOF STMIK ANJANI
Set lv = ListView1.ListItems.Add(, , Rs.Fields!id_nilai)
lv.SubItems(1) = Rs.Fields!nama STMIK ANJANI
lv.SubItems(2) = Rs.Fields!nilai STMIK ANJANI
Rs.MoveNext STMIK ANJANI
Loop STMIK ANJANI
End If STMIK ANJANI
End Sub STMIK ANJANI
Jalankan Program (
{ 0 komentar... Views All / Send Comment! }
Posting Komentar