6 Studi Kasus

• Masuklah ke bagian Code Editor dengan double-click form. ... ("0") And KeyAscii ...

0 downloads 108 Views 118KB Size
6

Studi Kasus

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 Label Label Label TextBox TextBox Command Button Studi Kasus

Nama frmUtama lblJudul lblCelcius lblFahrenheit txtCelcius txtFahrenheit cmdTutup Halaman 63



Atur properties untuk masing-masing kontrol seperti sebagai berikut. Nama Kontrol frmUtama lblJudul lblJudul lblCelcius lblFahrenheit txtCelcius txtFahrenheit txtTutup



Properties Caption Caption Alligment Caption Caption Text Text Caption

Nilai Konversi Suhu KONVERSI SUHU 2 - vbCenter Celcius : Fahrenheit : 0 0 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 End Sub

Private Sub Form_OKClick() App.End End Sub

Private Sub txtCelcius_Change() If txtCelcius.Text = "" Then txtCelcius.Text = 0 If

focus

=

1

Then

txtFahrenheit.Text

=

((9

/

5)

*

CDbl(txtCelcius.Text)) + 32 End Sub

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

Studi Kasus

End If End Sub

Private Sub txtFahrenheit_Change() If txtFahrenheit.Text = "" Then txtFahrenheit.Text = 0 If focus = 2 Then txtCelcius.Text = (5 / 9) * (CDbl(txtFahrenheit.Text) - 32) End Sub

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 End If End Sub



Coba jalankan program dengan menekan tombol Start Debug atau tombol F5

Gambar 6.1 Tampilan program Konversi Suhu

Studi Kasus

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.

cmbTemp

cmbNama

Kontrol Form Combo Box Combo Box Label Label Label Label Label Label Text Box Text Box Text Box Text Box Text Box Command Button Command Button Command Button Command Button

Halaman 66

Nama frmUtama cmbNama cmbTemp lblJudul lblNama lblPerusahaan lblAlamat lblTelepon lblEmail txtNama txtPerusahaan txtAlamat txtTelepon txtEmail cmdNew cmdSave cmdEdit cmdDelete

Studi Kasus



Atur properties untuk masing-masing kontrol seperti berikut ini : Nama Kontrol frmUtama lblJudul lblJudul cmbNama cmbTemp cmbTemp lblNama lblPerusahaan lblAlamat lblTelepon lblEmail txtAlamat cmdNew cmdSave cmdSave cmdSave cmdEdit cmdDelete



Properties Caption Caption Alligment Style Style Visible Caption Caption Caption Caption Caption ScrollBars Caption Caption Enabled Default Caption Caption

Nilai Buku Telepon Buku Telepon 2 – vbCenter 2-vbComboDropdownList 2-vbComboDropdownList False Nama : Perusahaan : Alamat : Telepon : Email : 2 – vbVertical New Save False True Edit 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.

Studi Kasus

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 End Sub

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 txtNama.Text

&

"'

"DELETE

AND

id="

FROM &

data

WHERE

nama='"

&

resolveId(cmbNama.ListIndex),

databaseName, 2, 3

txtNama.Text = "" txtPerusahaan.Text = "" txtAlamat.Text = "" txtTelepon.Text = "" txtEmail.Text = "" UpdateCombo End If End Sub

Private Sub cmdEdit_Click() cmdSave.Enabled = True cmdNew.Enabled = False cmdEdit.Enabled = False cmdDelete.Enabled = False cmbNama.Enabled = False

Halaman 68

Studi Kasus

statuus = "ubah" Dirubah = resolveId(cmbNama.ListIndex)

txtNama.Locked = False txtPerusahaan.Locked = False txtAlamat.Locked = False txtTelepon.Locked = False txtEmail.Locked = False

End Sub

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" End Sub

Private Sub CmdSave_Click() Dim skarang As Integer

If txtNama.Text <> "" Then If statuus = "baru" Then objRecordset.Open Studi Kasus

"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 End If 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 End If 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 End If

txtNama.Locked = True Halaman 70

Studi Kasus

txtPerusahaan.Locked = True txtAlamat.Locked = True txtTelepon.Locked = True txtEmail.Locked = True

End Sub

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

varchar(30),perusahaan

TABLE

data(id

varchar(30),alamat

int,nama text,telepon

varchar(15),email varchar(25))", databaseName, 2, 3

End Sub

Private Sub Form_OKClick() Set objRecordset = Nothing App.End End Sub

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 Studi Kasus

Halaman 71

End If objRecordset.Close

If cmbNama.ListCount > 0 Then cmbNama.ListIndex = 0 showName (0) End If

End Sub

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") End If objRecordset.Close End If End Sub

Private Function resolveId(index As Integer) As Integer If cmbTemp.List(index) <> "" Then resolveId = CInt(cmbTemp.List(index)) Else resolveId = 0 End If End Function



Coba jalankan program dengan menekan tombol Start Debug atau tombol F5

Halaman 72

Studi Kasus

Gambar 6.2 Tampilan program Buku Telepon

Studi Kasus

Halaman 73