INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Currency in a TextBox

Currency in a TextBox

(OP)
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

'46 - period
' 8 - BackSpace

If KA = 8 Then  'BackSpace
    TextBoxNumber = KA
    Exit Function
End If

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

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

If (KA > 47 And KA < 58) Or KA = 46 Then
    'Only numbers are entered - it is OK
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.

RE: Currency in a TextBox

Does the MaskedEdit control not provide what you need?

RE: Currency in a TextBox

(OP)
I was playing around with MaskedEdit control, but the Currency format gives me a $ sign, plus negative values, and users don't want that. sad
Unless I did not explore MaskedEdit control enough...

Have fun.

---- Andy

There is a great need for a sarcasm font.

RE: Currency in a TextBox

(OP)
Thanks for the link.
Don't you just hate when the sample provided by Micro$oft does not work?
You place Option Explicit and 4 variables are not declared.
Plus whole bunch of other problems sad

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.

RE: Currency in a TextBox

Fairly trivial to correct the non-declaration. But what are the 'whole bunch' of other problems?

RE: Currency in a TextBox

(OP)
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.

RE: Currency in a TextBox

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 --> VB

'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 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close