PDA

View Full Version : Numeric textbox


R.Amin
02-16-04, 08:22 AM
Hi there i'm really stuck! I have created two text boxes which store numeric values, e.g 1000! The value of the text of textbox1 must be less than the value of text2 or a error message occurs when i click the save button The user though should be able to press "k", "m" or "b" after a number so to turn it to the corresponding value e.g 1k = 1000! The format must also change so that 1000 = 1,000 0r 1000000 = 1,000,000 etc! I've been able to sort these problems out, although now when i click from one textbox to the other, the number in the textbox i have just left changes. so for example if the user enters 23455 the format then changes to 23,455 when the texbox loses focus which iscorrect, but then if the user clicks back in and then out of the same textbox the value changes to 23! What can i do?? Please help anyone!!

the coding is below!!

Option Explicit

Const billion As Double = 1000000000
Const million As Double = 1000000
Const thousand As Double = 1000

Dim bnumber As Double
Dim mnumber As Double
Dim knumber As Double
Dim bbnumber As Double


Private Sub Command1_Click()

Dim dtext1 As Double
Dim dtext2 As Double

dtext1 = Text1.Text
dtext2 = Text2.Text

If Val(dtext1) > Val(dtext2) Then
MsgBox "Error"
End If
End Sub

Private Sub Text1_LostFocus()

bnumber = Val(Text1.Text)
bnumber = bnumber * billion

mnumber = Val(Text1.Text)
mnumber = mnumber * million

knumber = Val(Text1.Text)
knumber = knumber * thousand

If Right$(Text1.Text, 1) = "b" Then
Text1.Text = Format(Val(bnumber), "#,###,###,###")
Else
If Len(Text1.Text) >= 10 Then
Text1.Text = Format(Val(Text1.Text), "#,###,###,###")
ElseIf Right$(Text1.Text, 1) = "m" Then
Text1.Text = Format(Val(mnumber), "#,###,###")
ElseIf Len(Text1.Text) <= 9 And Len(Text1.Text) >= 7 Then
Text1.Text = Format(Val(Text1.Text), "#,###,###")
ElseIf Right$(Text1.Text, 1) = "k" Then
Text1.Text = Format(Val(knumber), "#,###")
ElseIf Len(Text1.Text) <= 6 And Len(Text1.Text) >= 4 Then
Text1.Text = Format(Val(Text1.Text), "#,###")
End If
End If



End Sub

Private Sub Text2_LostFocus()

bnumber = Val(Text2.Text)
bnumber = bnumber * billion


mnumber = Val(Text2.Text)
mnumber = mnumber * million

knumber = Val(Text2.Text)
knumber = knumber * thousand


If Right$(Text2.Text, 1) = "b" Then
Text2.Text = Format(Val(bnumber), "#,###,###,###")
Else
If Len(Text2.Text) >= 10 Then
Text2.Text = Format(Val(Text2.Text), "#,###,###,###")
ElseIf Right$(Text2.Text, 1) = "m" Then
Text2.Text = Format(Val(mnumber), "#,###,###")
ElseIf Len(Text2.Text) <= 9 And Len(Text2.Text) >= 7 Then
Text2.Text = Format(Val(Text2.Text), "#,###,###")
ElseIf Right$(Text2.Text, 1) = "k" Then
Text2.Text = Format(Val(knumber), "#,###")
ElseIf Len(Text2.Text) <= 6 And Len(Text2.Text) >= 4 Then
Text2.Text = Format(Val(Text2.Text), "#,###")
End If
End If

End Sub

hyjacked
02-16-04, 05:35 PM
it sounds like when you go back into the textbox the second time, it is taking 23,455 as a string, so when it converts it back to a number, it is losing everything after the comma, perhaps, when you enter the textbox convert the number back to an int, so that when you leave it again it has a proper number to convert.

hope this helps.