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
| ' ---
' NUMEROTATION AUTOMATIQUE PERSONNALISEE
' ---
' Entrée : strTable <- Nom de la table - Remplacé par T_Base_Affaires
' strField <- Nom du champ contenant le numéro - Remplacé par Numero_Proposition
' strFormat <- Gabarit décrivant comment formater
' le numéro.
' intDigits <- Nombre de caractères pour le
' numéro proprement dit.
' dtDate <- Date de référence pour le calcul
' de l'année, du mois...
'
Function AutoNumber( _
ByVal T_Base_Affaires As String, _
ByVal Numero_Proposition As String, _
Optional ByVal strFormat As String = "", _
Optional ByVal intDigits As Integer = 4, _
Optional ByVal dtDate As Date = #1/1/100#)
' Quelques variables...
On Error GoTo AutoNumberErr
Dim varMarkers As Variant, varMark As Variant
Dim strCriteria As String
Dim strNum As String, lngNum As Long, strPart As String
' Quelques retraitements...
If dtDate = #1/1/100# Then dtDate = Now()
strField = "[" & strField & "]"
strFormat = Replace(strFormat, "'", "''")
' Marqueurs à remplacer
varMarkers = Array("YYYY", "YY", "Q", "MM", "WW", "DD")
For Each varMark In varMarkers
' Formater la date et l'injecter dans le template
strPart = Format(dtDate, varMark, vbMonday, vbFirstFourDays)
strFormat = Replace(strFormat, "[" & varMark & "]", _
Format(strPart, String(Len(varMark), "0")))
Next
' On cherche la valeur maximale déjà employée dans la table
strCriteria = strField & " LIKE '" & strFormat & "*'"
strNum = Nz(DMax(strField, strTable, strCriteria), "")
' On crée le nouveau numéro
lngNum = IIf(strNum = "", 1, Val(Mid(strNum, Len(strFormat) + 1)) + 1)
strFormat = strFormat & Format(lngNum, String(intDigits, "0"))
' Valeur finale
AutoNumber = strFormat
Exit Function |
Partager