VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A simple textbox is used in currency format and mask. for example 1234567.000 is shown as 1,234,567

by Ozden OZPEK (1 Submission)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sat 3rd April 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)

A simple textbox is used in currency format and mask. for example 1234567.000 is shown as 1,234,567.00.

Rate A simple textbox is used in currency format and mask. for example 1234567.000 is shown as 1,234,567



'the code below
'create a text box ont the form and name it txtamount
Private Sub txtamount_KeyPress(KeyAscii As Integer)
    Dim c
    Dim l As Integer
    Dim digit As Integer
    digit = 0
    Dim value As String
    Dim newvalue As String
    If (KeyAscii >= 48 And KeyAscii <= 57) Then
        DoEvents
        If InStr(1, txtAmount, ".") > 0 And Len(txtAmount) - InStr(1, txtAmount, ".") > 1 Then
            KeyAscii = 0
            Exit Sub
        End If
        txtAmount = txtAmount & Chr(KeyAscii)
    ElseIf KeyAscii = 46 Then
        If InStr(1, txtAmount, ".") > 0 Then
            KeyAscii = 0
            Exit Sub
        End If
        txtAmount = txtAmount & Chr(KeyAscii)
        KeyAscii = 0
        Exit Sub
    
    ElseIf KeyAscii <> 8 Then
        KeyAscii = 0
    Else
        If txtAmount <> "" Then txtAmount = Mid(txtAmount, 1, Len(txtAmount) - 1)
    End If
        c = InStr(1, txtAmount, ".")
        If c = 0 Then c = Len(txtAmount) + 1
        For l = 1 To c - 1
            If Mid(txtAmount, l, 1) <> "," Then
                value = value + Mid(txtAmount, l, 1)
            End If
        Next
        For l = Len(value) To 1 Step -1
            If digit = 3 And Len(value) <> l Then
                newvalue = Mid(value, l, 1) & "," & newvalue
                digit = 0
            Else
                newvalue = Mid(value, l, 1) & newvalue
            End If
            digit = digit + 1
        Next
        c = InStr(1, txtAmount, ".")
        If c > 0 Then
            txtAmount = newvalue & Mid(txtAmount, c)
        Else
            txtAmount = newvalue
        End If
        SendKeys ("{END}")
        
        KeyAscii = 0
End Sub


Download this snippet    Add to My Saved Code

A simple textbox is used in currency format and mask. for example 1234567.000 is shown as 1,234,567 Comments

No comments have been posted about A simple textbox is used in currency format and mask. for example 1234567.000 is shown as 1,234,567. Why not be the first to post a comment about A simple textbox is used in currency format and mask. for example 1234567.000 is shown as 1,234,567.

Post your comment

Subject:
Message:
0/1000 characters