adrienr11vdv
Messages postés99Date d'inscriptionjeudi 13 juillet 2006StatutMembreDernière intervention16 avril 2009
-
14 mars 2007 à 10:43
adrienr11vdv
Messages postés99Date d'inscriptionjeudi 13 juillet 2006StatutMembreDernière intervention16 avril 2009
-
14 mars 2007 à 11:49
bonjour à tous,
Dans mon projet, il y a plusieurs TextBox où on ne peut saisir que des nombres et la virgule.
Private Sub txt_note_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 44 Then
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
End If
End If
End Sub
voici le code qui marche très bien.
Comme il y a plusieurs TextBox, j'aimerai mettre ce fragment de code dans une procédure. mais lorsque je le fais, le code si dessus ne fonctionne plus.
Voici ma procédure:
Public Sub numerique()
If KeyAscii <> 8 And KeyAscii <> 44 Then
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
End If
End If
End Sub
Private Sub txt_note_KeyPress(KeyAscii As Integer)
drikce06
Messages postés2236Date d'inscriptionlundi 29 mai 2006StatutMembreDernière intervention29 mai 200810 14 mars 2007 à 11:39
Ce bout de code vérifie si c'est une valeur numérique, tiens compte du séparateur de décimale et si il y a déjà un séparateur dans la chaîne.
Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCTYPE As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCTYPE As Long, ByVal lpLCData As String) As Long
Private Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
'Déclaration de la constante séparateur décimal
Private Const LOCALE_SDECIMAL = &HE
Dim Separateur As String
Public Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Public Sub numerique(ByVal KeyAscii As MSForms.ReturnInteger, ByVal Text As String)
If Not IsNumeric(Chr(KeyAscii)) And CStr(Chr(KeyAscii)) <> Separateur Then KeyAscii = 0If Chr(KeyAscii) Separateur And InStr(1, Text, Separateur, vbTextCompare) 2 Then KeyAscii = 0
End Sub
Public Property Get DecimalSeparator() As String
Dim nLength As Long
Dim nLocale As Long
nLocale = GetUserDefaultLCID()
nLength = GetLocaleInfo(nLocale, LOCALE_SDECIMAL, vbNullString, 0) - 1
DecimalSeparator = Space$(nLength)
GetLocaleInfo nLocale, LOCALE_SDECIMAL, DecimalSeparator, nLength
End Property
Drikce 06
Si la réponse vous convient: Réponse acceptée. Si la réponse vous convient pas: