Salah satu fitur menarik yang dimiliki aplikasi
Smart Library School yaitu form untuk input hari libur, dengan adanya fitur ini memudahkan operator untuk mencatat data hari libur.
Nah pada postingan kali ini saya akan share source code untuk membuat form tersebut, adapun komponen yang digunakan cukup :
- MSFlexGrid untuk menampilkan tanggal
- CommandButton untuk navigasi/perpindahan bulan
- TextBox untuk untuk menampilkan bulan aktif
- ListBox untuk menampilkan keterangan hari libur
Oke untuk pertama kita akan memformat tampilkan MsFlexGrid, adapun sourcenya seperti berikut :
01 | Dim arrHari( 6 ) As String |
20 | For x = 0 To .Cols - 1 |
25 | .FixedAlignment(x) = flexAlignCenterCenter |
28 | .ColAlignment(x) = flexAlignCenterCenter |
31 | For x = 0 To .Cols - 1 |
32 | .TextMatrix( 0 , x) = arrHari(x) 'menampilkan hari |
35 | For x = 0 To .Rows - 1 |
39 | .GridLines = flexGridFlat |
40 | .GridLinesFixed = flexGridFlat |
42 | .ForeColorFixed = &H 0 & 'WARNA_HITAM |
43 | .BackColorSel = &HED 9564 'WARNA_BIRU |
47 | Private Sub Form_Load() |
jika source diatas dijalankan akan menghasilkan tampilan seperti berikut :
Selanjutnya kita akan membuat prosedur untuk mengenerate data kalender bulan yang aktif, menampilkan hari libur minggu dan hari libur lainnya.
003 | Private Function roundOff(ByVal num As Double) As Integer |
009 | For ctr = 1 To Len(str) |
010 | If Mid(str, ctr, 1 ) = "." Then |
011 | roundOff = CInt(str 2 ) |
014 | str 2 = str 2 & Mid(str, ctr, 1 ) |
018 | roundOff = CInt(str 2 ) |
021 | Private Function detrmMonth(ByVal bulan As Integer) As Integer |
069 | Private Function DOTW(ByVal hari As Integer, ByVal bulan As Integer, ByVal tahun As Integer) As String |
071 | Dim result As Integer |
074 | result = roundOff(yr) + tahun |
077 | result = result - roundOff(yr) |
080 | result = result + roundOff(yr) |
081 | result = result + hari |
082 | result = result + detrmMonth(bulan) |
084 | result = result Mod 7 |
086 | DOTW = getHariByAngka(result) |
089 | Private Function getHariByAngka(ByVal hari As Integer) As String |
091 | Case 0: getHariByAngka = "Minggu" |
092 | Case 1: getHariByAngka = "Senin" |
093 | Case 2: getHariByAngka = "Selasa" |
094 | Case 3: getHariByAngka = "Rabu" |
095 | Case 4: getHariByAngka = "Kamis" |
096 | Case 5: getHariByAngka = "Jum'at" |
097 | Case 6: getHariByAngka = "Sabtu" |
101 | Private Function getAngkaByHari(ByVal hari As String) As Integer |
103 | Case "Minggu" : getAngkaByHari = 0 |
104 | Case "Senin" : getAngkaByHari = 1 |
105 | Case "Selasa" : getAngkaByHari = 2 |
106 | Case "Rabu" : getAngkaByHari = 3 |
107 | Case "Kamis" : getAngkaByHari = 4 |
108 | Case "Jum'at" : getAngkaByHari = 5 |
109 | Case "Sabtu" : getAngkaByHari = 6 |
113 | Private Sub setToDay(ByVal Col As Integer, ByVal Row As Integer) |
118 | .CellPictureAlignment = flexAlignCenterTop |
119 | Set .CellPicture = LoadPicture(App.Path & "\today.bmp" ) |
125 | Private Function getRowByCell(ByVal cell As Integer) As Integer |
127 | Case 1 To 7: getRowByCell = 1 |
128 | Case 8 To 14: getRowByCell = 2 |
129 | Case 15 To 21: getRowByCell = 3 |
130 | Case 22 To 28: getRowByCell = 4 |
131 | Case 29 To 35: getRowByCell = 5 |
132 | Case 36 To 42: getRowByCell = 6 |
133 | Case Else: getRowByCell = 1 |
137 | Private Function getColByCell(ByVal cell As Integer) As Integer |
139 | Case 1 , 8 , 15 , 22 , 29 , 36 |
142 | Case 2 , 9 , 16 , 23 , 30 , 37 |
145 | Case 3 , 10 , 17 , 24 , 31 , 38 |
148 | Case 4 , 11 , 18 , 25 , 32 , 39 |
151 | Case 5 , 12 , 19 , 26 , 33 , 40 |
154 | Case 6 , 13 , 20 , 27 , 34 , 41 |
157 | Case 7 , 14 , 21 , 28 , 35 , 42 |
162 | Private Sub setHariLibur(ByVal hari As Integer) |
167 | For x = 0 To .Cols - 1 |
168 | For y = 1 To .Rows - 1 |
169 | If Val(.TextMatrix(y, x)) = hari Then |
173 | If Day(Now) = hari Then 'hari libur pas hari ini |
174 | .CellPictureAlignment = flexAlignCenterTop |
176 | .CellPictureAlignment = flexAlignLeftTop |
179 | Set .CellPicture = LoadPicture(App.Path & "\smile.bmp" ) |
182 | .CellForeColor = vbRed |
189 | Private Sub makeCalendar(ByVal jumlahHari As Integer, ByVal bulan As Integer, ByVal tahun As Integer) |
200 | Dim ketLibur As String |
203 | lstKetHariLibur.Clear |
204 | For hari = 1 To jumlahHari |
205 | str = DOTW(hari, bulan, tahun) |
206 | y = getAngkaByHari(str) |
208 | For Index = cell To 41 |
209 | baris = getRowByCell(cell) |
210 | kolom = getColByCell(cell) |
214 | gridKalender.TextMatrix(baris, kolom) = hari |
216 | If Day(Now) = hari And Month(Now) = bulan Then Call setToDay(kolom, baris) 'setToDay -> prosedur untuk menampilkan icon today |
219 | Call setHariLibur(hari) |
221 | strSql = "SELECT COUNT(*) FROM hari_libur " & _ |
222 | "WHERE DAY(tanggal) = " & hari & " AND " & _ |
223 | "MONTH(tanggal) = " & bulan & " AND YEAR(tanggal) = " & tahun & "" |
224 | ret = CInt(dbGetValue(strSql, 0 )) |
226 | Call setHariLibur(hari) |
228 | strSql = "SELECT keterangan FROM hari_libur " & _ |
229 | "WHERE DAY(tanggal) = " & hari & " AND " & _ |
230 | "MONTH(tanggal) = " & bulan & " AND YEAR(tanggal) = " & tahun & "" |
231 | ketLibur = CStr(dbGetValue(strSql, "" )) |
232 | lstKetHariLibur.AddItem hari & " : " & ketLibur |
237 | If baris > 0 And kolom > 0 Then gridKalender.TextMatrix(baris, kolom) = "" |
245 | Private Function getJumlahHariByBulan(ByVal bulan As Integer, ByVal tahun As Long) As Integer |
246 | getJumlahHariByBulan = Day(DateSerial(tahun, bulan + 1 , 0 )) |
249 | Private Sub resetKalender() |
254 | For x = 0 To .Cols - 1 |
255 | For y = 1 To .Rows - 1 |
256 | .TextMatrix(y, x) = "" |
260 | Set .CellPicture = Nothing |
262 | .CellFontBold = False |
263 | .CellForeColor = &H 0 & 'WARNA_HITAM |
264 | .CellBackColor = &H 80000005 'WARNA_PUTIH |
270 | Private Sub genKalender() |
271 | Dim jumlahHariByBulan As Integer |
274 | num = Year(setMonth) Mod 4 |
283 | jumlahHariByBulan = getJumlahHariByBulan(Month(setMonth), Year(setMonth)) |
284 | Call makeCalendar(jumlahHariByBulan, Month(setMonth), Year(setMonth)) |
287 | Private Sub Form_Load() |
Hari libur akan disimpan didatabase Ms Access dengan struktur seperti berikut :
Prosedur berikutnya adalah untuk melakukan navigasi/perpindahan antar bulan
03 | Private Sub refreshBulan(ByVal bulan As Date) |
04 | txtBulan.Text = getBulanIndonesia(Month(bulan)) & " " & Year(bulan) |
07 | Private Sub cmdNext_Click() |
08 | setMonth = setNewMonth(True) |
09 | Call refreshBulan(setMonth) |
13 | Private Sub cmdPrev_Click() |
14 | setMonth = setNewMonth(False) |
15 | Call refreshBulan(setMonth) |
Untuk menambah dan menghapus hari libur kita akan memanfaat menu biasa dengan mode Pop Up dan untuk menghemat form untuk inputannya cukup menggunakan fungsi InputBox
01 | Private Sub mnuHariLibur_Click() |
02 | Dim inputKetLibur As String |
06 | inputKetLibur = InputBox( "Keterangan Hari Libur" , "Hari Libur" ) |
07 | If Len(inputKetLibur) > 0 Then |
08 | tanggal = Year(setMonth) & "/" & Month(setMonth) & "/" & Val(gridKalender.TextMatrix(gridKalender.Row, gridKalender.Col)) |
10 | strSql = "SELECT COUNT(*) FROM hari_libur " & _ |
11 | "WHERE tanggal = #" & tanggal & "#" |
12 | ret = CInt(dbGetValue(strSql, 0 )) |
14 | strSql = "INSERT INTO hari_libur(tanggal, keterangan) VALUES (#" & _ |
15 | tanggal & "#,'" & inputKetLibur & "')" |
24 | Private Sub mnuHapusHariLibur_Click() |
27 | If MsgBox( "Apakan Anda ingin menghapus hari libur ???" , vbExclamation + vbYesNo, "Konfirmasi" ) = vbYes Then |
28 | If Val(gridKalender.TextMatrix(gridKalender.Row, gridKalender.Col)) > 0 Then |
29 | tanggal = Year(setMonth) & "/" & Month(setMonth) & "/" & Val(gridKalender.TextMatrix(gridKalender.Row, gridKalender.Col)) |
31 | strSql = "DELETE FROM hari_libur " & _ |
32 | "WHERE tanggal = #" & tanggal & "#" |
adapun kode untuk menampilkan popup menu pada saat mengklik kanan kalender adalah seperti berikut :
01 | Private Sub gridKalender_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) |
02 | If Button = vbRightButton Then |
04 | If .MouseCol = 0 Then 'kolom hari minggu, semua menu dinonaktifkan |
05 | mnuHariLibur.Enabled = False |
06 | mnuHapusHariLibur.Enabled = False |
09 | If Val(.TextMatrix(.MouseRow, .MouseCol)) > 0 Then |
13 | If .CellForeColor > 0 Then 'font warna merah, berarti hari libur |
14 | mnuHariLibur.Enabled = False 'menu hari libur dinonaktifkan |
15 | mnuHapusHariLibur.Enabled = True |
18 | mnuHariLibur.Enabled = True |
19 | mnuHapusHariLibur.Enabled = False |
23 | mnuHariLibur.Enabled = True |
24 | mnuHapusHariLibur.Enabled = False |
sebagai penutup kita akan menambahkan prosedur otomatis untuk menyimpan hari libur khusus minggu yang akan dijalankan pada method Main
01 | Private Function getFirstSunday() As Integer |
02 | Dim firstDay As String |
04 | firstDay = Year(Now) & "/" & Month(Now) & "/1" |
05 | firstDay = Weekday(firstDay) |
06 | If Val(firstDay) > 1 Then |
07 | getFirstSunday = 9 - Val(firstDay) |
09 | getFirstSunday = Val(firstDay) |
13 | Private Sub addHariMinggu() |
15 | Dim firstDay As Integer |
20 | firstDay = getFirstSunday 'ambil tgl hari minggu pertama |
21 | For i = firstDay To getJumlahHariByBulan(Month(Now), Year(Now)) Step 7 |
22 | tgl = Year(Now) & "/" & Month(Now) & "/" & i |
24 | strSql = "SELECT COUNT(*) FROM hari_libur " & _ |
25 | "WHERE tanggal = #" & tgl & "# AND keterangan = 'Minggu'" |
26 | ret = CInt(dbGetValue(strSql, 0 )) |
28 | strSql = "INSERT INTO hari_libur(tanggal, keterangan) VALUES (#" & tgl & "#, 'Minggu')" |
35 | Set conn = New ADODB.Connection |
36 | conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\sampleDB.mdb" |
43 | 'prosedur otomatis untuk mengisikan tgl libur khusus hari minggu |
Selamat mencoba....
salam T.I.....
Tidak ada komentar:
Posting Komentar