Modul Visual BasicD3 Agus Muhardi – Alga Musa
BAB XV PENCARIAN DATA DENGAN INDEX
Untuk melakukan pencarian dengan cepat diperlukan index data. Dengan adanya index data, maka data akan terurut sehingga pencarian tidak harus berurutan, tetapi dapat
dengan melakukan pencarian biner, yaitu selalu mencari data dengan cara membandingkan data yang dicari dengan data tengah suatu set data. Jika data tidak sama,
maka set data akan dipecah lagi dan membandingkan data yang dicari dengan tengah set data yang sesuai setelah dipecah. Demikian seterusnya, sehingga langkah pencarian akan
kecil sedikit dan didapatkan kecepatan pencarian yang baik cepat, walaupun datanya besar.
Untuk memahami pencarian data dengan index lakukan langkah-langkah sebagai berikut :
1. Buka LAT02. 2. Simpan proyek dengan nama baru LAT03 File, Save Project As
3. Simpan form dengan nama baru LAT03 File, Save Lat02.frm As
4. Karena objek Adodc tidak mempunyai metoda pencarian data dengan index, maka harus diganti dengan objek Data.
5. Tentukan setting property objek data menjadi sbb :
Objek Setting
Name dbPembelian Caption Database
Pembelian Connect Acces
Database Name C:\Zidan\Pembelian.mdb
RecordsetType 0 – Table karena property Index dan
metode Seek hanya dapat digunakan pada setting ini
RecordSource Barang
Modul Visual BasicD3 Agus Muhardi – Alga Musa
6. Tampilkan editor kode program dan editlah kode programnya menjadi sbb :
Privat e Sub cmdcari_Click Dim mkode As St ring 6
mkode = t xt cari. Text msyarat = Kode = mkode
dbpembelian. Recordset . Index = Kodebrg dbpembelian. Recordset . Seek =, mkode
If dbpembelian. Recordset . NoMat ch Then
X = MsgBoxDat a t idak dit emukan , vbOKOnly, Pencarian Kode Barang
End If
End Sub Privat e Sub cmdexit _Click
End End Sub
Privat e Sub Form_Act ivat e t xt cari. Set Focus
End Sub Privat e Sub t xt cari_Got Focus
cmdcari. Def ault = True End Sub
Privat e Sub t xt cari_Lost Focus cmdcari. Def ault = False
End Sub
7. Simpan lagi proyek dan form dengan nama sama. 8. Jalankan program.
Modul Visual BasicD3 Agus Muhardi – Alga Musa
Contoh program 2: ¾ Buatlah form dengan lay-out sbb :
¾ Hasil program adalah sbb :
¾ Caranya : 1
Buka LAT03. 2
Simpan proyek dengan nama baru LAT04 File, Save Project As 3
Simpan form dengan nama baru LAT04 File, Save Lat03.frm As 4
Hapus kontrol yang tidak diperlukan. 5
Atur property untuk kontrol-kontrolnya sbb :
Kontrol Properti Nilai
Form1 Caption
Contoh Kontrol Data
Modul Visual BasicD3 Agus Muhardi – Alga Musa
Name dbPembelian Caption Database
Pembelian Connect Acces
Database Name
C:\Zidan\Pembelian.mdb RecordsetType 0 – Table
Data1
RecordSource Barang Name TxtKode
Text Kosongkan DataSource dbPembelian
Text1
DataField Kode Name TxtNama
Text Kosongkan DataSource dbPembelian
Text2
DataField Nama Name TxtSatuan
Text Kosongkan DataSource dbPembelian
Text3
DataField Satuan Name TxtHarga
Text Kosongkan DataSource dbPembelian
Text4
DataField Harga Name CmdTop
Caption Kosongkan Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Vfp98\Wizard\Graphic
Command1
ToolTipText Top Name CmdBack
Caption Kosongkan Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Vfp98\Wizard\Graphic
Command2
ToolTipText Back Name CmdNext
Caption Kosongkan Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Vfp98\Wizard\Graphic
Command3
ToolTipText Next Name CmdLast
Caption Kosongkan Command4
Style 1 - Graphical
Modul Visual BasicD3 Agus Muhardi – Alga Musa
Picture C:\Program Files\Visual
Studio\Vfp98\Wizard\Graphic ToolTipText Last
Name CmdNew Caption Kosongkan
Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Common\Graphic\Bitmaps\OffC
tlrBr\Large\Color Command5
ToolTipText New Name CmdSave
Caption Kosongkan Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Common\Graphic\Bitmaps\OffC
tlrBr\Large\Color Command6
ToolTipText Save Name CmdUndo
Caption Kosongkan Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Common\Graphic\Bitmaps\OffC
tlrBr\Large\Color Command7
ToolTipText Undo Name CmdEdit
Caption Kosongkan Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Common\Graphic\Bitmaps\OffC
tlrBr\Large\Color Command8
ToolTipText Edit Name CmdDel
Caption Kosongkan Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Common\Graphic\Bitmaps\OffC
tlrBr\Large\Color Command9
ToolTipText Del Name CmdCari
Caption Kosongkan Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Common\Graphic\Bitmaps\OffC
tlrBr\Large\Color Command10
ToolTipText Find Command11 Name
CmdExit
Modul Visual BasicD3 Agus Muhardi – Alga Musa
Caption Kosongkan Style
1 - Graphical Picture C:\Program
Files\Visual Studio\Common\Graphic\Bitmaps\OffC
tlrBr\Large\Color ToolTipText Exit
6 Tampilkan editor kode program dan ketiklah kode programnya sbb :
Dim t ambah As Boolean Sub nonakt if
Dim ct l As Cont rol For Each ct l In FormBrg
If TypeNamect l = t ext box Then ct l. Locked = True
End If If TypeNamect l = combobox Then
ct l. Locked = True End If
Next End Sub
Privat e Sub cmdback_Click dbpembelian. Recordset . MovePrevious
If dbpembelian. Recordset . BOF Then dbpembelian. Recordset . MoveFirst
MsgBox Sudah di awal record, 16, Inf ormasi End If
End Sub Privat e Sub cmdcari_Click
Dim mkode As St ring 6
mkode = Input BoxKet ik Kode Barangnya : , Pencarian Dat a Barang
msyarat = Kode = mkode dbpembelian. Recordset . Index = Kodebrg
dbpembelian. Recordset . Seek =, mkode If dbpembelian. Recordset . NoMat ch Then
MsgBox Record Kode Barang + cr + Tidak ada, 16, Inf ormasi End If
End Sub Privat e Sub cmddel_Click
del = MsgBoxRecord ini dihapus ?, 36, Konf irmasi If del = 6 Then
dbpembelian. Recordset . Delet e dbpembelian. Recordset . MoveNext
If dbpembelian. Recordset . EOF Then
Modul Visual BasicD3 Agus Muhardi – Alga Musa
dbpembelian. Recordset . MoveLast End If
End If End Sub
Privat e Sub cmdedit _Click dbpembelian. Recordset . Edit
akt if End Sub
Privat e Sub cmdexit _Click Unload Me
End Sub Privat e Sub cmdlast _Click
dbpembelian. Recordset . MoveLast MsgBox Sudah di akhir record, 16, Imf ormasi
End Sub Privat e Sub cmdnew_Click
t ambah = True dbpembelian. Recordset . AddNew
kosong akt if
End Sub Privat e Sub cmdnext _Click
dbpembelian. Recordset . MoveNext If dbpembelian. Recordset . EOF Then
dbpembelian. Recordset . MoveLast MsgBox Sudah di akhir record, 16, Inf ormasi
End If End Sub
Privat e Sub cmdsave_Click If t ambah = True Then
dbpembelian. Recordset . Updat e Else
dbpembelian. Recordset . Updat e End If
nonakt if End Sub
Privat e Sub cmdt op_Click dbpembelian. Recordset . MoveFirst
MsgBox Sudah di awal record, 16, Imf ormasi End Sub
Privat e Sub cmdundo_Click dbpembelian. Recordset . CancelUpdat e
Modul Visual BasicD3 Agus Muhardi – Alga Musa
dbpembelian. Recordset . MoveLast nonakt if
End Sub Privat e Sub Form_Load
nonakt if t ambah = False
End Sub Sub kosong
Dim ct l As Cont rol For Each ct l In FormBrg
If TypeNamect l = t ext box Then ct l. Text =
End If If TypeNamect l = combobox Then
ct l. Text = End If
Next End Sub
Sub akt if Dim ct l As Cont rol
For Each ct l In FormBrg If TypeNamect l = t ext box Then
ct l. Locked = False End If
If TypeNamect l = combobox Then ct l. Locked = False
End If Next
End Sub
7 Jalankan aplikasi tersebut dengan menekan F5
Modul Visual BasicD3 Agus Muhardi – Alga Musa
BAB XVI MENAMPILKAN DATA DENGAN DBGRID