You are on page 1of 6

Tulis kode di bawah ini If MsgBox("Tutup program ?

",
'Deklarasi variabel global vbQuestion + vbYesNo, _
Dim xpos As Single, ypos As Single Me.Caption) = vbYes Then
Private Sub Form_Load() Unload Me
'Pindahkan posisi roket ke tengah form End If
xpos = (Me.ScaleWidth - picRoket.Width) / End Sub
2
ypos = (Me.ScaleHeight - picRoket.Height) Private Sub RoketKeKiri()
/2 xpos = xpos - 10 'Geser ke kiri 10 pixel
picRoket.Move xpos, ypos If xpos < 0 Then
End Sub xpos = 0 'Cek bila melewati batas
Private Sub Form_KeyDown(KeyCode As kiri
Integer, Shift As Integer) End If
'MsgBox "Kode tombol yang ditekan : " & picRoket.Move xpos 'Pindahkan posisi
KeyCode roket
'Cek tombol keyboard yang ditekan End Sub
Select Case KeyCode
Case vbKeyLeft 'Tombol panah kiri Private Sub RoketKeKanan()
If Shift = vbCtrlMask Then xpos = xpos + 10
Call RoketKeSisiKiri 'Geser ke kanan 10 pixel
Else If xpos > Me.ScaleWidth - picRoket.Width
Call RoketKeKiri Then 'Cek bila melewati batas kanan
End If xpos = Me.ScaleWidth - picRoket.Width
Case vbKeyRight 'Tombol panah kanan End If
If Shift = vbCtrlMask Then picRoket.Move xpos
Call RoketKeSisiKanan 'Pindahkan posisi roket
Else End Sub
Call RoketKeKanan
End If Private Sub RoketKeAtas()
End Select ypos = ypos - 10 'Geser ke atas 10
End Sub pixel
Private Sub Form_KeyUp(KeyCode As If ypos < 0 Then 'Cek bila melewati
Integer, Shift As Integer) batas atas
'MsgBox "Kode tombol yang dilepas : " & ypos = 0
KeyCode End If
picRoket.Move xpos, ypos 'Pindahkan
'Cek tombol keyboard yang dilepas posisi roket
Select Case KeyCode End Sub
Case vbKeyUp 'Tombol panah atas
Call RoketKeAtas Private Sub RoketKeBawah()
Case vbKeyDown 'Tombol panah bawah ypos = ypos + 10
Call RoketKeBawah 'Geser ke bawah 10 pixel
End Select If ypos > Me.ScaleHeight -
End Sub picRoket.Height Then 'Cek bila melewati
batas bawah
Private Sub Form_KeyPress(KeyAscii As ypos = Me.ScaleHeight -
Integer) picRoket.Height
'MsgBox "Kode ASCII tombol yang End If
ditekan : " & KeyAscii picRoket.Move xpos, ypos
'Cek tombol keyboard yang ditekan 'Pindahkan posisi roket
If KeyAscii = vbKeyEscape Then 'Tombol End Sub
Escape Private Sub RoketKeSisiKiri()
xpos = 0 'Geser ke sisi kiri xpos = Me.ScaleWidth - picRoket.Width
picRoket.Move xpos, ypos 'Pindahkan 'Geser ke sisi kanan
posisi roket picRoket.Move xpos, ypos
End Sub 'Pindahkan posisi roket
Private Sub RoketKeSisiKanan() End Sub
Ketik Kode berikut:
Private Sub Command1_Click() Private Sub Option2_Click()
Label2.Caption = Text1.Text Label2.ForeColor = vbRed
End Sub End Sub
Private Sub Command2_Click() Private Sub Check1_Click()
End Label2.FontBold = Check1.Value
End Sub End Sub
Private Sub Option1_Click() Private Sub Check2_Click()
Label2.ForeColor = vbBlue Label2.FontItalic = Check2.Value
End Sub End Sub

Ketik Kode berikut


