Sunday, April 21, 2013

Contoh Program Absensi Sederhana

pertama kita buat dulu database untuk absensi nya dengan nama latihan.mdb, kemudian isi field-field databasenya seperti ini :

Nama Database : latihan.mdb
Nama Tabel       : Absen


Field
Data Type
Size
NRP
Text
10
Nama
Text
35
Jurusan
Text
50
Matkul
Text
50
Masuk
Byte
Sakit
Byte
Izin
Byte
Alpa
Byte
Total
Byte

Setelah membuat database selesai, lalu buat desain form absen seperti ini :
Componen tambahan pada Toolbox :
Adodc dan DataGrid

 

 Jika sudah, kita buat module baru, Klik Menu Project > Add Module > Open,,Kemudian akan muncul jendela kode, tuliskan kode perintah berikut :


  1. Public ConN As New ADODB.Connection
  2. Public RsAbsen As New ADODB.Recordset
  3. Sub koneksi()
  4. Set ConN = New ADODB.Connection
  5. Set RsAbsen = New ADODB.Recordset
  6. ConN.Open "Provider=microsoft.jet.oledb.4.0;data source = "  
  7. & App.Path & "\latihan.mdb"
  8. End Sub

Kemudian Save Module yang sudah kita isi dengan kode perintah diatas,,
Setelah itu isikan kode berikut pada Form
  1. Private Sub Form_Activate()
  2. Call koneksi
  3. Adodc1.ConnectionString = "provider=microsoft.jet.oledb.4.0;data 
  4. source=" & App.Path & "\latihan.mdb"
  5. Adodc1.RecordSource = "absen"
  6. Adodc1.Refresh
  7. Set DataGrid1.DataSource = Adodc1
  8. DataGrid1.Refresh
  9. End Sub
  10. Private Sub Form_Load()
  11. Text1.Text = ""
  12. Text2.Text = ""
  13. Combo1.Text = ""
  14. Combo2.Text = ""
  15. Combo3.Text = ""
  16. Label11.Caption = 0
  17. Label12.Caption = 0
  18. Label13.Caption = 0
  19. Label14.Caption = 0
  20. Label15.Caption = 0
  21. Combo1.AddItem "Sistem Informasi"
  22. Combo1.AddItem "Teknik Informatika"
  23. Combo1.AddItem "Manajemen Informatika"
  24. Combo1.AddItem "Komputer Akuntansi"
  25. Combo2.AddItem "Pemprograman Visual I"
  26. Combo2.AddItem "Pemprograman Visual II"
  27. Combo3.AddItem "Hadir"
  28. Combo3.AddItem "Tidak Hadir"
  29. Option1.Value = False
  30. Option2.Value = False
  31. Option3.Value = False
  32. Option1.Enabled = False
  33. Option2.Enabled = False
  34. Option3.Enabled = False
  35. Text1.Enabled = False
  36. Text2.Enabled = False
  37. Combo1.Enabled = False
  38. Combo2.Enabled = False
  39. Combo3.Enabled = False
  40. End Sub

Kemudian buat function cari data, sub tampilkan data dan kosong seperti dibawah ini :

  1. Function CariData()
  2. Call koneksi
  3. RsAbsen.Open "Select * From absen where nrp='" & Text1 & "'", ConN
  4. End Function
  5. Private Sub TampilkanData()
  6. Text2 = RsAbsen!nama
  7. Combo1.Text = RsAbsen!jurusan
  8. Combo2.Text = RsAbsen!matkul
  9. Label11.Caption = RsAbsen!masuk
  10. Label12.Caption = RsAbsen!sakit
  11. Label13.Caption = RsAbsen!izin
  12. Label14.Caption = RsAbsen!alpa
  13. Label15.Caption = RsAbsen!total
  14. End Sub
  15. Private Sub kosong()
  16. Text1.Text = ""
  17. Text2.Text = ""
  18. Combo1.Text = ""
  19. Combo2.Text = ""
  20. Combo3.Text = ""
  21. Label11.Caption = 0
  22. Label12.Caption = 0
  23. Label13.Caption = 0
  24. Label14.Caption = 0
  25. Label15.Caption = 0
  26. Option1.Value = False
  27. Option2.Value = False
  28. Option3.Value = False
  29. Option1.Enabled = False
  30. Option2.Enabled = False
  31. Option3.Enabled = False
  32. End Sub

Untuk pencarian data, masukkan kode perintah berikut pada Text1 dengan procedure Lostfocus :

  1. Private Sub Text1_LostFocus()
  2. On Error Resume Next
  3. Call CariData
  4. If Not RsAbsen.EOF Then
  5. TampilkanData
  6. Text1.Enabled = False
  7. Text2.Enabled = False
  8. Combo1.Enabled = False
  9. Combo2.Enabled = False
  10. MsgBox "NRP Sudah Ada"
  11. Command1.Enabled = False
  12. End If
  13. End Sub

