USERFORM met
TextBox1 "kommaLabel" TextBox2
CommandButton1
Option Explicit
Private Sub CommandButton1_Click()
MsgBox (Val(TextBox1) + Val(TextBox2) / 10 ^ Len(TextBox2))
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1 = Val(TextBox1) '0123-->123 ""-->0
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If check_keycode(KeyAscii, TextBox1) = False Then KeyAscii = 0
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If check_keycode(KeyAscii, TextBox2) = False Then KeyAscii = 0
End Sub
Private Sub UserForm_Initialize()
TextBox1.MaxLength = 10
TextBox2.MaxLength = 4
End Sub
Private Function check_keycode(KeyAscii As ReturnInteger, ThisTextBox As MSForms.TextBox) As Boolean
Dim DS As Long
DS = Asc(Application.DecimalSeparator)
DS = Asc(".")
'DS = vbKeyDecimal 'doesn't seem to work
Select Case KeyAscii
Case DS
If Not InStr(TextBox1, Chr(DS)) Then check_keycode = True
Case 48 To 57
check_keycode = True
Case Else
End Select
With ThisTextBox
If .MaxLength = Len(.Text) Then MsgBox "Maximum number of characters = " & .MaxLength, vbCritical, "ERROR"
End With
End Function
De code bevat het instellen van "MaxLength": voor de komma 10 cijfers, na de komma 4.
Bijlagen:
"numeric input UF 1.jpg"
deel van UserForm
"Calculate"knop, vermenigvuldigt met 5 en plaatst het resultaat in een label. (in bovenstaande code vervangen door regel met MsgBox)
"numeric input UF 2.jpg"
Een "life"voorbeeld uit mijn facturatiemodel. Hier zijn geen kommalabels ingevoegd. De mensen weten vanzelf hoe het werkt :-)
beste groeten,
Erik