|
-
May 6th, 2010, 03:17 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Hide decimals in text box??
I'm wondering if there is a way to show a decimal (example 100.52) as 100 in a text box without losing the .52 cents? Kind of like in Excel how you can have a decimal in a cell but format it to not show the cents... but when you sum() the cells, the cents still add up.
I hope that made sense...
Any ideas?
-
May 6th, 2010, 03:43 PM
#2
Re: Hide decimals in text box??
On entering text do something like this:
Text1.Tag = Text1.Text
Text1.Text = Int(Text1.Text)
MsgBox Text1.Tag
Tag will hold the full value and text will hold the rounded (integer) value. You can also use the Round function to customize rounding in a different way if you need. But you would also need error checking because if you entered text ("asdf" for example) before running the code you would get an error.
-
May 6th, 2010, 04:37 PM
#3
Re: Hide decimals in text box??
So:
vb Code:
If IsNumeric(text1.text) Then
Text1.Tag = Text1.Text
Text1.Text = Int(Text1.Text)
else
Msgbox "Where I come from " & text1.text & " is not a number!"
if isnumeric(text1.tag) then
text1.text = text1.tag
else
text1.text = "0"
end if
end if
-
May 6th, 2010, 08:48 PM
#4
Re: Hide decimals in text box??
Build a form with a list box, a label, and a text box that will show the correctly rounded number. Then click the list box to show the rounded number in the text box.
Code:
Private Sub Form_Load()
Dim MyNum As Single, Sum As Single
Randomize
For I = 1 To 100
MyNum = Rnd * 500
List1.AddItem Format$(MyNum, "###.##")
Sum = Sum + MyNum
Next
Label1.Caption = Format$(Sum, "##,###.###")
List1.ListIndex = 0
End Sub
Private Sub List1_Click()
Text1.Text = Format$(List1.List(List1.ListIndex), "###")
End Sub
The label shows the exact sum of all the numbers in the text box, rounded to three digits to the right. I rounded the list box values to two digits to the right.
-
May 7th, 2010, 01:37 AM
#5
Re: Hide decimals in text box??
Perhaps this is simpler:
Code:
Private Sub Form_Load()
Text1.Tag = Text1.Text
End Sub
Private Sub Text1_GotFocus()
If Text1.Tag <> "" Then Text1.Text = Text1.Tag
End Sub
Private Sub Text1_LostFocus() '<<<=== check syntax
With Text1
If .Tag <> .Text Then .Tag = .Text
'-- round it if .Text is a number
'-- otherwise the next line below has no effect
.Text = Format$(.Text, "0")
End With
End Sub
On "summing", use .Tag instead of .Text
-
May 7th, 2010, 08:22 AM
#6
Thread Starter
Hyperactive Member
Re: Hide decimals in text box??
Thank you for the advice. I have never used .Tag before so I will give it a try. It sounds like that is what I am looking for.
Thank you everyone!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|