Comment obliger à mettre 4 caractères en textbox

Résolu
mythiac Messages postés 72 Date d'inscription mardi 23 septembre 2008 Statut Membre Dernière intervention 23 février 2009 - 8 janv. 2009 à 13:48
mythiac Messages postés 72 Date d'inscription mardi 23 septembre 2008 Statut Membre Dernière intervention 23 février 2009 - 8 janv. 2009 à 14:07
bonjour,

j'aurais beoins d'aide pour obliger mon utilisateur à remplir dans une textbox 4 chiffres:

voici pour l'instant ce qui me permet de mettre obligatoirement des chiffres, au maximum 4 chiffres, mais pas obligatoirement hélas qu'il y ait 4 caractères.

Private Sub TextBox1_Change()
  On Error Resume Next
    If Not IsNumeric(Right(TextBox1, 1)) Then

        MsgBox "Mettre en 4 chiffres"
        TextBox1 = ""
        Exit Sub

    TextBox1.MaxLength = 4 'nbr caractère max
    TextBox1.AutoTab = True 'autotabulation
End Sub

merci par avance

2 réponses

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
8 janv. 2009 à 14:00
embête pas les gens pendant la saisie...

sinon, y'a plusieurs manière d'aborder la chose...


Private Const ES_NUMBER As Long = &H2000&
Private Const GWL_STYLE As Long = -16

Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Sub Form_Load()
'# Textbox numérique
SetWindowLong Text1.hwnd, GWL_STYLE, GetWindowLong(Text1.hwnd, GWL_STYLE) Or ES_NUMBER
End Sub

Private Sub Text1_Change()
If LenB(Text1.Text) Then
If Not IsNumeric(Text1.Text) Then
Text1.Text = "0"
End If
End If
End Sub

Private Sub Text1_LostFocus()
Me.ValidateControls
End Sub

Private Sub Text1_Validate(Cancel As Boolean)
If Len(Text1.Text) <> 4 Then
Cancel = True
MsgBox "Vous n'avez pas respecté les regles !"
End If
End Sub
3
mythiac Messages postés 72 Date d'inscription mardi 23 septembre 2008 Statut Membre Dernière intervention 23 février 2009
8 janv. 2009 à 14:07
merci
0
Rejoignez-nous