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
| Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = VerifSaisieNombre(KeyAscii, 5, 2, True, , True, False)
End Sub
Public Function VerifSaisieNombre(ByVal KeyAscii As Integer, _
ByVal NombreEntier As Integer, _
ByVal NombreDecimal As Integer, _
ByVal Negatif As Boolean, _
Optional ByRef txtObject As Object = Nothing, _
Optional ByRef TestNbDecimal As Boolean = False, _
Optional ByVal Pourcentage As Boolean = False) As Integer
'Cette fonction gère la saisie des nombres
'
'Paramètres:
' - KeyAscii : (Entrée) Code ascii de la touche
' - NombreEntier : (Entrée) Nombre de chiffres avant le point décimal
' - NombreDecimal : (Entrée) Nombre de chiffres décimaux autorisés.
' - Negatif : (Entrée) Accepte le signe '-'.
' - TxtObject : (Entrée)(Optionnel) Contrôle sur lequel s'effectue la vérification
' Par défaut c'est le contrôle actif de l'application
' Attention, il doit avoir les propriétés SelStart,SelLength et Text
' - Pourcentage : (Entrée) Accepte le signe '%'
'Renvoi:
' le code de la touche qui était en entrée si celui-ci est valide,
' sinon renvoi 0
'
' Cette fonction doit être appelée depuis une procédure événementielle
' de type KeyPress
'
' Exemple d 'appel depuis un TextBox:
' Private Sub txtTpsEffectue_KeyPress(KeyAscii As Integer)
' KeyAscii = VerifSaisieNombre(KeyAscii, 7, 2, True, txtTpsEffectue) '- #######.##
' ou
' KeyAscii = VerifSaisieNombre(KeyAscii, 7, 2, True) '- #######.##
' End Sub
'
'
On Error GoTo VerifSaisieNombre_error
Dim PositSepDec As Integer
Dim Chaine As String
Dim Pos As Integer
Dim ChaineTest As String
If txtObject Is Nothing Then Set txtObject = Screen.ActiveControl
VerifSaisieNombre = KeyAscii
'----------------------------------------------------
30 '-- Traitement du signe "-"
If KeyAscii = Asc("-") And Negatif = False Then
VerifSaisieNombre = 0
Exit Function
End If
' Si déja présent, on l'enléve, sinon on le met
If KeyAscii = Asc("-") Then
40 'Signe "-" déja saisi, on le retire
If InStr(txtObject.Text, "-") > 0 Then
Pos = txtObject.SelStart
txtObject.Text = Replace(txtObject.Text, "-", "")
txtObject.SelStart = IIf(Pos > 0, Pos - 1, 0)
VerifSaisieNombre = 0
Else
50 'Signe "-" non saisi, on le met au debut de la chaine
Pos = txtObject.SelStart
txtObject.Text = "-" & _
Mid(txtObject.Text, 1, txtObject.SelStart) & _
Mid(txtObject.Text, txtObject.SelStart + txtObject.SelLength + 1)
txtObject.SelStart = Pos + 1
VerifSaisieNombre = 0
End If
End If 'KeyAscii = Asc("-")
'L'appui sur "+" supprime le signe "-"
If KeyAscii = Asc("+") Then
60 'On retire le signe "-" s'il existe
If InStr(txtObject.Text, "-") > 0 Then
Pos = txtObject.SelStart
txtObject.Text = Replace(txtObject.Text, "-", "")
txtObject.SelStart = IIf(Pos > 0, Pos - 1, 0)
VerifSaisieNombre = 0
End If
End If
'----------------------------------------------------
130 '-- Traitement du signe '%'
If KeyAscii = Asc("%") Then
If Not Pourcentage Or InStr(txtObject.Text, "%") > 0 Then
VerifSaisieNombre = 0
Exit Function
End If
150 ' Signe '%' non saisi, on le met à la fin de la chaine
Pos = txtObject.SelStart
txtObject.Text = Mid(txtObject.Text, 1, txtObject.SelStart) & _
Mid(txtObject.Text, txtObject.SelStart + txtObject.SelLength + 1) & "%"
txtObject.SelStart = Pos
VerifSaisieNombre = 0
End If
If VerifSaisieNombre = 0 Then Exit Function
'---------------------------------------------------
'-- Traitement du caractère en train d'être saisi
'Valeurs valides par défaut
Chaine = "0123456789" & "." & Chr(vbKeyBack)
70 'Suppression des caractères sélectionnés
ChaineTest = txtObject.Text
If txtObject.SelLength > 0 Then
ChaineTest = Mid(ChaineTest, 1, txtObject.SelStart) & _
Mid(ChaineTest, txtObject.SelStart + txtObject.SelLength + 1)
End If
PositSepDec = InStr(ChaineTest, ".")
If VerifSaisieNombre = Asc(".") Then
80 'Test si l'on peut saisir le point décimal
If NombreDecimal > 0 Then
If PositSepDec > 0 Then
Chaine = "0123456789" & Chr(vbKeyBack)
Else
Chaine = "0123456789" & "." & Chr(vbKeyBack)
End If
Else
Chaine = "0123456789" & Chr(vbKeyBack)
End If
Else
90 'Test la taille des entiers et des décimals
If PositSepDec = 0 Then PositSepDec = Len(txtObject.Text) + 1
If txtObject.SelStart < PositSepDec Then
100 'Test les entiers
If TailleEntier(ChaineTest, ".") >= NombreEntier Then
Chaine = Chr(vbKeyBack)
End If
Else
110 'Test les décimales
If TestNbDecimal = True Then
If TailleDecimal(ChaineTest, ".") >= NombreDecimal Then
Chaine = Chr(vbKeyBack)
End If
End If
End If
End If
120 'Vérifie si le caractère saisi (KeyAscii) est valide (est contenu dans "Chaine")
If InStr(Chaine, Chr(VerifSaisieNombre)) = 0 Then
VerifSaisieNombre = 0: Beep
End If
Exit Function
'Traitement des erreurs
VerifSaisieNombre_error:
VerifSaisieNombre = 0
MsgBox Err.Number & " : " & Err.Description
End Function
Private Function TailleEntier(ByVal txt As String, _
ByVal Separateur As String) As Integer
'Renvoi le nombre d'entier du nombre
'Suppression du signe moins
If InStr(txt, "-") > 0 Then
txt = Left(txt, InStr(txt, "-") - 1) & Mid(txt, InStr(txt, "-") + 1)
End If
'Suppression des chiffres après le point décimal
If InStr(txt, Separateur) > 0 Then
txt = Left(txt, InStr(txt, Separateur) - 1)
End If
TailleEntier = Len(txt)
End Function
Private Function TailleDecimal(ByVal txt As String, _
ByVal Separateur As String) As Integer
'Renvoi le nombre de décimal du nombre
Dim PositSepDec As Integer
PositSepDec = InStr(txt, Separateur)
If PositSepDec > 0 Then
TailleDecimal = Len(Mid(txt, PositSepDec + 1))
Else
TailleDecimal = 0
End If
End Function |
Partager