Anda di halaman 1dari 25

PEMOGRAMAN BERORIENTASI OBJEK 2

TUTORIAL PEMBUATAN PROGRAM INPUT DATA SEKOLAH


DENGAN MENGGUNAKAN BAHASA PEMROGRAMAN VISUAL
BASIC 6.0 DAN DATABASE MENGGUNAKAN MYSQL

Oleh :
Nama

Fauziah

NOBP

11101156110017

Kelas

TIK-1

JURUSAN PENDIDIKAN TEKNIK INFORMATIKA DAN KOMPUTER


FAKULTAS KEGURUAN ILMU PENDIDIKAN
UNIVERSITAS PUTRA INDONESIA YPTK PADANG
2013

TUTORIAL PEMBUATAN PROGRAM INPUT DATA SEKOLAH DENGAN


MENGGUNAKAN BAHASA PEMROGRAMAN VISUAL BASIC 6.0 DAN
DATABASE MENGGUNAKAN MYSQL

1. Pastikanlah terlebih dahulu VB 6.0 dan database mySql nya telah terinstall dengan baik.
2. Click start >> program >> Microsoft Visual Studio >> Microsoft Visual Basic 6.0

3. Pilih
4. Buatlah form seperti dibawah ini

Gambar Input data Guru

Created By Fauziah

Gambar Input data Mata Pelajaran

Gambar Input data Siswa

Created By Fauziah

Gambar Input Nilai Siswa

Gambar MDI form (cara membuat mdi form adalah dengan klik kanan pada form lalu
klik add >> MDI form).
5. Lalu tambahkan juga form module dengan klik kanan >> add >> module
6. Langkah selanjutnya buatlah database baru.
7. Buat 4 buah tabel baru dan isi fieldnya sesuaikan dengan kebutuhan

Created By Fauziah

Gambar tabel data siswa dengan nis sebagai primary key.

Gambar tabel data mata pelajaran dengan kode_mapel sebagai primary key.

Created By Fauziah

Gambar table data guru dengan kode_guru sebagai primary key.

Gambar tabel datanilaisiswa


8. Setelak dibuat semua tabel
9. langkah selanjutnya ambil di control panel>administrative tool>ODBC>ADD>MySQL
ODBC 3.51 Driver>finish
10. isi Data Source Name dengan nama "database" jika mengalami error karena minta id isi
user dengan root
11. ok
12. cara untuk menghubungkan vb dengan ODBC adalah pertama klik kanan Adodc >>
Created By Fauziah

ADODC Properties >> use connection string >> use data source name >> pilih data base
yg telah dibuat (dalam kasus ini menggunakan database db_barang) >> test connection
terlebih dahulu >> ok.
13. Masih pada properties pada Adodc pilih >> setelah dibuat seperti gambar dibawah tekan
ok

14. Lalu pada Datagrid1 ubah lah datasourcenya menjadi Adodc1 pada properties
15. Dan begitulah langkah selanjutnya yang dilakukan pada form-form yang lain, dan hanya
memerlukan perubahan nama table penghubung dengan database MySQL.
16. Dan langkah selanjutnya sesuiakan lah script berikut ini
Form Input data siswa
Private Sub cmd_cancel_Click()
kosong
Call tabel
End Sub
Private Sub cmd_clear_Click()
kosong
Call tabel
End Sub
Private Sub cmd_edit_Click()
conn.Execute "update data_siswa set nama='" & Text2.Text & "',jurusan='" &
Combo1.Text & "',kelas='" & Text3.Text & "' where nis='" & Text1.Text & "'"
MsgBox "Data berhasil di update..", vbOKOnly + vbInformation
kosong
Call tabel
DataGrid1.Refresh
Adodc1.Refresh

Created By Fauziah

