Format tanggal ini mempunyai format dd/mm/yyyy karena menggunakan modul maka tidak perlu menuliskan kode lengkap pada tiap form cukup penggil dengan code yang simple saja pada Form Load, dan dapat disimpan ke database Ms Acces dengan type data Date and Time. Untuk lebih jelaskan berikut ini langkah-langkah pembuatanya..
1. Siapkan project di Visual Basic yaitu dengan membuat Standart Exe
2. Buka menu Project- Component, aktifkan Miscrosoft Masked Edit Control 6.0
1. Siapkan project di Visual Basic yaitu dengan membuat Standart Exe
2. Buka menu Project- Component, aktifkan Miscrosoft Masked Edit Control 6.0
'konversi string ke date
Public Function stod(sDate As String) As Date
Dim nDay As Integer
Dim nMonth As Integer
Dim nYear As Integer
nDay = Val(Left(sDate, 2))
nMonth = Val(Mid(sDate, 4, 2))
nYear = Val(Right(sDate, 4))
stod = DateSerial(nYear, nMonth, nDay)
End Function
'konversi data ke string
Public Function dtos(ddate As Date) As String
Dim nDay As Integer
Dim nMonth As Integer
Dim nYear As Integer
nDay = Day(ddate)
nMonth = Month(ddate)
nYear = Format(ddate, "yyyy")
dtos = AddZerro(Str(nDay), 2) + "/" + AddZerro(Str(nMonth), 2) + "/" + AddZerro(Str(nYear), 4)
End Function
'fungsi menambah karakter nol (0) di sebelah kiri
Public Function AddZerro(Stringdata As String, lenght As Integer) As String
Dim stemp As String
stemp = "1" + String(lenght, "0")
AddZerro = Right(Val(stemp) + Val(Stringdata), lenght)
End Function
'---------------------------------------------------------------
'Validasi Data String Berformat Tanggal
'---------------------------------------------------------------
Public Function DateError(sDate As String) As Boolean
Dim nDay As Integer
Dim nMonth As Integer
Dim nYear As Integer
Dim aMonthList As Variant
aMonthList = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
DateError = False
'Memisahkan Elemen tanggal
nDay = Val(Left(sDate, 2))
nMonth = Val(Mid(sDate, 4, 2))
nYear = Val(Right(sDate, 4))
'Cek Tahun Kabisat
If (nYear Mod 4) = 0 Then aMonthList(1) = 29
'Cek Bulan & Tanggal
If nMonth < 1 Or nMonth > 12 Then
DateError = True
ElseIf nDay < 1 Or nDay > aMonthList(nMonth - 1) Then
DateError = True
End If
If Mid(sDate, 3, 1) <> "/" Or Mid(sDate, 6, 1) <> "/" Then
DateError = True
End If
If Len(Trim(Str(nYear))) <> 4 Then
DateError = True
End If
If nYear <= 1800 Then
DateError = True
End If
If sDate = "__/__/____" Then
DateError = False
End If
End Function
6. Sebagai percobaan untuk menampilkan tanggal hari ini masuk pada View Code Form, lalu masukkan code dibawah ini
Private Sub Form_Load()
Me.MaskEdBox1 = dtos(Date)
End Sub
7. Jalankan Form, bila benar tampilan akan seperti dibawah ini
0 komentar :
Posting Komentar