Private Sub Form_Load() Combo1.SetFocus
Combo1.AddItem "Umar" End Sub
Combo1.AddItem "Salman" Private Sub Command2_Click()
Combo1.AddItem "Halimah" List1.RemoveItem List1.ListIndex
Combo1.AddItem "Shafira" End Sub
Combo1.ListIndex = 0 Private Sub Command3_Click()
End Sub List1.Clear
Private Sub Command1_Click() Combo1.SetFocus
List1.AddItem Combo1.Text End Sub

Ketik Kode Berikut


Option Explicit Label2.Caption = test2
Dim test2 As Integer Label3.Caption = test3
Private Sub Command1_Click() End Sub
Dim test1 As String Private Sub Command3_Click()
test1 = "nusantara" Const test3 As Single = 90.55
Label1.Caption = test1 'test3 = 50.22
Label2.Caption = test2
Label3.Caption = test3 Label1.Caption = test1
End Sub Label2.Caption = test2
Private Sub Command2_Click() Label3.Caption = test3
test2 = 10 End Sub
Label1.Caption = test1
Ketikkan Kode Berikut
Dim var1 As Single, var2 As Single End Sub
Dim hasil As Single Private Sub Option8_Click()
Private Sub Form_Load() var1 = Text1.Text
Text1.Text = "" var2 = Text2.Text
Text2.Text = "" hasil = (var1 = var2)
Label3.Caption = "" Label3.Caption = Format(hasil,
End Sub "True/False")
Private Sub Option1_Click() End Sub
var1 = Text1.Text Private Sub Option9_Click()
var2 = Text2.Text var1 = Text1.Text
hasil = var1 + var2 var2 = Text2.Text
Label3.Caption = hasil hasil = (var1 <> var2)
End Sub Label3.Caption = Format(hasil,
Private Sub Option2_Click() "True/False")
var1 = Text1.Text End Sub
var2 = Text2.Text Private Sub Option10_Click()
hasil = var1 - var2 var1 = Text1.Text
Label3.Caption = hasil var2 = Text2.Text
End Sub hasil = (var1 >= var2)
Private Sub Option3_Click() Label3.Caption = Format(hasil,
var1 = Text1.Text "True/False")
var2 = Text2.Text End Sub
hasil = var1 * var2 Private Sub Option11_Click()
Label3.Caption = hasil var1 = Text1.Text
End Sub var2 = Text2.Text
Private Sub Option4_Click() hasil = (var1 <= var2)
var1 = Text1.Text Label3.Caption = Format(hasil,
var2 = Text2.Text "True/False")
hasil = var1 / var2 End Sub
Label3.Caption = hasil Private Sub Option12_Click()
End Sub var1 = IIf(Text1.Text = "True", -1, 0)
Private Sub Option5_Click() hasil = Not (var1)
var1 = Text1.Text Label3.Caption = Format(hasil,
var2 = Text2.Text "True/False")
hasil = var1 & var2 End Sub
Label3.Caption = hasil Private Sub Option13_Click()
End Sub var1 = IIf(Text1.Text = "True", -1, 0)
Private Sub Option6_Click() var2 = IIf(Text2.Text = "True", -1, 0)
var1 = Text1.Text hasil = (var1 Or var2)
var2 = Text2.Text Label3.Caption = Format(hasil,
hasil = (var1 > var2) "True/False")
Label3.Caption = Format(hasil, End Sub
"True/False") Private Sub Option14_Click()
End Sub var1 = IIf(Text1.Text = "True", -1, 0)
Private Sub Option7_Click() var2 = IIf(Text2.Text = "True", -1, 0)
var1 = Text1.Text hasil = (var1 And var2)
var2 = Text2.Text Label3.Caption = Format(hasil,
hasil = (var1 < var2) "True/False")
Label3.Caption = Format(hasil, End Sub
"True/False")
Ketik Kode Berikut
Dim kesempatan As Integer MsgBox "Passwordnya Salah !"
Private Sub Command1_Click() If kesempatan < 3 Then
kesempatan = kesempatan + 1 Text1.Text = ""
If (LCase(Text1.Text) = "nusantara") And Text1.SetFocus
(kesempatan <= 3) Then Else
Image1.Visible = True End
Text1.Enabled = False End If
Command1.Enabled = False End If
Else End Sub

