Mollethewizard
IS-IT--Management
I’ve got a user form with textboxes where the users should put in amounts. In Sweden we divide the amounts in groups of 3. For example 1000 we write 1 000 and 78500 should be written 78 500. I’ve managed to write code that takes care of the spaces between the groups when the user exits the textboxes. Just to prevent the user from writing the amounts in the wrong order (with dots, commas etc.) the textboxes only accepts entered numbers.
A problem occurs when a user discovers that he has entered the wrong amount in a textbox further up and back-tabs to the textbox he wants to correct. The textboxes he back-tabs trough who has amounts, lets say, 1 234 or 12 345 or 123 456 etc. will have extra spaces.
Is there a way to increase "the spacing code" so the extra spaces don’t occur? I’ve tried with comparison of the string with “Like” but I haven’t managed to get it to work.
Preventing code:
Private Sub txtBel12_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Exit code:
Private Sub txtBel12_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Kronkoll = Me.txtBel12
Call Krondel
Me.txtBel12 = Kronor
“The spacing code” I suppose there is a smarter way to write this code too?
Sub Krondel()
If Len(Kronkoll) = 4 Then
Kr1 = Left(Kronkoll, 1)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & Kr2
ElseIf Len(Kronkoll) = 5 Then
Kr1 = Left(Kronkoll, 2)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & Kr2
ElseIf Len(Kronkoll) = 6 Then
Kr1 = Left(Kronkoll, 3)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & Kr2
ElseIf Len(Kronkoll) = 7 Then
Kr1 = Left(Kronkoll, 1)
KrMitt = Mid(Kronkoll, 2, 3)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & KrMitt & " " & Kr2
ElseIf Len(Kronkoll) = 8 Then
Kr1 = Left(Kronkoll, 2)
KrMitt = Mid(Kronkoll, 3, 3)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & KrMitt & " " & Kr2
ElseIf Len(Kronkoll) = 9 Then
Kr1 = Left(Kronkoll, 3)
KrMitt = Mid(Kronkoll, 4, 3)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & KrMitt & " " & Kr2
Else: Kronor = Kronkoll
End If
End Sub
Christer
A problem occurs when a user discovers that he has entered the wrong amount in a textbox further up and back-tabs to the textbox he wants to correct. The textboxes he back-tabs trough who has amounts, lets say, 1 234 or 12 345 or 123 456 etc. will have extra spaces.
Is there a way to increase "the spacing code" so the extra spaces don’t occur? I’ve tried with comparison of the string with “Like” but I haven’t managed to get it to work.
Preventing code:
Private Sub txtBel12_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Exit code:
Private Sub txtBel12_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Kronkoll = Me.txtBel12
Call Krondel
Me.txtBel12 = Kronor
“The spacing code” I suppose there is a smarter way to write this code too?
Sub Krondel()
If Len(Kronkoll) = 4 Then
Kr1 = Left(Kronkoll, 1)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & Kr2
ElseIf Len(Kronkoll) = 5 Then
Kr1 = Left(Kronkoll, 2)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & Kr2
ElseIf Len(Kronkoll) = 6 Then
Kr1 = Left(Kronkoll, 3)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & Kr2
ElseIf Len(Kronkoll) = 7 Then
Kr1 = Left(Kronkoll, 1)
KrMitt = Mid(Kronkoll, 2, 3)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & KrMitt & " " & Kr2
ElseIf Len(Kronkoll) = 8 Then
Kr1 = Left(Kronkoll, 2)
KrMitt = Mid(Kronkoll, 3, 3)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & KrMitt & " " & Kr2
ElseIf Len(Kronkoll) = 9 Then
Kr1 = Left(Kronkoll, 3)
KrMitt = Mid(Kronkoll, 4, 3)
Kr2 = Right(Kronkoll, 3)
Kronor = Kr1 & " " & KrMitt & " " & Kr2
Else: Kronor = Kronkoll
End If
End Sub
Christer