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
| Private Sub Commande29_Click()
Function NextID(n°_facture As String, tble_facture As String) As Long
'-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
'Fonction renvoyant le prochain Identifiant, en fonction des valeurs existantes dans une table.
'Arguments :
' LeChamp => Nom du champ Identifiant numérique Long concerné
' LaTable => Nom de la table contenant cet identifiant
'Retour :
' 1 s'il n'y a rien dans la table
' Le Nombre maximum +1 s'il y a une suite ininterrompue de nombres
' La valeur du nombre manquant en cas de trou.
'-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Dim sSQL As String
Dim rs As DAO.Recordset
Dim n As Long
'Chaîne SQL en fonction de n°_facture et de tble_facture, retournant NULL ou le numéro du trou
sSQL = "Select Min([" & n°_facture & "]-1) As NextID From " & tble_facture & " As T1 "
sSQL = sSQL & "Where ((([" & n°_facture & "]-1)>0) And (((Select [" & n°_facture & "] "
sSQL = sSQL & "From " & tble_facture & " T2 "
sSQL = sSQL & "Where T2.[" & n°_facture & "]=T1.[" & n°_facture & "]-1)) Is Null));"
Set rs = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)
'Nbre d'enregistrements dans tble_facture
n = DCount("[" & n°_facture & "]", "[" & tble_facture & "]")
If n = 0 Then 'S'il n'y a pas d'enregistrements, mettre 1
NextID = 1
ElseIf IsNull(rs(0)) Then 'Si la requête ne renvoie rien, incrémenter de 1 le maximum
NextID = DMax("[" & n°_facture & "]", "[" & tble_facture & "]") + 1
Else
NextID = rs(0) 'Sinon, il y a un trou. Renvoyer la valeur du trou
End If
End Function |