Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency in a TextBox

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,486
5
38
US
I have an application written in VB6.
I would like to add a capability to have a text box that would accept Currency data entry, so I can specify: (let's say) 5 whole numbers, and (possibly) 2 digits for cents.

As of now, I have this:

Code:
Private Sub txtMoney_KeyPress(KeyAscii As Integer)
    KeyAscii = TextBoxNumber(txtMoney, KeyAscii, 5, 2)
End Sub

and this Function:
Code:
Public Function TextBoxNumber(ByRef txt As TextBox, KA As Integer, _
    ByRef intW As Integer, ByRef intDec As Integer) As Integer

TextBoxNumber = KA
[green]
'46 - period
' 8 - BackSpace
[/green]
If KA = 8 Then  [green]'BackSpace[/green]
    TextBoxNumber = KA
    Exit Function
End If

If InStr(txt, ".") And KA = 46 Then[green]
    'Allow one period only[/green]
    TextBoxNumber = 0
    Exit Function
End If

If Len(txt.Text) > 0 Then
    If Len(Split(txt.Text, ".")(0)) > intW - 1 Then[green]
        'We exceeded allowed digits[/green]
        TextBoxNumber = 0
        Exit Function
    End If
    
    If InStr(txt.Text, ".") Then
        If Len(Split(txt.Text, ".")(1)) > intDec - 1 Then[green]
            'We exceeded allowed digits after the period[/green]
            TextBoxNumber = 0
            Exit Function
        End If
    End If
    
End If

If (KA > 47 And KA < 58) Or KA = 46 Then[green]
    'Only numbers are entered - it is OK[/green]
Else
    TextBoxNumber = 0
End If

End Function

It 'works', kind of.
I can type 123.45, it does not allow me to add any more numbers after .45, which is what I want. But I cannot do anything with the 123 part of the number. I must be doing something or missing something here.

Too bad I cannot use Regular Expressions.

Does anybody have any logic to allow typing currency and modifying it in VB6 in a text box?

Right now we have 2 text boxes: one for whole number and one for decimal part, and I hate this approach.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Does the MaskedEdit control not provide what you need?
 
I was playing around with MaskedEdit control, but the Currency format gives me a $ sign, plus negative values, and users don't want that. :-(
Unless I did not explore MaskedEdit control enough...

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Thanks for the link.
Don't you just hate when the sample provided by Micro$oft does not work?
You place [tt]Option Explicit[/tt] and 4 variables are not declared.
Plus whole bunch of other problems :-(

I guess I will stick with 2 text boxes and call it quits.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Fairly trivial to correct the non-declaration. But what are the 'whole bunch' of other problems?
 
I did declare them.
Other problems - you type just the period and you crash. Here in US - unfortunately - this is a common way, like .25 (I always type 0.25, but that's just me)

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
I pulled this code from somewhere when I wanted to allow for numeric entry only. The one thing I'm looking at in this code is that it makes use of SelStart which would let you re-position the cursor when the user clicks the '.' button. (Although the code below does that for the '-' button, you can see what has been done.)

Code:
'OnKeyPress
Select Case KeyAscii
        Case 8, 48 To 57
            'bksp 0123456789 all ok
        Case 46
            If InStr(1, YourTextBox.Text, ".") > 0 Then
                KeyAscii = 0
            End If
        Case 45    'minus
            If InStr(1, YourTextBox.Text, "-") > 0 Then
                If YourTextBox.SelLength = Len(YourTextBox.Text) Then    'entire field is selected so overwrite...
                    Exit Sub
                End If
                KeyAscii = 0
            Else
                YourTextBox.Text = "-" & YourTextBox.Text
                YourTextBox.SelStart = Len(YourTextBox.Text)
                YourTextBox= 0
            End If
        Case Else
            KeyAscii = 0
    End Select
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top