Results 1 to 6 of 6

Thread: [RESOLVED] Hide decimals in text box??

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Resolved [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?

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  3. #3
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Hide decimals in text box??

    So:

    vb Code:
    1. If IsNumeric(text1.text) Then
    2.   Text1.Tag = Text1.Text
    3.   Text1.Text = Int(Text1.Text)
    4. else
    5.   Msgbox "Where I come from " & text1.text & " is not a number!"
    6.   if isnumeric(text1.tag) then
    7.     text1.text = text1.tag
    8.   else
    9.     text1.text = "0"
    10.   end if
    11. end if

  4. #4
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    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
  •  



Click Here to Expand Forum to Full Width