6 KASUS 1: KONVERSI SUHU Jalankan aplikasi Embedded Visual Basic 3.0 Buat File baru, dan pilih file yang menggunakan form yaitu Windows CE for Pocket PC 2002. Lalu tambahkan kontrol-kontrol seperti dibawah ini. Kontrol Form TextBox TextBox Command Button Nama frmutama lbljudul lblcelcius lblfahrenheit txtcelcius txtfahrenheit cmdtutup Halaman 63
Atur properties untuk masing-masing kontrol seperti sebagai berikut. Nama Kontrol Properties Nilai frmutama Caption Konversi Suhu lbljudul Caption KONVERSI SUHU lbljudul Alligment 2 - vbcenter lblcelcius Caption Celcius : lblfahrenheit Caption Fahrenheit : txtcelcius Text 0 txtfahrenheit Text 0 txttutup Caption Tutup Masuklah ke bagian Code Editor dengan double-click form. Dan tambahkan isi kan kode program berikut ini. Kode Program 5.1 Option Explicit Dim focus As Byte Private Sub cmdtutup_click() App.End Private Sub Form_OKClick() App.End Private Sub txtcelcius_change() If txtcelcius.text = "" Then txtcelcius.text = 0 If focus = 1 Then txtfahrenheit.text = ((9 / 5) * CDbl(txtCelcius.Text)) + 32 Private Sub txtcelcius_keypress(keyascii As Integer) focus = 1 If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or KeyAscii = 8) Then KeyAscii = 0 Halaman 64
Private Sub txtfahrenheit_change() If txtfahrenheit.text = "" Then txtfahrenheit.text = 0 If focus = 2 Then txtcelcius.text = (5 / 9) * (CDbl(txtFahrenheit.Text) - 32) Private Sub txtfahrenheit_keypress(byval KeyAscii As Integer) focus = 2 If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or KeyAscii = 8) Then KeyAscii = 0 Coba jalankan program dengan menekan tombol Start Debug atau tombol F5 Gambar 6.1 Tampilan program Konversi Suhu Halaman 65
KASUS 2: BUKU ALAMAT Jalankan aplikasi Embedded Visual Basic 3.0. Buat File baru, dan pilih file yang menggunakan form, Windows CE for Pocket PC 2002. Lalu tambahkan kontrol-kontrol seperti dibawah ini. cmbnama cmbtemp Kontrol Form Combo Box Combo Box Text Box Text Box Text Box Text Box Text Box Command Button Command Button Command Button Command Button Nama frmutama cmbnama cmbtemp lbljudul lblnama lblperusahaan lblalamat lbltelepon lblemail txtnama txtperusahaan txtalamat txttelepon txtemail cmdnew cmdsave cmdedit cmddelete Halaman 66
Atur properties untuk masing-masing kontrol seperti berikut ini : Nama Kontrol Properties Nilai frmutama Caption Buku Telepon lbljudul Caption Buku Telepon lbljudul Alligment 2 vbcenter cmbnama Style 2-vbComboDropdownList cmbtemp Style 2-vbComboDropdownList cmbtemp Visible False lblnama Caption Nama : lblperusahaan Caption Perusahaan : lblalamat Caption Alamat : lbltelepon Caption Telepon : lblemail Caption Email : txtalamat ScrollBars 2 vbvertical cmdnew Caption New cmdsave Caption Save cmdsave Enabled False cmdsave Default True cmdedit Caption Edit cmddelete Caption Delete Tambahkan References Microsoft CE ADO Control 3.0 yang terdapat pada menu Project, References. Lalu klik OK Masuklah ke bagian Code Editor dengan double-click form. Dan tambahkan isi kan kode program berikut ini. Halaman 67
Kode Program 5.2 Option Explicit Dim objconnection Dim objrecordset Dim statuus As String Dim databasename As String Dim Dirubah As Integer Private Sub cmbnama_click() showname cmbnama.listindex Private Sub cmddelete_click() Dim a As Integer a = MsgBox("Apakah anda yakin ingin menghapus data ini??", vbyesno + vbquestion, App.Title) If a = vbyes Then objrecordset.open "DELETE FROM data WHERE nama='" & txtnama.text & "' AND id=" & resolveid(cmbnama.listindex), databasename, 2, 3 txtnama.text = "" txtperusahaan.text = "" txtalamat.text = "" txttelepon.text = "" txtemail.text = "" UpdateCombo Private Sub cmdedit_click() cmdsave.enabled = True cmdnew.enabled = False cmdedit.enabled = False cmddelete.enabled = False cmbnama.enabled = False Halaman 68
statuus = "ubah" Dirubah = resolveid(cmbnama.listindex) txtnama.locked = False txtperusahaan.locked = False txtalamat.locked = False txttelepon.locked = False txtemail.locked = False Private Sub cmdnew_click() txtnama.text = "" txtperusahaan.text = "" txtalamat.text = "" txttelepon.text = "" txtemail.text = "" txtnama.locked = False txtperusahaan.locked = False txtalamat.locked = False txttelepon.locked = False txtemail.locked = False cmdsave.enabled = True cmdnew.enabled = False cmdedit.enabled = False cmddelete.enabled = False cmbnama.enabled = False statuus = "baru" Private Sub CmdSave_Click() Dim skarang As Integer If txtnama.text <> "" Then If statuus = "baru" Then objrecordset.open "SELECT id FROM data ORDER BY id ASC", Halaman 69
databasename, 2, 3 If objrecordset.recordcount > 0 Then objrecordset.movelast skarang = objrecordset.fields("id") + 1 Else skarang = 1 objrecordset.close objrecordset.open "SELECT * FROM data", databasename, 2, 3 objrecordset.addnew objrecordset.fields("id") = skarang Else objrecordset.open "SELECT * FROM data WHERE id=" & Dirubah, databasename, 2, 3 objrecordset.fields("nama") = txtnama.text objrecordset.fields("perusahaan") = txtperusahaan.text objrecordset.fields("alamat") = txtalamat.text objrecordset.fields("telepon") = txttelepon.text objrecordset.fields("email") = txtemail.text objrecordset.update objrecordset.close MsgBox "Data berhasil disimpan", vbokonly, App.Title cmdsave.enabled = False cmdnew.enabled = True cmdedit.enabled = True cmddelete.enabled = True cmbnama.enabled = True UpdateCombo Else MsgBox "Silahkan masukkan Nama-nya", vbokonly + vbcritical, App.Title txtnama.locked = True Halaman 70
txtperusahaan.locked = True txtalamat.locked = True txttelepon.locked = True txtemail.locked = True Private Sub Form_Load() databasename = App.Path & "\BukuTelp.cdb" On Error Resume Next Set objrecordset = CreateObject("ADOCE.Recordset.3.0") UpdateCombo objrecordset.open "CREATE DATABASE '" & App.Path & "\BukuTelp.cdb'", "", 2, 3 objrecordset.open "CREATE TABLE data(id int,nama varchar(30),perusahaan varchar(30),alamat text,telepon varchar(15),email varchar(25))", databasename, 2, 3 Private Sub Form_OKClick() Set objrecordset = Nothing App.End Private Sub UpdateCombo() cmbnama.clear cmbtemp.clear objrecordset.open "SELECT * FROM data ORDER BY nama", databasename, 2, 3 If objrecordset.recordcount > 0 Then Do cmbnama.additem objrecordset.fields("nama") cmbtemp.additem objrecordset.fields("id") objrecordset.movenext Loop Until objrecordset.eof = True Halaman 71
objrecordset.close If cmbnama.listcount > 0 Then cmbnama.listindex = 0 showname (0) Private Sub showname(indexnama As Integer) If resolveid(indexnama) <> 0 Then objrecordset.open "SELECT * FROM data WHERE id=" & resolveid(indexnama), databasename, 2, 3 If objrecordset.recordcount > 0 Then txtnama.text = objrecordset.fields("nama") txtperusahaan.text = objrecordset.fields("perusahaan") txtalamat.text = objrecordset.fields("alamat") txttelepon.text = objrecordset.fields("telepon") txtemail.text = objrecordset.fields("email") objrecordset.close Private Function resolveid(index As Integer) As Integer If cmbtemp.list(index) <> "" Then resolveid = CInt(cmbTemp.List(index)) Else resolveid = 0 End Function Coba jalankan program dengan menekan tombol Start Debug atau tombol F5 Halaman 72
Gambar 6.2 Tampilan program Buku Telepon Halaman 73