Bonjour, ayant terminé mon appli Access, je bosse actuellement à l'optimisation du code.
Pour faire mes contrôles de saisie, j'ai fait une fonction ControleSaisie et une fonction EstVide (la première appelle la seconde) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 Function EstVide(controle As Control) As Boolean If IsNull(controle) Or controle = "" Then EstVide = True Else EstVide = False End If End FunctionJe fais ensuite mon contrôle de saisie sur mes contrôles de cette façon :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Function ControleSaisie(controle As Control, Optional nul As Boolean) As Boolean ' Le paramètre nul indique si le contrôle peut ne pas être rempli Dim rep As Integer If EstVide(controle) And nul = False Then rep = MsgBox(controle.ValidationText, vbOKOnly + vbCritical, "Information manquante...") controle.SetFocus ControleSaisie = False Else Select Case controle.StatusBarText Case "Nombre": If Not IsNumeric(controle) Then rep = MsgBox("Veuillez saisir un numéro valide", vbOKOnly + vbCritical, "Saisie incorrecte...") controle.SetFocus ControleSaisie = False Else: ControleSaisie = True End If Case "Date": If Not IsDate(controle) And Not EstVide(controle) Then rep = MsgBox("Veuillez saisir une date valide", vbOKOnly + vbCritical, "Saisie incorrecte...") controle.SetFocus ControleSaisie = False Else: ControleSaisie = True End If Case Else: ControleSaisie = True End Select End If End Function
Peut-on optimiser cela ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 If ControleSaisie(Me.txtNumCli) = False Then Exit Sub End If If ControleSaisie(Me.txtnumSIAC) = False Then Exit Sub End If If ControleSaisie(Me.txtDateNaiss) = False Then Exit Sub End If If ControleSaisie(Me.txtNom) = False Then Exit Sub End If If ControleSaisie(Me.txtPrenom) = False Then Exit Sub End If If ControleSaisie(Me.txtDateExam, True) = False Then Exit Sub End If If ControleSaisie(Me.cboJustif) = False Then Exit Sub End If If ControleSaisie(Me.txtDernJustif, True) = False Then Exit Sub End If If ControleSaisie(Me.txtDateCloture, True) = False Then Exit Sub End If
Partager