Latihan kali ini
adalah latihan membuat program untuk menghitung usia, dimana dalam
program ini akan ditampilkan hari lahir, tanggal lahir, bulan lahir dan tahun
lahir, serta akan dihitung jumlah tahun, bulan dan hari usia. Dalam membuat
program ini harus diperhatikan hal - hal berikut : Jumlah hari setiap bulan tidak sama. Untuk bulan Januari, Maret, Mei, Juli, Agustus, Oktober dan Desember,
jumlah hari = 31 hari. Untuk bulan April, Juni, September dan November, jumlah
hari = 30 hari. Sedangkan untuk bulan Februari, jumlah hari = 29 hari untuk
tahun kabisat dan 28 hari untuk tahun bukan tahun kabisat. Tanggal Lahir harus
lebih kecil ( < ) dari tanggal sekarang (tanggal system).
Berikut langkah – langkah pembuatan program Program
Hitung Usia :
·
Buka proyek Standard EXE yang baru.
· Klik menu Project =>
Components atau tekan tombol Ctrl + T pada keyboard atau klik kanan pada
ToolBox, lslu pilih Components.
· Centang Microsoft Windows Common Controls-2 6.0,
lalu klik Apply lalu klik OK (perhatikan gambar di bawah)
Gbr 1
Kemudian objek DTPicker akan muncul di ToolBox
Gbr 2
· Disain tampilan form seperti gambar berikut
dengan menggunakan objek Label, TextBox dan CommandButton dan DTPicker dan Line
Gbr 3
·
Atur Properties masing - masing kontrol seperti
dibawah ini :
Kontrol
|
Properties
|
Pengaturan
|
DTPicker1
|
Text
Font
ForeColor
BackColor
|
(dikosongkan)
MS Sans Serif, Bold,
8
&H00800000&
&H00FFFFC0&
|
CommandButton1
|
Name
Caption
Font
Style
BackColor
|
cmd_cek
&Cek Tgl Lahir
MS Sans Serif, Bold,
8
1 - Graphical
&H00C0FFFF&
|
CommandButton2
|
Name
Caption
Font
Style
BackColor
|
cmd_hitung
&Hitung Usia
MS Sans Serif, Bold,
8
1 - Graphical
&H00C0FFFF&
|
CommandButton3
|
Name
Caption
Font
Style
BackColor
|
cmd_exit
E&xit
MS Sans Serif, Bold,
8
1 - Graphical
&H00C0FFFF&
|
Label1
|
Caption
Font
ForeColor
BackColor
|
Tanggal
Sekarang :
MS Sans Serif, Bold,
10
&H00004080&
&H0080FF80&
|
Label2
|
Caption
Font
ForeColor
BackColor
|
Tanggal
Lahir :
MS Sans Serif, Bold,
8
&H00000040&
&H0080FF80&
|
Label3
|
Caption
Font
ForeColor
BackColor
|
Anda Lahir
Hari :
MS Sans Serif, Bold,
8
&H00000040&
&H0080FF80&
|
Label4
|
Caption
Font
ForeColor
BackColor
|
Tanggal :
MS Sans Serif, Bold,
8
&H00000040&
&H0080FF80&
|
Label5
|
Caption
Font
ForeColor
BackColor
|
Bulan :
MS Sans Serif, Bold,
8
&H00000040&
&H0080FF80&
|
Label6
|
Caption
Font
ForeColor
BackColor
|
Tahun :
MS Sans Serif, Bold,
8
&H00000040&
&H0080FF80&
|
Label7
|
Caption
Font
ForeColor
BackColor
|
Usia
Anda :
MS Sans Serif, Bold,
8
&H00000040&
&H0080FF80&
|
Label8
|
Name
Caption
Font
ForeColor
BackColor
|
TglHariIni
(diskosongkan)
MS Sans Serif, Bold,
10
&H00004080&
&H0080FF80&
|
Label9
|
Name
Caption
Font
ForeColor
BackColor
|
Hari
(dikosongkan)
MS Sans Serif, Bold,
10
&H00800080&
&H0080C0FF&
|
Label10
|
Name
Caption
Font
ForeColor
BackColor
|
Tgl
(dikosongkan)
MS Sans Serif, Bold,
10
&H00000040&
&H0080C0FF&
|
Label11
|
Name
Caption
Font
ForeColor
BackColor
|
BlnLahir
(dikosongkan)
MS Sans Serif, Bold,
10
&H00000040&
&H0080C0FF&
|
Label13
|
Name
Caption
Font
ForeColor
BackColor
|
Thn
(dikosongkan)
MS Sans Serif, Bold,
10
&H00000040&
&H0080C0FF&
|
Label14
|
Name
Caption
Font
ForeColor
BackColor
|
Usia
(dikosongkan)
MS Sans Serif, Bold,
8
&H00000040&
&H0080C0FF&
|
Line1
|
BorderColor
BorderWidth
|
&H00FF0000&
2
|
Line2
|
BorderColor
BorderWidth
|
&H00FF0000&
2
|
Form1
|
Name
Caption
BackColor
|
Frm_HitungUsia
Program Untuk Menghitung Usia
&H0080FF80&
|
· Klik menu View => Code, lalu ketik Copy Paste (copas) kode dibawah ini :
Dim Tgl_Lahir As Date
Dim Tgl_Lahir As Date
Dim Tgl_Sekarang As Date
Dim Hr As Integer
Dim TglLhr, TglSystem As Integer
Dim BlnLhr, BlnSystem As Integer
Dim ThnLhr, ThnSystem As Integer
Dim SelisihHari, SelisihBln, SelisihThn
As Integer
Dim UsiaAnda As Integer
Private Sub cmd_cek_Click()
'bersihkan caption objek label
Me.Hari.Caption = ""
Me.Tgl.Caption = ""
Me.BlnLahir.Caption = ""
Me.Thn.Caption = ""
Me.Usia.Caption = ""
'ambil tanggal, bulan dan tahun dari
tanggal lahir
TglLhr = Me.DTPicker1.Day
BlnLhr = Me.DTPicker1.Month
ThnLhr = Me.DTPicker1.Year
'periksa tanggal lahir terhadap tanggal
system
If ThnSystem < ThnLhr And BlnSystem
< BlnLhr And TglSystem < TglLhr Then
MsgBox "Tanggal Lahir, Bulan Lahir dan Tahun Lahir Salah",
vbOKOnly + vbInformation, "Data Salah"
Me.DTPicker1.Value = Format(Now, "dd mm yyyy")
Me.cmd_hitung.Enabled = False
ElseIf ThnSystem < ThnLhr And
BlnSystem < BlnLhr Then
MsgBox "Bulan Lahir dan Tahun Lahir Salah", vbOKOnly +
vbInformation, ""
Me.DTPicker1.Value = Format(Now, "dd mm yyyy")
Me.cmd_hitung.Enabled = False
ElseIf ThnSystem = ThnLhr And BlnSystem
< BlnLhr And TglSystem < TglLhr Then
MsgBox "Tanggal Lahir dan Bulan Lahir Salah", vbOKOnly +
vbInformation, ""
Me.DTPicker1.Value = Format(Now, "dd mm yyyy")
Me.cmd_hitung.Enabled = False
ElseIf ThnSystem < ThnLhr And
BlnSystem >= BlnLhr And TglSystem < TglLhr Then
MsgBox "Tanggal Lahir dan Tahun Lahir Salah", vbOKOnly +
vbInformation, ""
Me.DTPicker1.Value = Format(Now, "dd mm yyyy")
Me.cmd_hitung.Enabled = False
ElseIf ThnSystem < ThnLhr Then
MsgBox "Tahun Lahir Salah", vbOKOnly + vbInformation,
""
Me.DTPicker1.Value = Format(Now, "dd mm yyyy")
Me.cmd_hitung.Enabled = False
ElseIf ThnSystem = ThnLhr And BlnSystem
< BlnLhr Then
MsgBox "Bulan Lahir Salah", vbOKOnly + vbInformation,
""
Me.DTPicker1.Value = Format(Now, "dd mm yyyy")
Me.cmd_hitung.Enabled = False
ElseIf ThnSystem = ThnLhr And BlnSystem
= BlnLhr And TglSystem < TglLhr Then
MsgBox "Tanggal Lahir Salah", vbOKOnly + vbInformation,
""
Me.DTPicker1.Value = Format(Now, "dd mm yyyy")
Me.cmd_hitung.Enabled = False
Else
Me.cmd_hitung.Enabled = True
End If
End Sub
Private Sub cmd_exit_Click()
End
End Sub
Private Sub cmd_hitung_Click()
Tgl_Sekarang = Format(Now, "dd mm
yyyy")
Hr = Me.DTPicker1.DayOfWeek
If Hr = 1 Then
Me.Hari.Caption = "Minggu"
ElseIf Hr = 2 Then
Me.Hari.Caption = "Senin"
ElseIf Hr = 3 Then
Me.Hari.Caption = "Selasa"
ElseIf Hr = 4 Then
Me.Hari.Caption = "Rabu"
ElseIf Hr = 5 Then
Me.Hari.Caption = "Kamis"
ElseIf Hr = 6 Then
Me.Hari.Caption = "Jumat"
Else
Me.Hari.Caption = "Sabtu"
End If
Me.Tgl.Caption = Me.DTPicker1.Day
Me.Bln.Caption = Me.DTPicker1.Month
BlnLhr = Me.DTPicker1.Month
Me.Thn.Caption = Me.DTPicker1.Year
If BlnLhr = 1 Then
Me.BlnLahir.Caption =
"Januari"
ElseIf BlnLhr = 2 Then
Me.BlnLahir.Caption =
"Februari"
ElseIf BlnLhr = 3 Then
Me.BlnLahir.Caption = "Maret"
ElseIf BlnLhr = 4 Then
Me.BlnLahir.Caption = "April"
ElseIf BlnLhr = 5 Then
Me.BlnLahir.Caption = "Mei"
ElseIf BlnLhr = 6 Then
Me.BlnLahir.Caption = "Juni"
ElseIf BlnLhr = 7 Then
Me.BlnLahir.Caption = "Juli"
ElseIf BlnLhr = 8 Then
Me.BlnLahir.Caption =
"Agustus"
ElseIf BlnLhr = 9 Then
Me.BlnLahir.Caption =
"September"
ElseIf BlnLhr = 10 Then
Me.BlnLahir.Caption =
"Oktober"
ElseIf BlnLhr = 11 Then
Me.BlnLahir.Caption =
"November"
Else
Me.BlnLahir.Caption =
"Desember"
End If
'cek tahun kabisat
'jika tahun kabisat, maka jumlah hari
untuk bulan februari = 29
If (Me.Bln.Caption = 2 And
Me.Thn.Caption Mod 4 = 0) Then
If (ThnSystem >= Me.Thn.Caption And BlnSystem > Me.Bln.Caption And
TglSystem < Me.Tgl.Caption) Then
SelisihHari = (TglSystem - Me.Tgl.Caption) + 29
SelisihBln = (BlnSystem - Me.Bln.Caption) - 1
SelisihThn = (ThnSystem - Me.Thn.Caption)
ElseIf ThnSystem > Me.Thn.Caption And BlnSystem < Me.Bln.Caption
And TglSystem < Me.Tgl.Caption Then
SelisihHari = (TglSystem - Me.Tgl.Caption) + 29
SelisihBln = (BlnSystem - Me.Bln.Caption) + 11
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
ElseIf ThnSystem > Me.Thn.Caption And BlnSystem < Me.Bln.Caption
And TglSystem > Me.Tgl.Caption Then
SelisihHari = (TglSystem - Me.Tgl.Caption)
SelisihBln = (BlnSystem - Me.Bln.Caption) + 12
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
End If
'jika bukan tahun kabisat, maka jumlah
hari untuk bulan februari = 28
ElseIf (Me.Bln.Caption = 2 And
Me.Thn.Caption Mod 4 <> 0) Then
If (ThnSystem >= Me.Thn.Caption And BlnSystem > Me.Bln.Caption And
TglSystem < Me.Tgl.Caption) Then
SelisihHari = (TglSystem - Me.Tgl.Caption) + 28
SelisihBln = (BlnSystem - Me.Bln.Caption) - 1
SelisihThn = (ThnSystem - Me.Thn.Caption)
ElseIf ThnSystem > Me.Thn.Caption And BlnSystem < Me.Bln.Caption
And TglSystem < Me.Tgl.Caption Then
SelisihHari = (TglSystem - Me.Tgl.Caption) + 28
SelisihBln = (BlnSystem - Me.Bln.Caption) + 11
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
ElseIf ThnSystem > Me.Thn.Caption And BlnSystem < Me.Bln.Caption
And TglSystem >= Me.Tgl.Caption Then
SelisihHari = (TglSystem - Me.Tgl.Caption)
SelisihBln = (BlnSystem - Me.Bln.Caption) + 12
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
End If
'jika bulan = januari, maret, mei,
juli,agustus, oktober atau desember, maka jumlah hari = 31
ElseIf (Me.Bln.Caption = 1 Or
Me.Bln.Caption = 3 Or Me.Bln.Caption = 5 Or Me.Bln.Caption = 7 Or
Me.Bln.Caption = 8 Or Me.Bln.Caption = 10 Or Me.Bln.Caption = 12) Then
If (ThnSystem > Me.Thn.Caption And BlnSystem >= Me.Bln.Caption And
TglSystem < Me.Tgl.Caption) Then
SelisihHari = (TglSystem - Me.Tgl.Caption) + 31
SelisihBln = (BlnSystem - Me.Bln.Caption) + 11
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
ElseIf (ThnSystem > Me.Thn.Caption And BlnSystem < Me.Bln.Caption
And TglSystem < Me.Tgl.Caption) Then
SelisihHari = (TglSystem - Me.Tgl.Caption) + 31
SelisihBln = (BlnSystem - Me.Bln.Caption) + 11
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
ElseIf (ThnSystem > Me.Thn.Caption And BlnSystem < Me.Bln.Caption
And TglSystem >= Me.Tgl.Caption) Then
SelisihHari = (TglSystem - Me.Tgl.Caption)
SelisihBln = (BlnSystem - Me.Bln.Caption) + 12
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
ElseIf (ThnSystem >= Me.Thn.Caption Or BlnSystem >= Me.Bln.Caption
Or TglSystem >= Me.Tgl.Caption) Then
SelisihHari = TglSystem - Me.Tgl.Caption
SelisihBln = BlnSystem - Me.Bln.Caption
SelisihThn = ThnSystem - Me.Thn.Caption
End If
'jika bulan = april,juni, september atau
november, maka jumlah hari = 30
ElseIf (Me.Bln.Caption = 4 Or
Me.Bln.Caption = 6 Or Me.Bln.Caption = 9 Or Me.Bln.Caption = 11) Then
If (ThnSystem > Me.Thn.Caption And BlnSystem >= Me.Bln.Caption And
TglSystem < Me.Tgl.Caption) Then
SelisihHari = (TglSystem - Me.Tgl.Caption) + 30
SelisihBln = (BlnSystem - Me.Bln.Caption) + 11
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
ElseIf (ThnSystem > Me.Thn.Caption And BlnSystem < Me.Bln.Caption
And TglSystem < Me.Tgl.Caption) Then
SelisihHari = (TglSystem -
Me.Tgl.Caption) + 30
SelisihBln = (BlnSystem - Me.Bln.Caption) + 11
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
ElseIf (ThnSystem > Me.Thn.Caption And BlnSystem < Me.Bln.Caption
And TglSystem >= Me.Tgl.Caption) Then
SelisihHari = (TglSystem - Me.Tgl.Caption)
SelisihBln = (BlnSystem - Me.Bln.Caption) + 12
SelisihThn = (ThnSystem - Me.Thn.Caption) - 1
ElseIf (ThnSystem >= Me.Thn.Caption Or BlnSystem >= Me.Bln.Caption
Or TglSystem >= Me.Tgl.Caption) Then
SelisihHari = TglSystem - Me.Tgl.Caption
SelisihBln = BlnSystem - Me.Bln.Caption
SelisihThn = ThnSystem - Me.Thn.Caption
End If
End If
Me.Usia.Caption = SelisihThn &
" Tahun, " & SelisihBln
& " Bulan, " &
SelisihHari & " Hari"
Me.cmd_hitung.Enabled = False
End Sub
Private Sub DTPicker1_Click()
'bersihkan caption objek label
Me.Hari.Caption = ""
Me.Tgl.Caption = ""
Me.BlnLahir.Caption = ""
Me.Thn.Caption = ""
Me.Usia.Caption = ""
End Sub
Private Sub Form_Load()
Me.cmd_hitung.Enabled = False
Me.TglHariIni.Caption = Format(Now,
"dddd, dd mmmm yyyy")
'ambil tanggal, bulan dan tahun dari
tanggal system
TglSystem = Format(Now, "dd")
BlnSystem = Format(Now, "mm")
ThnSystem = Format(Now,
"yyyy")
End Sub
·
Jalankan Program dengan cara menekan tombol F5
pada keyboard atau pilih menu Run =>
Start
·
Hasilnya ketika program dijalankan
Gbr 4
Download
Program Hitung Usia disini
Tidak ada komentar:
Posting Komentar
Pos kan komentar anda !