Ketik Kode Berikut


Private Sub Form_Load() satuan = "Box"
List1.AddItem "Disket" Case "Buku"
List1.AddItem "Buku" Harga = 20000
List1.AddItem "Kertas" satuan = "Lusin"
List1.AddItem "Pulpen" Case "Kertas"
End Sub Harga = 25000
satuan = "Rim"
Private Sub Command1_Click() Case "Pulpen"
Dim Harga As Currency, Total As Currency Harga = 10000
Dim jumlah As Integer satuan = "Pak"
Dim Diskon As Single End Select
Dim satuan As String
lblBarang.Caption = "Barang : " &
If List1.Text = "" Then List1.Text
MsgBox "Anda belum memilih lblHarga.Caption = "Harga : " &
barang !!" Format(Harga, "Currency") & "/" & satuan
List1.ListIndex = 0 lblJumlah.Caption = "Jumlah : " &
Exit Sub Text1.Text & " " & satuan
End If
jumlah = Text1.Text
If Text1.Text = "" Then
MsgBox "Anda belum mengisi jumlah Select Case jumlah
barang !!" Case Is < 10
Text1.SetFocus Diskon = 0
Exit Sub Case 10 To 20
End If Diskon = 0.15
Case Else
If Not IsNumeric(Text1.Text) Then Diskon = 0.2
MsgBox "Isi jumlah barang harus End Select
angka !!"
Text1.SetFocus Total = jumlah * (Harga * (1 - Diskon))
Exit Sub
End If lblDiskon.Caption = "Diskon : " &
Format(Diskon, "0 %")
Select Case List1.Text lblTotal.Caption = "Total Bayar : " &
Case "Disket" Format(Total, "Currency")
Harga = 35000 End Sub
Ketik Kode Berikut
Dim i As Integer i = Asc("A")
Private Sub Command1_Click() Do Until i > Asc("Z")
List1.Clear List1.AddItem "Huruf " & Chr(i)
For i = 1 To 100 'If Chr(i) = "M" Then Exit Do
List1.AddItem "Angka " & i i=i+1
'If i = 50 Then Exit For Loop
Next i End Sub
End Sub Private Sub Command4_Click()
Private Sub Command2_Click() List1.Clear
List1.Clear i = Asc("Z")
For i = 100 To 1 Step -2 Do While i >= Asc("A")
List1.AddItem "Angka " & i List1.AddItem "Huruf " & Chr(i)
Next i i=i-1
End Sub Loop
Private Sub Command3_Click() End Sub
List1.Clear

Dim NamaSiswa() As String Next i


Private Sub Form_Load() End If
Dim i As Integer End Sub
ReDim NamaSiswa(1 To 5) Private Sub Command2_Click()
For i = 1 To 5 Dim num As Integer, i As Integer
Combo1.AddItem i If Not IsNumeric(Text1.Text) Then Exit
Next i Sub
Combo1.ListIndex = 0 num = CInt(Text1.Text)
End Sub 'ReDim NamaSiswa(1 To num)
Private Sub Command1_Click() ReDim Preserve NamaSiswa(1 To num)
Dim no As Integer, i As Integer Combo1.Clear
no = CInt(Combo1.Text) List1.Clear
NamaSiswa(no) = InputBox("Tuliskan For i = 1 To UBound(NamaSiswa)
nama siswa no : " & no, "Input Nama Siswa") Combo1.AddItem i
If NamaSiswa(no) <> "" Then List1.AddItem "NamaSiswa(" & i & ")="
List1.Clear & NamaSiswa(i)
For i = 1 To UBound(NamaSiswa) Next i
List1.AddItem "NamaSiswa(" & i & Combo1.ListIndex = 0
")=" & NamaSiswa(i) End Sub