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
| Function AutoNumber_abo_prepa_n_facture( _
ByVal T_abo_vendu_cumul As String, _
ByVal N_facture_abo 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 AutoNumber_abo_prepa_n_factureErr
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()
ACH_N_facture = "[" & N_facture_abo & "]"
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 = N_facture_abo & " LIKE '" & strFormat & "*'"
strNum = Nz(DMax(N_facture_abo, T_abo_vendu_cumul, 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_abo_prepa_n_facture = strFormat
Exit Function
AutoNumber_abo_prepa_n_factureErr:
MsgBox "Erreur : " & Err.Description, vbCritical
AutoNumber_abo_prepa_n_facture = ""
Exit Function
End Function |
Partager