-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCSM_String.bas
More file actions
427 lines (375 loc) · 16.9 KB
/
CSM_String.bas
File metadata and controls
427 lines (375 loc) · 16.9 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
Attribute VB_Name = "CSM_String"
Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As POINTAPI) As Long
Public Function ReplaceQuote(ByVal Expression As String) As String
ReplaceQuote = Replace(Expression, "'", "''")
End Function
Public Function GetSubString(ByVal MainString As String, ByVal SubStringPosition As Integer, ByVal SubStringSeparator As String) As String
Dim aArray() As String
aArray = Split(MainString, SubStringSeparator)
If SubStringPosition <= UBound(aArray) + 1 Then
GetSubString = aArray(SubStringPosition - 1)
End If
End Function
Public Function GetSubStringAsByte(ByVal MainString As String, ByVal SubStringPosition As Integer, ByVal SubStringSeparator As String) As Byte
Dim TempValue As String
TempValue = GetSubString(MainString, SubStringPosition, SubStringSeparator)
If IsNumeric(TempValue) Then
If Val(TempValue) < 256 Then
GetSubStringAsByte = CByte(TempValue)
Else
GetSubStringAsByte = 0
End If
Else
GetSubStringAsByte = 0
End If
End Function
Public Function RemoveFirstSubString(ByVal MainString As String, ByVal SubStringSeparator As String) As String
Dim aArray() As String
If Len(MainString) > 0 Then
aArray = Split(MainString, SubStringSeparator)
RemoveFirstSubString = Mid(MainString, Len(aArray(LBound(aArray))) + 2)
End If
End Function
Public Function RemoveLastSubString(ByVal MainString As String, ByVal SubStringSeparator As String) As String
Dim aArray() As String
If Len(MainString) > 0 Then
aArray = Split(MainString, SubStringSeparator)
If UBound(aArray) > 0 Then
RemoveLastSubString = Left(MainString, Len(MainString) - Len(aArray(UBound(aArray))) - 1)
Else
RemoveLastSubString = MainString
End If
End If
End Function
Public Function CountSubString(ByVal MainString As String, ByVal SubStringSeparator As String) As String
Dim aArray() As String
aArray = Split(MainString, SubStringSeparator)
CountSubString = UBound(aArray) + 1
End Function
Public Function GetSubStringValue(ByVal MainString As String, ByVal SubStringSeparator As String, ByVal ValueSeparator As String, ByVal ValueName As String) As String
Dim aArray() As String
Dim ValueIndex As Integer
Dim CurrentValueName As String
aArray = Split(MainString, SubStringSeparator)
For ValueIndex = 0 To UBound(aArray)
CurrentValueName = GetSubString(aArray(ValueIndex), 1, ValueSeparator)
If CurrentValueName = ValueName Then
GetSubStringValue = GetSubString(aArray(ValueIndex), 2, ValueSeparator)
Exit For
End If
Next ValueIndex
End Function
Public Function GetBooleanString(ByVal Value As Boolean) As String
GetBooleanString = IIf(Value, "Sí", "No")
End Function
Public Function GetBooleanValueFromString(ByVal Value As String) As Boolean
GetBooleanValueFromString = (Value = "Sí")
End Function
Public Function CleanInvalidCharsByAllowed(ByVal Value As String, ByVal AllowedChars As String) As String
Dim CharIndex As Long
For CharIndex = 1 To Len(Value)
If InStr(1, AllowedChars, Mid(Value, CharIndex, 1)) > 0 Then
CleanInvalidCharsByAllowed = CleanInvalidCharsByAllowed & Mid(Value, CharIndex, 1)
End If
Next CharIndex
End Function
Public Function CleanInvalidCharsByNotAllowed(ByVal Value As String, ByVal NotAllowedChars As String) As String
Dim CharIndex As Long
For CharIndex = 1 To Len(Value)
If InStr(1, NotAllowedChars, Mid(Value, CharIndex, 1)) = 0 Then
CleanInvalidCharsByNotAllowed = CleanInvalidCharsByNotAllowed & Mid(Value, CharIndex, 1)
End If
Next CharIndex
End Function
Public Function CleanNotNumericChars(ByVal Value As String) As String
CleanNotNumericChars = CleanInvalidCharsByAllowed(Value, "0123456789")
End Function
Public Function CleanInvalidSpaces(ByVal Value As String) As String
CleanInvalidSpaces = Trim(Value)
Do While InStr(1, CleanInvalidSpaces, " ") > 0
CleanInvalidSpaces = Replace(CleanInvalidSpaces, " ", " ")
Loop
End Function
Public Function CleanNullChars(ByVal Value As String) As String
CleanNullChars = Value
Do While InStr(1, CleanNullChars, vbNullChar) > 0
CleanNullChars = Replace(CleanNullChars, vbNullChar, "")
Loop
End Function
Public Function ConvertCurrencyToVBNumber(ByVal Value As Currency) As String
ConvertCurrencyToVBNumber = Replace(Format(Value), pRegionalSettings.CurrencyDecimalSymbol, ".")
End Function
Public Function ConvertDoubleToVBNumber(ByVal Value As Double) As String
ConvertDoubleToVBNumber = Replace(Format(Value), pRegionalSettings.NumberDecimalSymbol, ".")
End Function
Public Function FormatDoubleToString_NoGrouping_DotAsDecimal(ByVal Value As Double, ByVal FormatExpression As String) As String
FormatDoubleToString_NoGrouping_DotAsDecimal = Replace(Replace(Format(Value, FormatExpression), pRegionalSettings.NumberDigitGroupingSymbol, ""), pRegionalSettings.NumberDecimalSymbol, ".")
End Function
Public Function FormatDoubleToString_NoGrouping_CommaAsDecimal(ByVal Value As Double, ByVal FormatExpression As String) As String
FormatDoubleToString_NoGrouping_CommaAsDecimal = Replace(Replace(Format(Value, FormatExpression), pRegionalSettings.NumberDigitGroupingSymbol, ""), pRegionalSettings.NumberDecimalSymbol, ",")
End Function
Public Function GetStringExtentInPixels(ByVal hdc As Long, ByVal Value As String) As Long
Dim TextSize As POINTAPI
Dim lngResult As Long
lngResult = GetTextExtentPoint32(hdc, Value, Len(Value), TextSize)
If lngResult <> 0 Then
GetStringExtentInPixels = TextSize.x
End If
End Function
Public Function RemoveCharsAfterNull(ByVal Value As String) As String
Dim FirstNullPosition As Long
FirstNullPosition = InStr(1, Value, vbNullChar)
If FirstNullPosition > 0 Then
RemoveCharsAfterNull = Left(Value, FirstNullPosition - 1)
Else
RemoveCharsAfterNull = Value
End If
End Function
Public Function PadString(ByVal Value As String, ByVal FillerComplete As String, ByVal FillLeft As Boolean, ByVal FillRight As Boolean, Optional ByVal TrimString As Boolean = True) As String
If Len(Value) >= Len(FillerComplete) Then
If TrimString Then
If FillLeft And FillRight Then
'CENTER
PadString = Mid(Value, ((Len(FillerComplete) - Len(Value)) \ 2), ((Len(FillerComplete) - Len(Value)) \ 2) + IIf((Len(FillerComplete) - Len(Value)) Mod 2 > 0, 1, 0))
ElseIf FillLeft Then
'LEFT
PadString = Right(Value, Len(FillerComplete))
ElseIf FillRight Then
'RIGHT
PadString = Left(Value, Len(FillerComplete))
Else
'NONE
PadString = Value
End If
Else
'DON'T TRIM
PadString = Value
End If
Else
If FillLeft And FillRight Then
'CENTER
PadString = Left(FillerComplete, (Len(FillerComplete) - Len(Value)) \ 2) & Value & Right(FillerComplete, ((Len(FillerComplete) - Len(Value)) \ 2) + IIf((Len(FillerComplete) - Len(Value)) Mod 2 > 0, 1, 0))
ElseIf FillLeft Then
'LEFT
PadString = Left(FillerComplete, Len(FillerComplete) - Len(Value)) & Value
ElseIf FillRight Then
'RIGHT
PadString = Value & Right(FillerComplete, Len(FillerComplete) - Len(Value))
Else
'NONE
PadString = Value
End If
End If
End Function
Public Function PadStringLeft(ByVal Value As String, ByVal FillerChar As String, ByVal CharCount As Integer) As String
PadStringLeft = PadString(Value, String(CharCount, FillerChar), True, False)
End Function
Public Function PadStringRight(ByVal Value As String, ByVal FillerChar As String, ByVal CharCount As Integer) As String
PadStringRight = PadString(Value, String(CharCount, FillerChar), False, True)
End Function
Public Function PadStringCenter(ByVal Value As String, ByVal FillerChar As String, ByVal CharCount As Integer) As String
PadStringCenter = PadString(Value, String(CharCount, FillerChar), True, True)
End Function
Public Function ConvertCurrencyToWords(ByVal Importe As Currency) As String
If Importe < 0 Then
Importe = Abs(Importe)
ConvertCurrencyToWords = "Menos "
Else
ConvertCurrencyToWords = ""
End If
ConvertCurrencyToWords = ConvertCurrencyToWords & ConvertIntegerToWords(Int(Importe)) & " con " & Format(((Importe - Int(Importe)) * 100), "00") & "/100"
End Function
Public Function ConvertIntegerToWords(ByVal Number As Long) As String
Select Case Number
Case 0
ConvertIntegerToWords = ""
Case Is < 0
ConvertIntegerToWords = "menor que cero"
Case 1
ConvertIntegerToWords = "uno"
Case 2
ConvertIntegerToWords = "dos"
Case 3
ConvertIntegerToWords = "tres"
Case 4
ConvertIntegerToWords = "cuatro"
Case 5
ConvertIntegerToWords = "cinco"
Case 6
ConvertIntegerToWords = "seis"
Case 7
ConvertIntegerToWords = "siete"
Case 8
ConvertIntegerToWords = "ocho"
Case 9
ConvertIntegerToWords = "nueve"
Case 10
ConvertIntegerToWords = "diez"
Case 11
ConvertIntegerToWords = "once"
Case 12
ConvertIntegerToWords = "doce"
Case 13
ConvertIntegerToWords = "trece"
Case 14
ConvertIntegerToWords = "catorce"
Case 15
ConvertIntegerToWords = "quince"
Case 16
ConvertIntegerToWords = "dieciseis"
Case 17
ConvertIntegerToWords = "diecisiete"
Case 18
ConvertIntegerToWords = "dieciocho"
Case 19
ConvertIntegerToWords = "diecinueve"
Case 20
ConvertIntegerToWords = "veinte"
Case 21 To 29
ConvertIntegerToWords = "veinti" + ConvertIntegerToWords(Number - 20)
Case 30
ConvertIntegerToWords = "treinta"
Case 31 To 39
ConvertIntegerToWords = "treinta y " + ConvertIntegerToWords(Number - 30)
Case 40
ConvertIntegerToWords = "cuarenta"
Case 41 To 49
ConvertIntegerToWords = "cuarenta y " + ConvertIntegerToWords(Number - 40)
Case 50
ConvertIntegerToWords = "cincuenta"
Case 51 To 59
ConvertIntegerToWords = "cincuenta y " + ConvertIntegerToWords(Number - 50)
Case 60
ConvertIntegerToWords = "sesenta"
Case 61 To 69
ConvertIntegerToWords = "sesenta y " + ConvertIntegerToWords(Number - 60)
Case 70
ConvertIntegerToWords = "setenta"
Case 71 To 79
ConvertIntegerToWords = "setenta y " + ConvertIntegerToWords(Number - 70)
Case 80
ConvertIntegerToWords = "ochenta"
Case 81 To 89
ConvertIntegerToWords = "ochenta y " + ConvertIntegerToWords(Number - 80)
Case 90
ConvertIntegerToWords = "noventa"
Case 91 To 99
ConvertIntegerToWords = "noventa y " + ConvertIntegerToWords(Number - 90)
Case 100
ConvertIntegerToWords = "cien"
Case 101 To 199
ConvertIntegerToWords = "ciento " + ConvertIntegerToWords(Number - 100)
Case 200
ConvertIntegerToWords = "doscientos"
Case 201 To 299
ConvertIntegerToWords = "doscientos " + ConvertIntegerToWords(Number - 200)
Case 300
ConvertIntegerToWords = "trescientos"
Case 301 To 399
ConvertIntegerToWords = "trescientos " + ConvertIntegerToWords(Number - 300)
Case 400
ConvertIntegerToWords = "cuatrocientos"
Case 401 To 499
ConvertIntegerToWords = "cuatrocientos " + ConvertIntegerToWords(Number - 400)
Case 500
ConvertIntegerToWords = "quinientos"
Case 501 To 599
ConvertIntegerToWords = "quinientos " + ConvertIntegerToWords(Number - 500)
Case 600
ConvertIntegerToWords = "seiscientos"
Case 601 To 699
ConvertIntegerToWords = "seiscientos " + ConvertIntegerToWords(Number - 600)
Case 700
ConvertIntegerToWords = "setecientos"
Case 701 To 799
ConvertIntegerToWords = "setecientos " + ConvertIntegerToWords(Number - 700)
Case 800
ConvertIntegerToWords = "ochocientos"
Case 801 To 899
ConvertIntegerToWords = "ochocientos " + ConvertIntegerToWords(Number - 800)
Case 900
ConvertIntegerToWords = "novecientos"
Case 901 To 999
ConvertIntegerToWords = "novecientos " + ConvertIntegerToWords(Number - 900)
Case 1000
ConvertIntegerToWords = "un mil"
Case 1001 To 1999
ConvertIntegerToWords = "un mil " + ConvertIntegerToWords(Number Mod 1000)
Case 2000 To 1000000
ConvertIntegerToWords = ConvertIntegerToWords(Int(Number / 1000)) + " mil " + ConvertIntegerToWords(Number Mod 1000)
Case 1000000 To 2000000
ConvertIntegerToWords = "un millon " + ConvertIntegerToWords(Number - 1000000)
Case 2000000 To 1E+20
ConvertIntegerToWords = ConvertIntegerToWords(Int(Number / 1000000)) + " millones " + ConvertIntegerToWords(Number Mod 1000000)
End Select
End Function
Public Function ConvertDelimitedStringToCollection(ByVal DelimitedString As String, ByVal SubStringSeparator As String) As Collection
Set ConvertDelimitedStringToCollection = ConvertArrayToCollection(Split(DelimitedString, SubStringSeparator))
End Function
Public Function ConvertArrayToCollection(ByVal aArray As Variant) As Collection
Dim Index As Integer
If IsArray(aArray) Then
Set ConvertArrayToCollection = New Collection
If UBound(aArray) > -1 Then
For Index = 0 To UBound(aArray)
ConvertArrayToCollection.Add aArray(Index)
Next Index
End If
End If
End Function
Public Function FormatStringToSQL(ByVal Value As String, Optional ConvertEmptyToNull As Boolean = False) As String
If Trim(Value) = "" And ConvertEmptyToNull Then
FormatStringToSQL = "NULL"
Else
FormatStringToSQL = "'" & ReplaceQuote(Value) & "'"
End If
End Function
Public Function FormatIntegerToSQL(ByVal Value As Long, Optional ConvertZeroToNull As Boolean = False) As String
If Value = 0 And ConvertZeroToNull Then
FormatIntegerToSQL = "NULL"
Else
FormatIntegerToSQL = Value
End If
End Function
Public Function FormatDecimalToSQL(ByVal Value As Single, Optional ConvertZeroToNull As Boolean = False) As String
If Value = 0 And ConvertZeroToNull Then
FormatDecimalToSQL = "NULL"
Else
FormatDecimalToSQL = Replace(Value, pRegionalSettings.NumberDecimalSymbol, ".")
End If
End Function
Public Function FormatCurrencyToSQL(ByVal Value As Currency, Optional ConvertZeroToNull As Boolean = False) As String
If Value = 0 And ConvertZeroToNull Then
FormatCurrencyToSQL = "NULL"
Else
FormatCurrencyToSQL = Replace(Value, pRegionalSettings.NumberDecimalSymbol, ".")
End If
End Function
Public Function FormatDateTimeToSQL(ByVal Value As Date, Optional ConvertEmptyToNull As Boolean = False) As String
If Value = DATE_TIME_FIELD_NULL_VALUE And ConvertEmptyToNull Then
FormatDateTimeToSQL = "NULL"
Else
FormatDateTimeToSQL = "'" & Format(Value, "yyyy/mm/dd hh:nn") & "'"
End If
End Function
Public Function FormatBooleanToSQL(ByVal Value As Boolean) As String
If Value Then
FormatBooleanToSQL = "1"
Else
FormatBooleanToSQL = "2"
End If
End Function
Public Function GetOLEColorFromString(ByVal CommaSeparatedValue As String) As OLE_COLOR
Dim ColorComponentRed As Byte
Dim ColorComponentGreen As Byte
Dim ColorComponentBlue As Byte
ColorComponentRed = GetSubStringAsByte(CommaSeparatedValue, 1, ",")
ColorComponentGreen = GetSubStringAsByte(CommaSeparatedValue, 2, ",")
ColorComponentBlue = GetSubStringAsByte(CommaSeparatedValue, 3, ",")
GetOLEColorFromString = RGB(ColorComponentRed, ColorComponentGreen, ColorComponentBlue)
End Function