Results 1 to 10 of 10

Thread: How Format A Textbox For Currency

  1. #1
    Guest
    How to format a textbox for currency without using Maskedit Box. The only way at the moment is read the textbox and write back to the textbox with format function in the coding. This somehow creates some complication in the multiuser environment because the data is bound to a table.
    Because it writes back to the textbox with the currency format "#,###,##0.00" it's actually changing the field.

    Is it possible using API? Or is there any possible way where the format can be set and without have to use Maskedit or Richtextbox.

    Please advise..

    Thanking in advance



  2. #2
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    There are a couple of ways to approach this.
    If possible I recommend using #1 below. It's a lot less work.

    Hope this helps.

    JC.


    1. Update the format after the value is entered and the
    user leaves the control. (You will want to restrict
    the user input to numbers and the decimal point/period
    key in the keypress/keydown events.)

    Code:
    Private Sub Text1_LostFocus()
        Text1.Text = Format(Text1.Text, "$#####0.00")
    End Sub
    2. Format the text as it's being typed by placing the
    formatting code in the keypress event of the text
    box. The following code will create an ATM cash
    machine type of dollar value entry... it automatically
    places the decimal and fills in from right to left.
    There is more code needed to really handle all of the
    user entry possibilities, but this will give you the
    gist of it.

    Code:
    Private Sub txtMoney_KeyPress(KeyAscii As Integer)
        Dim strTemp1 As String
        Dim strTemp2 As String
        Dim strDecimal As String
        Dim strCents as String
        Dim strDollars as String
        
        'Accept BackSpace Key
        If KeyAscii = vbKeyBack Then Exit Sub
        
        'Check which key is pressed
        If Not IsNumeric(Chr(KeyAscii)) Then  
            'If key pressed is not a Numeric
            'Key then discard key and beep.
            KeyAscii = 0   
            Beep
        Else  'If key pressed is a numeric key then process.
            
            'Check for Selected text
            If Len(txtMoney.SelText) > 0 Then txtMoney.SelText = ""
            
            'Limit the length of the number that can be entered to 12 (This is arbitrary)
            If Len(txtMoney.Text) > 12 Then
                KeyAscii = 0
                Exit Sub
            End If
            strTemp1 = txtMoney.Text
            
            'Remove the Decimal Point
            strDecimal = InStr(1, strTemp1, ".")
            
            If strDecimal <> 0 Then
                strTemp2 = Mid(strTemp1, 1, strDecimal - 1) & Mid(strTemp1, _
                            strDecimal + 1, Len(strTemp1))
            Else
                strTemp2 = strTemp1
            End If
    
            'Break out Dollars & Cents
            If strTemp2 <> strTemp1 Then
                strCents = Right(strTemp2, 1)
                strDollars = Left(strTemp2, Len(strTemp2) - 1)
            Else
                strCents = ""
                strDollars = strTemp2
            End If
    
            'Trim Leading Zero off of value if necessary
            If Len(strDollars) > 1 Then
                strDollars = Val(strDollars)
            End If
    
            'ReBuild and Display Amount
            txtMoney.Text = strDollars & "." & strCents
        End If
    End Sub
    JC

  3. #3
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Sudbury, Ontario, Canada
    Posts
    274
    Check out the textbox's dataformat property, if you don't want to use code you could use the currency format.

  4. #4
    Guest

    Thank you all....


    Check out the textbox's dataformat property, if you don't want to use code you could use the currency format.
    I couldn't find any dataformat property setting in the textbox's property box. I'm using VB5 and it don't have the dataformat. Do you know where to get textbox control with a dataformat settings..?

    Thank you...

  5. #5
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Sudbury, Ontario, Canada
    Posts
    274

    DataFormat

    Sorry, I'm using VB6 and haven't used VB5 in a while.

    This doesn't work?

    Private Sub Text1_LostFocus()
    Text1.Text = Format(Text1.Text, "$0.00")
    End Sub

  6. #6
    Guest

    This Works But Not Applicable...


    Private Sub Text1_LostFocus()
    Text1.Text = Format(Text1.Text, "$0.00")
    End Sub
    Thank you dcarlson for helping, I really appreciate it...

    The format sample above works and I'm currently using this way of formatting a currency field. But it's not efficient in a multiuser environment. Everytime I browse a set of records I have to format the currency field in the 'data_reposition' event because the field does not format the value by itself and don't have the dataformat property, the sample above which I'm currently using causes the data to read and write to the field over and over again because the field is a databound field whenever I move from one record to another. Is there any other OCX controls that has the dataformat property...?

    Thank you...



  7. #7
    Guest
    Yeap MS really blew this one. Am using vb5 as well and find this is one of the great weaknesses for business apps. You either use standard textbox with lost focus formating as suggested, or use the masked edit box and arfe frustrated by the lack of a justification property.

    We went out and brought Greentree's masked edit control, and at under a $100oz resolved the issue.

    Since then we got their toolbox as well, which has all sorts of cool things.

    Major problem is lack of documentation, and really lame examples. Any one know if Sheridan's are any better?

  8. #8
    Junior Member
    Join Date
    Mar 2000
    Location
    Montreal, QC
    Posts
    24

    Arrow

    Hey all,

    No offense is intended, but it seems to me like you are all overcomplicating things (Especially you JC). Why not just use
    Code:
        Text1.text = Format(Text1.text, "Currency")
    This has always worked for me. Good luck!

    JMik

  9. #9
    Guest
    Thank you all...

    Thank you for your comments JMik...

    but if you can read thru my earlier posted messages this method is not applicable in a multiuser environment because it's actually writing back to textbox's text which means it's writing to the database's table field in which the textbox is bound to a data control.

    Thanks Again...


  10. #10
    Junior Member
    Join Date
    Mar 2000
    Location
    Montreal, QC
    Posts
    24

    Unhappy

    Oop, my apologies to everyone. I got a little ahead of myself. But isn't it possible to declare a currency type in an most SQL-compatible databases?

    Once again, sorry for the misunderstanding.

    JMik

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