-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdlFunctions.bas
360 lines (343 loc) · 12.8 KB
/
mdlFunctions.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
Option Explicit
Function FalseInput(CtrlName As Control) As Boolean
Dim tData As String
If CtrlName = "" Then Exit Function
If Not IsDate(CtrlName) Then GoTo tCont
tData = InputDate(CtrlName)
If Not tData Like "12:00*" Then Exit Function
tCont:
CtrlName = ""
CtrlName.SetFocus
FalseInput = True
End Function
Function InputDate(iDateStr As Variant) As Date
' Send data piece from database to console
' default the data will from db to console, output shall be formated
' input shall be converted back to serial date
Dim iStr As String, iSpliter As Variant
On Error GoTo ErrHandler
iSpliter = Split(iDateStr, "/")
If UBound(iSpliter) < 2 Then GoTo ErrHandler
' Now we have to see what locale we are now at
InputDate = DateSerial(iSpliter(2), iSpliter(0), iSpliter(1))
ErrHandler:
End Function
'===================================================
' For form level object
Private Sub ArchiveConfig()
ExternalLoad = True
Dim MyCtl As Control, i As Long, j As Long
Dim MyForm As UserForm
Dim MyCell As Range, tmpCell As Range
Set MyCell = Range("tblFormConfig").Offset(1)
Set MyForm = frmMain
'1. Search for area to keep data
For Each MyCtl In MyForm.Controls
MyCell = "FORM_frmMain"
MyCell.Offset(, 2) = MyCtl.Name
'If TypeOf MyCtl Is MultiPage Then
' MyCell.Offset(, 1) = 1
' Set tmpCell = MyCell
' For j = 0 To MyCtl.Pages.Count - 1
' tmpCell.Offset(, j + 3) = MyCtl.Pages(j).Caption
' Next
'Else
' MyCell.Offset(, 1) = 0
' MyCell.Offset(, 3) = GetCaption(MyCtl)
'End If
Set MyCell = MyCell.Offset(1)
Next
End Sub
Property Get SetConfig(ObjName As String, FrmObj As UserForm) As String
Dim j As Long
Dim MyCell As Range, tmpCell As Range
Set MyCell = Range("tblFormConfig").Offset(1)
'1. Search for area to keep data
While MyCell <> "" And MyCell <> ObjName
Set MyCell = MyCell.Offset(1)
Wend
While MyCell = ObjName
Select Case MyCell.Offset(, 1)
Case 0:
SetCaption FrmObj.Controls(CStr(MyCell.Offset(, 2))), MyCell.Offset(, 3), MyCell.Offset(, 4)
Case 2, 4: ' Just set tag value
FrmObj.Controls(CStr(MyCell.Offset(, 2))).Tag = MyCell.Offset(, 4)
SetCaption FrmObj.Controls(CStr(MyCell.Offset(, 2))), MyCell.Offset(, 3), MyCell.Offset(, 4)
Case 3:
' for form caption
SetConfig = MyCell.Offset(, 3)
Case Else
Set tmpCell = MyCell
For j = 0 To FrmObj.Controls(CStr(MyCell.Offset(, 2))).Pages.Count - 1
FrmObj.Controls(CStr(MyCell.Offset(, 2))).Pages(j).Caption = tmpCell.Offset(, j + 3)
Next
End Select
Set MyCell = MyCell.Offset(1)
Wend
Set MyCell = Nothing
Set tmpCell = Nothing
End Property
Private Sub SetCaption(MyObj As Object, iCaption As String, Optional ControlTipStr As String = "")
If iCaption <> "" Then MyObj.Caption = iCaption
If ControlTipStr <> "" Then MyObj.ControlTipText = ControlTipStr
End Sub
Private Function GetCaption(Obj As Object) As String
On Error GoTo ErrHandler
GetCaption = Obj.Caption
ErrHandler:
End Function
Sub ToggleFilterKey()
' This shall help to disable filter
If Not ActiveSheet.FilterMode Then
QuickFilter
Else
ShowAll ActiveSheet
' Repair sheet if neccessary
RepairSheet ActiveSheet.Name
End If
End Sub
Sub InsertVillage()
If ActiveSheet.Name <> "II.2.A" Then Exit Sub
If MsgBox(MSG("MSG_ADD_VILLAGE"), vbQuestion + vbYesNo) = vbYes Then
Dim TheRange As Range
Set TheRange = AddRevVillage(1)
ShowOff
ModifyColumns
ShowOff True
' Get to Data table for putting village name
Sheets("Data").Activate
TheRange.Activate
End If
End Sub
Sub RemoveVillage()
If ActiveSheet.Name <> "II.2.A" Then Exit Sub
' if just remain 2 colums - dont allow removal
If Range("RNG_II2A").Column - Range("RNG_IIAST").Column = 6 Then
MsgBox MSG("MSG_REMOVE_VILLAGE_DISALLOW"), vbCritical
Exit Sub
End If
If MsgBox(Replace(MSG("MSG_REMOVE_VILLAGE"), "%s%", Sheet4.Range("RNG_II2A").Offset(0, -1)), vbQuestion + vbYesNo) = vbYes Then
Call AddRevVillage(-1)
ShowOff
ModifyColumns -1
ShowOff True
End If
End Sub
Private Function AddRevVillage(param As Long) As Range
Dim rng As Range
Set rng = ThisWorkbook.Sheets("Data").Range("tblVillageStart")
While Len(Trim(rng)) > 0
Set rng = rng.Offset(1)
Wend
' Now I am at the last point
If param < 0 Then
rng.Offset(-2) = rng.Offset(-1)
rng.Offset(-1) = ""
Else
rng = MSG("MSG_VIL_NEW")
Set AddRevVillage = rng
End If
Set rng = Nothing
End Function
Sub ShowAll(SheetObj As Worksheet)
On Error Resume Next
XUnProtectSheet SheetObj
SheetObj.ShowAllData
XProtectSheet SheetObj
SheetObj.Range("A8").Activate
End Sub
Sub ShowSelectForm()
' This shall display a form for selecting something
Dim isSelected As Boolean
Select Case ActiveSheet.Name
Case "II.5.B"
With ActiveCell
If .Row > 6 And .Row <= 555 Then
Select Case .Column
Case 8: ' Location for activity
With frmObjectParameter
.SpecialNote = MSG("MSG_ADD_LOCATION_VILLAGE")
.DataSetName = MSG("MSG_SELECT_VILLAGE")
.DataSource = "tblVillage"
.ReadOnly = True
.WrapOutput = True
End With
Case 15: ' Funding source
With frmObjectParameter
.DataSetName = MSG("MSG_ADD_BUDGET")
.DataSource = "Nguonvon"
.ModifyColumn = True
End With
Case 17: ' Unit in charge
With frmObjectParameter
.DataSetName = MSG("MSG_ADD_UNIT_INCHARGE")
.DataSource = "Bannganh"
End With
Case 18: ' Category
With frmObjectParameter
.DataSetName = MSG("MSG_SELECT_ACT_CATEGORY")
.DataSource = "LST_CATEGORY|"
'.SpecialNote = MSG("VAL_STATUS")
'.ReadOnly = True
End With
Case 19: ' Status
With frmObjectParameter
.DataSetName = MSG("MSG_SELECT_STATUS_ACT")
.DataSource = "Dexuat"
.SpecialNote = MSG("VAL_STATUS")
.ReadOnly = True
End With
Case Else
isSelected = True
End Select
If Not isSelected Then frmSelect.Show vbModal
End If
End With
Case "II.5.A":
With ActiveCell
If .Row > 5 And .Row <= 386 Then
Select Case .Column
Case 1: ' Sector
With frmObjectParameter
.DataSetName = MSG("MSG_ADD_SECTOR")
.DataSource = "Linhvuc"
.ReadOnly = True
End With
Case 2: ' Unit in charge
With frmObjectParameter
.DataSetName = MSG("MSG_ADD_UNIT_INCHARGE")
.DataSource = "Bannganh"
End With
Case Else
isSelected = True
End Select
If Not isSelected Then frmSelect.Show vbModal
End If
End With
Case "II.5.C":
With ActiveCell
If .Row > 5 And .Row <= 386 Then
Select Case .Column
Case 1: ' Sector
With frmObjectParameter
.SpecialNote = MSG("MSG_SEL_YEARS")
.DataSetName = MSG("MSG_SELECT_YEARS")
.DataSource = "LST_YEARS"
.ReadOnly = True
End With
Case 2: ' Unit in charge
With frmObjectParameter
.SpecialNote = MSG("MSG_ADD_LOCATION_VILLAGE")
.DataSetName = MSG("MSG_SELECT_VILLAGE")
.DataSource = "tblVillage"
.ReadOnly = True
End With
Case 3: ' Unit in charge
With frmObjectParameter
.DataSetName = MSG("MSG_CLIMATE_TYPE")
.SpecialNote = MSG("MSG_ADD_CLIMATE_TYPE")
.DataSource = "LST_CLIMATE_TYPE"
End With
Case 4: ' Unit in charge
With frmObjectParameter
.DataSetName = MSG("MSG_ADD_SECTOR")
.DataSource = "Linhvuc"
End With
Case Else
isSelected = True
End Select
If Not isSelected Then frmSelect.Show vbModal
End If
End With
Case "II.5.D":
With ActiveCell
If .Row > 5 And .Row <= 386 Then
Select Case .Column
Case 1: ' Sector
With frmObjectParameter
.SpecialNote = MSG("MSG_ADD_LOCATION_VILLAGE")
.DataSetName = MSG("MSG_SELECT_VILLAGE")
.DataSource = "tblVillage"
.ReadOnly = True
End With
Case 2: ' Unit in charge
With frmObjectParameter
.DataSetName = MSG("MSG_PRODUCTS_LINE")
.SpecialNote = MSG("MSG_ADD_PRODUCTS_LINE")
.DataSource = "LST_PRODUCTS_LINE"
End With
Case Else
isSelected = True
End Select
If Not isSelected Then frmSelect.Show vbModal
End If
End With
Case "II.6.D":
With ActiveCell
If .Row > 10 And .Row <= 561 Then
' If the ID row is blank - get off..
If ActiveSheet.Cells(.Row, 1) = "" Then Exit Sub
Select Case .Column
Case 11: ' Sector
With frmObjectParameter
.DataSetName = MSG("MSG_ADD_PROCURE_FORM")
.DataSource = "ProcureFORM"
End With
Case 12: ' Unit in charge
With frmObjectParameter
.DataSetName = MSG("MSG_ADD_CHECK_FORM")
.DataSource = "CHECKFORM"
End With
Case 15:
With frmObjectParameter
.DataSetName = MSG("MSG_ADD_COMPONENT")
.DataSource = "tblComponent"
End With
Case Else
isSelected = True
End Select
With frmObjectParameter
.ReadOnly = True
.NotAllowSelection = "["
End With
If Not isSelected Then frmSelect.Show vbModal
End If
End With
End Select
'reset form argument value
Dim lRet As FormArgument
frmObjectParameter = lRet
End Sub
Function GetFolder(strPath As String, Optional FilePicker As Boolean = False, Optional FileExtension As String = "*.*") As String
Dim fldr As FileDialog
Dim sItem As String
If FilePicker Then
Set fldr = Application.FileDialog(msoFileDialogFilePicker)
Else
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
End If
With fldr
.Title = MSG("MSG_SELECTDATAFOLDER")
.AllowMultiSelect = False
.InitialFileName = strPath
If FilePicker Then
.Filters.Clear
.Filters.Add "Mirosoft Excel File", FileExtension
End If
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
Function GetDate(txtString As String) As Date
' This will help converting Vietnamese date to English date
Dim Arr As Variant
Arr = Split(Replace(txtString, "'", ""), "/")
GetDate = DateSerial(Arr(2), Arr(1), Arr(0))
End Function
Function FormatDate(GivenDate As Date, Optional FormatType = VnDate, Optional DontSurpress As Boolean = False) As String
' This will override problematic date formating in Excel
FormatDate = IIf(DontSurpress, "", "'") & Format(GivenDate, FormatType)
End Function