Memanfaatkan objek option button, combo box, dbgrid dan objek data untuk mengelola data petugas dalam sebuah aplikasi.
Untuk persiapan buatlah Database dengan nama file dbAplikasi
Buat tabel dengan nama tblPetugas dengan struktur seperti berikut :
Field Name Type Size Keterangan
KdTgs Text 5 Kode Petugas
NmTgs Text 35 Nama Petugas
TpLhr Text 25 Tempat Lahir Petugas
TgLhr Date/Time 8 Tanggal Lahir Petugas
KdJek Text 1 Kode Jenis Kelamin Petugas
Agama Text 10 Agama Petugas
Adres Text 25 Alamat Petugas
Atur/tetapkan file index seperti berikut :
Index Name KeyField Properti Index
IdxKdTgs KdTgs Primary, Required, Unique
IdxNmTgs NmTgs –
Buat Desain Form seperti berikut :
Objek | Property | Value | Objek | Property | Value |
Frame1 | Caption | Biodata | Command3 | Name | cmdPerbaiki |
Frame2 | Capiton | Proses | Caption | &PERBAIKI | |
Text1 | Name | txtKdTgs | Command4 | Name | cmdHapus |
Text2 | Name | txtNmTgs | Caption | &HAPUS | |
Text3 | Name | txtTpLhr | Command5 | Name | cmdBatal |
Text4 | Name | txtTgLhr | Caption | &BATAL | |
Option1 | Name | optJkLki | Command6 | Name | cmdSelesai |
Caption | &LAKI-LAKI | Caption | S&ELESAI | ||
Option2 | Name | optJkPrp | Command7 | Name | cmdCari |
Caption | PERE&MPUAN | Caption | &CARI | ||
Combo1 | Name | cboAgama | DBGrid1 | Name | grdTabel |
Text5 | Name | txtAdres | Caption | DAFTAR PETUGAS | |
Label9 | Name | lblPesan | DataSource | Data1 | |
Caption | LBLPESAN | Data1 | Caption | Data1 | |
Command1 | Name | cmdTambah | DatabaseName | dbAplikasi | |
Caption | &TAMBAH | RecordSource | tblPetugas | ||
Command2 | Name | cmdSimpan | Visible | False | |
Caption | &SIMPAN |
Source Code Program :
________________________________________________________________
Dim dbData As Database
Dim rsPetugas As Recordset
Dim cPilih As Boolean
________________________________________________________________
Private Sub Kosong()
txtKdTgs.Text = “”
txtNmTgs.Text = “”
txtTpLhr.Text = “”
txtTgLhr.Text = “”
optJkLki.Value = False
optJKPrp.Value = False
cboAgama.Text = “”
txtAdres.Text = “”
cPilih = False
End Sub
________________________________________________________________
Private Sub TombolAwal()
cmdTambah.Enabled = True
cmdSimpan.Enabled = False
cmdPerbaiki.Enabled = False
cmdHapus.Enabled = False
cmdBatal.Enabled = False
cmdSelesai.Enabled = True
cmdCari.Enabled = True
cmdSimpan.Caption = “&SIMPAN”
lblPesan.Visible = False
End Sub
________________________________________________________________
Private Sub TakBisa()
txtKdTgs.Enabled = False
txtNmTgs.Enabled = False
txtTpLhr.Enabled = False
txtTgLhr.Enabled = False
optJkLki.Enabled = False
optJKPrp.Enabled = False
cboAgama.Enabled = False
txtAdres.Enabled = False
End Sub
________________________________________________________________
Private Sub Bisa()
txtKdTgs.Enabled = True
txtNmTgs.Enabled = True
txtTpLhr.Enabled = True
txtTgLhr.Enabled = True
optJkLki.Enabled = True
optJKPrp.Enabled = True
cboAgama.Enabled = True
txtAdres.Enabled = True
End Sub
________________________________________________________________
Private Sub cboAgama_Click()
txtAdres.SetFocus
End Sub
________________________________________________________________
Private Sub cmdBatal_Click()
Kosong
txtKdTgs.Text = “”
TakBisa
TombolAwal
End Sub
________________________________________________________________
Private Sub cmdCari_Click()
cmdCari.Enabled = False
cmdTambah.Enabled = False
cmdBatal.Enabled = True
cmdSelesai.Enabled = False
TakBisa
txtNmTgs.Enabled = True
txtNmTgs.SetFocus
End Sub
________________________________________________________________
Private Sub cmdHapus_Click()
cPesan = MsgBox(“Yakin Data Petugas Ybs Akan Dihapus…?”, vbYesNo, “Konfirmasi”)
If cPesan = vbYes Then
rsPetugas.Delete
Data1.Refresh
Data1.Recordset.MoveFirst
End If
Kosong
txtKdTgs.Text = “”
TakBisa
TombolAwal
End Sub
________________________________________________________________
Private Sub cmdPerbaiki_Click()
cmdSimpan.Enabled = True
cmdSimpan.Caption = “&UPDATE”
cmdPerbaiki.Enabled = False
cmdHapus.Enabled = False
If cmdCari.Enabled = False Then
rsPetugas.Index = “IdxKdTgs”
rsPetugas.Seek “=”, txtKdTgs
End If
cmdCari.Enabled = True
Bisa
txtNmTgs.SetFocus
End Sub
________________________________________________________________
Private Sub cmdSelesai_Click()
Unload Me
frmUtama.Enabled = True
frmUtama.SetFocus
End Sub
________________________________________________________________
Private Sub cmdSimpan_Click()
If cmdSimpan.Caption = “&SIMPAN” Then
rsPetugas.AddNew
rsPetugas!KdTgs = txtKdTgs.Text
rsPetugas!NmTgs = txtNmTgs.Text
rsPetugas!TpLhr = txtTpLhr.Text
rsPetugas!TgLhr = txtTgLhr.Text
If optJkLki.Value = True Then
rsPetugas!KdJek = “L”
Else
If optJKPrp.Value = True Then
rsPetugas!KdJek = “P”
End If
End If
rsPetugas!Agama = cboAgama.Text
rsPetugas!Adres = txtAdres.Text
rsPetugas.Update
Data1.Refresh
Data1.Recordset.MoveLast
Else
If cmdSimpan.Caption = “&UPDATE” Then
rsPetugas.Edit
rsPetugas!NmTgs = txtNmTgs.Text
rsPetugas!TpLhr = txtTpLhr.Text
rsPetugas!TgLhr = txtTgLhr.Text
If optJkLki.Value = True Then
rsPetugas!KdJek = “L”
Else
If optJKPrp.Value = True Then
rsPetugas!KdJek = “P”
End If
End If
rsPetugas!Agama = cboAgama.Text
rsPetugas!Adres = txtAdres.Text
rsPetugas.Update
cmdSimpan.Enabled = False
cmdSimpan.Caption = “&SIMPAN”
Data1.Refresh
End If
End If
Kosong
cmdSimpan.Enabled = False
txtKdTgs.SetFocus
End Sub
________________________________________________________________
Private Sub cmdTambah_Click()
Kosong
Bisa
cmdCari.Enabled = True
cmdTambah.Enabled = False
cmdBatal.Enabled = True
cmdSelesai.Enabled = False
txtKdTgs.SetFocus
End Sub
________________________________________________________________
Private Sub Form_Activate()
cmdTambah.SetFocus
End Sub
________________________________________________________________
Private Sub Form_Load()
Kosong
TakBisa
TombolAwal
cboAgama.AddItem (“ISLAM”)
cboAgama.AddItem (“KRISTEN”)
cboAgama.AddItem (“KATOLIK”)
cboAgama.AddItem (“HINDU”)
cboAgama.AddItem (“BUDHA”)
Set dbData = OpenDatabase(App.Path & “\dbAplikasi.mdb”)
Set rsPetugas = dbData.OpenRecordset(“tblPetugas”)
rsPetugas.Index = “IdxKdTgs”
cPilih = False
End Sub
________________________________________________________________
Private Sub grdTabel_Click()
cmdPerbaiki.Enabled = True
cmdHapus.Enabled = True
cmdBatal.Enabled = True
cmdTambah.Enabled = False
cmdSelesai.Enabled = False
cPilih = True
End Sub
________________________________________________________________
Private Sub grdTabel_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
If cPilih = True Then
cmdCari.Enabled = False
rsPetugas.Seek “=”, Data1.Recordset!KdTgs
txtNmTgs.Text = rsPetugas!NmTgs
txtTpLhr.Text = rsPetugas!TpLhr
txtTgLhr.Text = rsPetugas!TgLhr
If rsPetugas!KdJek = “L” Then
optJkLki.Value = True
Else
optJKPrp.Value = True
End If
cboAgama.Text = rsPetugas!Agama
txtAdres.Text = rsPetugas!Adres
TakBisa
End If
End Sub
________________________________________________________________
Private Sub optJkLki_Click()
If cmdCari.Enabled = True Then
cboAgama.SetFocus
End If
End Sub
________________________________________________________________
Private Sub optJKPrp_Click()
If cmdCari.Enabled = True Then
cboAgama.SetFocus
End If
End Sub
________________________________________________________________
Private Sub txtAdres_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
cmdSimpan.Enabled = True
cmdSimpan.SetFocus
End If
End Sub
________________________________________________________________
Private Sub txtKdTgs_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
rsPetugas.Index = “IdxKdTgs”
rsPetugas.Seek “=”, txtKdTgs.Text
If rsPetugas.NoMatch Then
txtNmTgs.SetFocus
Else
txtNmTgs.Text = rsPetugas!NmTgs
txtTpLhr.Text = rsPetugas!TpLhr
txtTgLhr.Text = rsPetugas!TgLhr
If rsPetugas!KdJek = “L” Then
optJkLki.Value = True
Else
optJKPrp.Value = True
End If
cboAgama.Text = rsPetugas!Agama
txtAdres.Text = rsPetugas!Adres
TakBisa
cmdHapus.Enabled = True
cmdPerbaiki.Enabled = True
Data1.Recordset.FindFirst “KdTgs =’” + txtKdTgs.Text + “‘”
Data1.Recordset.GetRows
Data1.Recordset.MovePrevious
End If
End If
End Sub
________________________________________________________________
Private Sub txtNmTgs_Change()
If cmdCari.Enabled = False Then
Dim nPanjang As String
nPanjang = Len(RTrim(txtNmTgs.Text))
Data1.Recordset.FindFirst “Left(NmTgs,” + nPanjang + “) =’” + RTrim(txtNmTgs.Text) + “‘”
If Data1.Recordset.NoMatch Then
lblPesan.Visible = True
lblPesan.Caption = “DATA YANG DICARI TIDAK ADA”
txtKdTgs.Text = “”
txtTpLhr.Text = “”
txtTgLhr.Text = “”
optJkLki.Value = False
optJKPrp.Value = False
cboAgama.Text = “”
txtAdres.Text = “”
Else
Data1.Recordset.GetRows
Data1.Recordset.MovePrevious
lblPesan.Visible = True
lblPesan.Caption = “TEKAN ENTER UNTUK MEMPERBAIKI/MENGHAPUS”
txtKdTgs.Text = Data1.Recordset!KdTgs
txtTpLhr.Text = Data1.Recordset!TpLhr
txtTgLhr.Text = Data1.Recordset!TgLhr
If Data1.Recordset!KdJek = “L” Then
optJkLki.Value = True
Else
optJKPrp.Value = True
End If
cboAgama.Text = Data1.Recordset!Agama
txtAdres.Text = Data1.Recordset!Adres
End If
End If
End Sub
________________________________________________________________
Private Sub txtNmTgs_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If cmdCari.Enabled = True Then
If Len(txtNmTgs.Text) > 0 Then
txtTpLhr.SetFocus
End If
Else
txtKdTgs.Text = Data1.Recordset!KdTgs
txtNmTgs.Text = Data1.Recordset!NmTgs
txtTpLhr.Text = Data1.Recordset!TpLhr
txtTgLhr.Text = Data1.Recordset!TgLhr
If Data1.Recordset!KdJek = “L” Then
optJkLki.Value = True
Else
optJKPrp.Value = True
End If
cboAgama.Text = Data1.Recordset!Agama
txtAdres.Text = Data1.Recordset!Adres
txtNmTgs.Enabled = False
cmdPerbaiki.Enabled = True
cmdHapus.Enabled = True
lblPesan.Visible = False
End If
End If
End Sub
________________________________________________________________
Private Sub txtTpLhr_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Len(txtTpLhr.Text) > 0 Then
txtTgLhr.SetFocus
End If
End If
End Sub
________________________________________________________________
Pagi Pak, Bapak punya modul tentang Visual Basic Express 2010?
Trims
Oke Pak,,, Terima kasih
permisi pk mf menganggu, sy mau tanya pk untuk coding mencari data tp lgsg tmpil gambarnya, misal kita ketik Nim klik tombol cari mk akan tmpil
Nama :
Tgl lahir :
foto :
dstnya
mohon pencerahannya pk, tks
Coba download modul Pemrograman Visual Basic yang ada di blog ini Mas ya…, makasih
oh ia pak, makasih