Pada Combo3 atau combo untuk kehadiran, isikan kode berikut untk proses perhitungan absen masuk :
  1. Private Sub Combo3_Click()
  2. If Combo3.Text = "Hadir" Then
  3. Option1.Enabled = False
  4. Option2.Enabled = False
  5. Option3.Enabled = False
  6. Label11.Caption = Val(Label11.Caption) + 1
  7. Else
  8. Option1.Enabled = True
  9. Option2.Enabled = True
  10. Option3.Enabled = True
  11. End If
  12. End Sub


Kemudain Pada Option 1, 2 dan 3, isikan kode perintah untuk menghitung absen alasan tidak hadir dan total tidak hadir :
  1. Private Sub Option1_Click()
  2. If Option1.Value = True Then
  3. Label12.Caption = Val(Label12.Caption) + 1
  4. Else
  5. Label12.Caption = Val(Label12.Caption) + 0
  6. End If
  7. Label15.Caption = Val(Label12.Caption) + Val(Label13.Caption) 
  8. + Val(Label14.Caption)
  9. End Sub
  10. Private Sub Option2_Click()
  11. If Option2.Value = True Then
  12. Label13.Caption = Val(Label13.Caption) + 1
  13. Else
  14. Label13.Caption = Val(Label13.Caption) + 0
  15. End If
  16. Label15.Caption = Val(Label12.Caption) + Val(Label13.Caption) 
  17. + Val(Label14.Caption)
  18. End Sub
  19. Private Sub Option3_Click()
  20. If Option3.Value = True Then
  21. Label14.Caption = Val(Label14.Caption) + 1
  22. Else
  23. Label14.Caption = Val(Label14.Caption) + 0
  24. End If
  25. Label15.Caption = Val(Label12.Caption) + Val(Label13.Caption) 
  26. + Val(Label14.Caption)
  27. End Sub



Terakhir isikan kode perintah untuk command simpan, edit, absen, hapus, isi data dan selesai :

  1. Private Sub Command1_Click() 'Kode Perintah Untuk tombol Simpan
  2. Dim SQLSimpan As String
  3. SQLSimpan = "Insert Into absen (nrp,nama,jurusan,matkul)
  4.  values ('" & Text1 & "','" & Text2 & "','" & Combo1.Text & "','" & 
  5. Combo2.Text & "')"
  6. ConN.Execute SQLSimpan
  7. Form_Activate
  8. Call kosong
  9. End Sub
  10. Private Sub Command2_Click() ' Kode Perintah Untuk tombol Edit
  11. If Command2.Caption = "Edit Data" Then
  12. Text1.Enabled = False
  13. Text2.Enabled = True
  14. Combo1.Enabled = True
  15. Combo2.Enabled = True
  16. Combo3.Enabled = True
  17. Command2.Caption = "Edit"
  18. Else
  19. Dim SQLAbsen As String
  20. SQLAbsen = "Update absen Set nama='" & Text2.Text
  21.  & "'," & " matkul='" & Combo2.Text & "'," & " jurusan='" 
  22.  & Combo1.Text & "' where nrp='" & Text1 & "'"
  23. ConN.Execute SQLAbsen
  24. Form_Activate
  25. Call kosong
  26. Command2.Caption = "Edit Data"
  27. End If
  28. End Sub
  29. Private Sub Command3_Click() 'Kode Perintah Untuk Tombol Absen
  30. Dim SQLAbsen As String
  31. SQLAbsen = "Update absen Set masuk= '" & Label11.Caption 
  32. & "'," & " sakit='" & Label12.Caption & "'," & "
  33.  izin='" & Label13.Caption & "'," & " alpa='" &
  34.  Label14.Caption & "'," & " total='" & 
  35. Label15.Caption & "' where nrp='" & Text1 & "'"
  36. ConN.Execute SQLAbsen
  37. Form_Activate
  38. Call kosong
  39. End Sub
  40. Private Sub Command4_Click() 'KOde Perintah Untuk Tombol Hapus
  41. Adodc1.Recordset.Delete
  42. Adodc1.Recordset.Update
  43. DataGrid1.Refresh
  44. End Sub
  45. Private Sub Command5_Click() 'Kode Perintah Untuk Tombol Selesai
  46. pesan = MsgBox("Anda Yakin Ingin Keluar Dari Program ini?", 
  47. vbQuestion + vbYesNo, "Keluar")
  48. If pesan = vbYes Then
  49. Unload Me
  50. Else
  51. End If
  52. End Sub
  53. Private Sub Command6_Click() 'Kode Perintah Untuk Tombol Isi Data
  54. Text1.Enabled = True
  55. Text2.Enabled = True
  56. Combo1.Enabled = True
  57. Combo2.Enabled = True
  58. Combo3.Enabled = True
  59. Text1.SetFocus
  60. End Sub

Source : http://masvisualbasic.blogspot.com


No comments:

Post a Comment

Kebahagiaan sejati bukanlah pada saat kita berhasil meraih apa yg kita perjuangkan, melainkan bagaimana kesuksesan kita itu memberi arti atau membahagiakan orang lain.