End Sub
Private Sub cmd_hapus_Click()
If Text1.Text = "" Then
MsgBox "Error! No Record Has Been Selected", vbCritical, "No Record
Selected!"
Else
With rsdatasiswa
'Confirm the Delete procedure with the user
If MsgBox("Are You Sure You Wish To Delete Room ID " & Text1.Text & "'s
Record?", vbYesNo + vbQuestion, "Delete Record?") = vbYes Then
.Delete 'Delete the record from the database
'Display Success Message
MsgBox "The Record Has Been Deleted Successfully!", vbInformation,
"Successful Delete Procedure!"
Form_Load 'Calling the Form_Load Procedure
clearAllFields 'Calling a Private Function To Clear All Fields
Else
'Display 'Delete Procedure Cancelled' Message
MsgBox "The Delete Procedure Was Cancelled!", vbExclamation, "Delete
Procedure Cancelled!"
Form_Load 'Calling the Form_Load Procedure
clearAllFields 'Calling a Private Function To Clear All Fields
End If
.Requery

'Requerying the Table

End With
End If
End Sub
Public Function clearAllFields() 'This function will clear all fields on the interface

Created By Fauziah

On Error Resume Next


For Each eachField In Me.Controls 'Running a Loop through all the Controls
'The following If Condition will clear all TextBoxes
If TypeOf eachField Is TextBox Then
eachField.Text = ""
End If
Next
End Function

Private Sub cmd_keluar_Click()


Unload Me
End Sub
Private Sub cmd_simpan_Click()
conn.Execute ("insert into data_siswa values('" & Text1.Text & "','" & Text2.Text &
"','" & Combo1.Text & "','" & Text3.Text & "')")
MsgBox "Data berhasil di simpan..", vbOKOnly + vbInformation
kosong
Call tabel
DataGrid1.Refresh
Adodc1.Refresh
End Sub
Private Sub DataGrid1_Click()
With rsdatasiswa

'
'
'
'
'

'Entering the values in the particular record into the fields on the interface
Text1.Text = .Fields(0).Value
Text2.Text = .Fields(1).Value
Combo1.Text = .Fields(2).Value
Text3.Text = .Fields(3).Value
' texttelpsupplier.Text = .Fields(3).Value
txtWardID.Text = .Fields(4).Value
txtWardNumber.Text = .Fields(5).Value
txtRoomCost.Text = .Fields(6).Value
txtAdditionalNotes.Text = .Fields(7).Value
End With
enableAllFields

End Sub
Private Sub Form_Load()
Combo1.AddItem ("IPA")

Created By Fauziah

Combo1.AddItem ("IPS")
Combo1.AddItem ("BAHASA")
If conn.State = adStateClosed Then
main
End If
Call tabel
Call datasiswa_Maintenance
Set DataGrid1.DataSource = rsdatasiswa
DataGrid1.Enabled = True
End Sub
Sub kosong()
Text1.Text = ""
Text2.Text = ""
Combo1.Text = ""
Text3.Text = ""
Text1.SetFocus
End Sub
Private Sub tabel()
Set rec = New ADODB.Recordset
rec.CursorLocation = adUseClient
rec.Open "select * from data_siswa", conn, adOpenForwardOnly, adLockReadOnly
Set DataGrid1.DataSource = rec
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If rec.State Then rec.Close
rec.Open "select * from data_siswa where nis='" & Text1.Text & "'", conn,
adOpenForwardOnly, adLockReadOnly
If Not rec.EOF Then
Text2.Text = rec("nama")
Combo1.Text = rec("jurusan")
Text3.Text = rec("kelas")
Call tabel
MsgBox "data sudah ada", vbInformation + vbOKOnly
Else
Text2.Text = ""
Combo1.Text = ""
Text3.Text = ""
Text2.SetFocus
Call tabel
End If
rec.Close
End If
End Sub

Created By Fauziah

Form Input data Mata Pelajaran


Private Sub Command1_Click()
conn.Execute ("insert into data_mapel values('" & Text1.Text & "','" & Text2.Text &
"','" & Text3.Text & "','" & Text4.Text & "')")
MsgBox "Data berhasil di simpan..", vbOKOnly + vbInformation
kosong
Call tabel
DataGrid1.Refresh
Adodc1.Refresh
End Sub
Private Sub Command2_Click()
conn.Execute "update data_mapel set kode_guru='" & Text1.Text & "',nama_mapel='"
& Text3.Text & "'where nis='" & Text2.Text & "'"
MsgBox "Data berhasil di update..", vbOKOnly + vbInformation
kosong
Call tabel
DataGrid1.Refresh
Adodc1.Refresh
End Sub
Private Sub Command3_Click()
If Text2.Text = "" Then
MsgBox "Error! No Record Has Been Selected", vbCritical, "No Record
Selected!"
Else
With rsdatamapel
'Confirm the Delete procedure with the user
If MsgBox("Are You Sure You Wish To Delete Room ID " & Text2.Text & "'s
Record?", vbYesNo + vbQuestion, "Delete Record?") = vbYes Then
.Delete 'Delete the record from the database
'Display Success Message
MsgBox "The Record Has Been Deleted Successfully!", vbInformation,
"Successful Delete Procedure!"
Form_Load 'Calling the Form_Load Procedure
clearAllFields 'Calling a Private Function To Clear All Fields
Else

Created By Fauziah

'Display 'Delete Procedure Cancelled' Message


MsgBox "The Delete Procedure Was Cancelled!", vbExclamation, "Delete
Procedure Cancelled!"
Form_Load 'Calling the Form_Load Procedure
clearAllFields 'Calling a Private Function To Clear All Fields
End If
.Requery

'Requerying the Table

End With
End If
End Sub
Public Function clearAllFields() 'This function will clear all fields on the interface
On Error Resume Next
For Each eachField In Me.Controls 'Running a Loop through all the Controls
'The following If Condition will clear all TextBoxes
If TypeOf eachField Is TextBox Then
eachField.Text = ""
End If
Next
End Function
Private Sub Command4_Click()
kosong
Call tabel
End Sub
Private Sub Command5_Click()
Unload Me
End Sub
Private Sub DataGrid1_Click()
With rsdatamapel
'Entering the values in the particular record into the fields on the interface
Text1.Text = .Fields(0).Value
Text2.Text = .Fields(1).Value
Text3.Text = .Fields(2).Value

Created By Fauziah

'
'
'
'

Text4.Text = .Fields(3).Value
' texttelpsupplier.Text = .Fields(3).Value
txtWardID.Text = .Fields(4).Value
txtWardNumber.Text = .Fields(5).Value
txtRoomCost.Text = .Fields(6).Value
txtAdditionalNotes.Text = .Fields(7).Value

End With
' enableAllFields
End Sub
Private Sub Form_Load()
If conn.State = adStateClosed Then
main
End If
Call tabel
Call datamapel_Maintenance
Set DataGrid1.DataSource = rsdatamapel
DataGrid1.Enabled = True
End Sub
Sub kosong()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.SetFocus
End Sub
Private Sub tabel()
Set rec = New ADODB.Recordset
rec.CursorLocation = adUseClient
rec.Open "select * from data_mapel", conn, adOpenForwardOnly, adLockReadOnly
Set DataGrid1.DataSource = rec
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If rec.State Then rec.Close
rec.Open "select * from data_mapel where kode_mapel='" & Text2.Text & "'", conn,
adOpenForwardOnly, adLockReadOnly
If Not rec.EOF Then
Text1.Text = rec("kode_guru")
Text3.Text = rec("nama_mapel")
Text4.Text = rec("sks")
Call tabel
MsgBox "data sudah ada", vbInformation + vbOKOnly
Else

Created By Fauziah

Text1.Text = ""
Text3.Text = ""
Text4.Text = ""
Text1.SetFocus
Call tabel
End If
rec.Close
End If
End Sub
Form input data guru
Private Sub Command1_Click()
conn.Execute ("insert into data_guru values('" & txt_kdguru.Text & "','" &
txt_nmguru.Text & "')")
MsgBox "Data berhasil di simpan..", vbOKOnly + vbInformation
kosong
Call tabel
DataGrid1.Refresh
Adodc1.Refresh
End Sub
Private Sub Command2_Click()
conn.Execute "update data_guru set nama_guru='" & txt_nmguru & "' where
kode_guru='" & txt_kdguru & "'"
MsgBox "Data berhasil di update..", vbOKOnly + vbInformation
kosong
Call tabel
DataGrid1.Refresh
Adodc1.Refresh
End Sub
Private Sub Command3_Click()
'conn.Execute "delete from data_guru where kode_guru='" & txt_kdguru & "'"
'MsgBox "Data berhasil di hapus..", vbOKOnly + vbInformation
'kosong
'Call tabel
'If txt_kdguru = "" Then
'MsgBox ("data tidak lengkap,gagal menghapus data...!")
'Exit Sub
'End If
'Dim x As String
'x = MsgBox("Ingin Hapus data ini? [Y/N]", vbYesNo + vbQuestion)
'If x = vbYes Then
' conn.Execute ("delete from data_guru where kode_guru='" & Trim(txt_kdguru) &
"'")
Created By Fauziah

' MsgBox ("Data berhasil dihapus")


'End If
'kosong
'Call tabel
If txt_kdguru.Text = "" Then
MsgBox "Error! No Record Has Been Selected", vbCritical, "No Record
Selected!"
Else
With rsdataguru
'Confirm the Delete procedure with the user
If MsgBox("Are You Sure You Wish To Delete Room ID " & txt_kdguru.Text
& "'s Record?", vbYesNo + vbQuestion, "Delete Record?") = vbYes Then
.Delete 'Delete the record from the database
'Display Success Message
MsgBox "The Record Has Been Deleted Successfully!", vbInformation,
"Successful Delete Procedure!"
Form_Load 'Calling the Form_Load Procedure
clearAllFields 'Calling a Private Function To Clear All Fields
Else
'Display 'Delete Procedure Cancelled' Message
MsgBox "The Delete Procedure Was Cancelled!", vbExclamation, "Delete
Procedure Cancelled!"
Form_Load 'Calling the Form_Load Procedure
clearAllFields 'Calling a Private Function To Clear All Fields
End If
.Requery

'Requerying the Table

End With
End If
End Sub
Public Function clearAllFields() 'This function will clear all fields on the interface

Created By Fauziah

On Error Resume Next


For Each eachField In Me.Controls 'Running a Loop through all the Controls
'The following If Condition will clear all TextBoxes
If TypeOf eachField Is TextBox Then
eachField.Text = ""
End If
Next
End Function
Private Sub Command4_Click()
kosong
Call tabel
End Sub
Private Sub Command5_Click()
Unload Me
End Sub
Sub kosong()
txt_kdguru = ""
txt_nmguru = ""
txt_kdguru.SetFocus
End Sub
Private Sub DataGrid1_Click()
With rsdataguru
'Entering the values in the particular record into the fields on the interface
txt_kdguru.Text = .Fields(0).Value
txt_nmguru.Text = .Fields(1).Value
'
'
'
'
'

' texttelpsupplier.Text = .Fields(3).Value


txtWardID.Text = .Fields(4).Value
txtWardNumber.Text = .Fields(5).Value
txtRoomCost.Text = .Fields(6).Value
txtAdditionalNotes.Text = .Fields(7).Value
End With
enableAllFields

End Sub
Private Sub Form_Load()
If conn.State = adStateClosed Then
main
End If

Created By Fauziah

Call tabel
Call dataguru_Maintenance
Set DataGrid1.DataSource = rsdataguru
DataGrid1.Enabled = True
End Sub
Private Sub txt_kdguru_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If rec.State Then rec.Close
rec.Open "select * from data_guru where kode_guru='" & txt_kdguru & "'", conn,
adOpenForwardOnly, adLockReadOnly
If Not rec.EOF Then
txt_nmguru = rec("nama_guru")
Call tabel
MsgBox "data sudah ada", vbInformation + vbOKOnly
Else
txt_nmguru = ""
txt_nmguru.SetFocus
Call tabel
End If
rec.Close
End If
End Sub
Private Sub tabel()
Set rec = New ADODB.Recordset
rec.CursorLocation = adUseClient
rec.Open "select * from data_guru", conn, adOpenForwardOnly, adLockReadOnly
Set DataGrid1.DataSource = rec
End Sub
Script untuk MDI Form
Private Sub dataguru_Click()
entry_dataguru.Show
End Sub
Private Sub datamapel_Click()
entry_datamapel.Show
End Sub
Private Sub datasiswa_Click()
entry_datasiswa.Show
End Sub
Private Sub edit_Click()

Created By Fauziah

End Sub
Private Sub exit_Click()
Unload Me
End Sub
Private Sub nilaisiswa_Click()
entry_nilaisiswa.Show
End Sub
Note: untuk memfullscreen kan ubah windowstate menjadi maximized
Script pada module1
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public rsnilai As ADODB.Recordset
Public rsdatasiswa As ADODB.Recordset
Public rsdatamapel As ADODB.Recordset
Public rsdataguru As ADODB.Recordset

Sub main()
'The Purpose of this function is to open a connection to link the database
Set conn = New ADODB.Connection
'Provider=MSDASQL.1;Password=root;Persist Security Info=True;User
ID=root;Data Source=konekpersediaan_aj
conn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User
ID=root;Data Source=db_barang"

'conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _


& App.Path & "\sdp.mdb;Persist Security Info=False"
conn.Open
MDIForm1.Show

End Sub
Public Sub nilai_Maintenance()
'The Purpose of this function is to manage the recordset "Rooms_Maintenance"

Created By Fauziah

Set rsnilai = New ADODB.Recordset


With rsnilai
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.ActiveConnection = conn
.Source = "Select * from datanilaisiswa"
.CursorLocation = adUseClient
.Open
End With
End Sub
Public Sub datasiswa_Maintenance()
'The Purpose of this function is to manage the recordset "Rooms_Maintenance"
Set rsdatasiswa = New ADODB.Recordset
With rsdatasiswa
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.ActiveConnection = conn
.Source = "select * from data_siswa"
.CursorLocation = adUseClient
.Open
End With
End Sub
Public Sub datamapel_Maintenance()
'The Purpose of this function is to manage the recordset "Rooms_Maintenance"
Set rsdatamapel = New ADODB.Recordset
With rsdatamapel
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.ActiveConnection = conn
.Source = "select * from data_mapel"
.CursorLocation = adUseClient
.Open
End With
End Sub
Public Sub dataguru_Maintenance()

Created By Fauziah

'The Purpose of this function is to manage the recordset "Rooms_Maintenance"


Set rsdataguru = New ADODB.Recordset
With rsdataguru
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.ActiveConnection = conn
.Source = "select * from data_guru"
.CursorLocation = adUseClient
.Open
End With
End Sub

Tampilan awal program

Created By Fauziah

Tampilan input data siswa saat dijalankan

Gambar dalam melakukan edit data

Created By Fauziah

Gambar dalam melakukan hapus data


Sekian terima kasih hanya itu saja yang bisa ditampilkan. Jika ada langkahlangkah yang tertinggal dalam pembuatan tutorial ini mohon dimaafkan.

Created By Fauziah

Private Sub Command1_Click()


db.Execute "insert into biodata values ('" & Text1.Text & "','" & Text2.Text & "','" &
Text3.Text & "','" & Combo1.Text & "','" & Text4.Text & "','" & Text5.Text & "','" &
Text6.Text & "','" & Combo3.Text & "','" & Combo4.Text & "','" & Combo2.Text & "','" &
Format(tgl, "yyyy-MM-dd") & "')"
MsgBox "Data Sukses Disimpan", vbOKOnly, "Keterangan"
Text1.SetFocus
table
bersih
End Sub
Private Sub Command2_Click()
db.Execute ("update biodata set nama='" & Text2.Text & "',tetala='" & Text3.Text &
"',alamat='" & Text4.Text & "',ayah='" & Text5.Text & "',ibu='" & Text6.Text & "',ka='" &
Combo3.Text & "',ki='" & Combo4.Text & "',agama='" & Combo2.Text & "',tanggal='" &
Format(tgl, "yyyy-MM-dd") & "'")
MsgBox "Data Sukses Diedit", vbOKOnly, "keterangan"
Text1.SetFocus
table
bersih
End Sub
Private Sub Command3_Click()
bersih
Text1.SetFocus
Command1.Enabled = True
End Sub

Created By Fauziah

Private Sub Command4_Click()


Unload Me
End Sub
Private Sub DataGrid1_Click()
Set rs = New Recordset
rs.Open "select*from biodata where nis='" & DataGrid1.Text & "'", db, adOpenStatic,
adLockOptimistic
If Not rs.EOF Then
Text1.Text = rs.Fields("nis")
Text2.Text = rs.Fields("nama")
Text3.Text = rs.Fields("tetala")
Combo1.Text = rs.Fields("jk")
Text4.Text = rs.Fields("alamat")
Text5.Text = rs.Fields("ayah")
Text6.Text = rs.Fields("ibu")
Combo4.Text = rs.Fields("ka")
Combo3.Text = rs.Fields("ki")
Combo2.Text = rs.Fields("agama")
Date = rs.Fields("tanggal")
Command1.Enabled = False
Else
Text1.Text = ""
End If
End Sub
Private Sub Form_Load()
table
Call main
End Sub
Sub table()
Set rs = New Recordset
rs.Open "select * from biodata", db, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs
DataGrid1.Refresh
End Sub
Sub bersih()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Combo1.Text = "--pilih--"
Combo2.Text = "--pilih--"
Combo3.Text = "--pilih--"
Combo4.Text = "--pilih--"
End Sub

Created By Fauziah

Script di module
Global db As Connection
Global rs As Recordset
Sub main()
Set db = New Connection
db.CursorLocation = adUseClient
db.Open "fauziah"
Form1.Show
End Sub

Created By Fauziah

Anda mungkin juga menyukai