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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency question about a text box

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
US
I have searched for an answer and cant find one. Sorry if this has already been answered and my searching techniques are to blame.
Anyway, Im working on a check book application.
I have a text box where user can enter the amount of the check, Example: 125.32
I have the text box data type set as currency.
I have it set for right justify.
What I need is the text box to initially display 0.00
As the user types, the numbers need to move to the left but skip the decimal point.
Example:
User types 1 display shows 0.01
User types 2 display shows 0.12
types 5 = 1.25
types 3 = 12.53
types 2 = 125.32

I know this has got to be a lot simpler then Im trying to do, Ive been scratching my head for a couple of days now.
Thanks.
 
Have a look at the masked edit text box control (VB Help).

Cogito eggo sum – I think, therefore I am a waffle.
 
Thanks for that suggestion, it looks very promising.
I have never messed around with the masked edit control so I am sure I am missing some tricks.
The first thing I dont like about it is that when user types numbers, they are entered from the left to right.
This means if I reserve enough spaces to enter in 99,999.99,
and if the user enters in 20.00, the text box shows 20,009.99.
Is there any way to make the numbers entered start on the right and work left?

I did come up with a way to do what I want using form keyview and 3 labels.
I have label 15, label16, and label 17.
Label16 is the decimal point.
I also drew a rectangle shape around the three labels to give it more of a text box look.
I dim junk as string in beginning of the code.

Here is my code:

Private Sub form_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then 'backspace
If Len(Label17.Caption) = 1 Then Label17.Caption = "00":_ GoTo exitme
If Label17.Caption = "01" Then Label17.Caption = "00":_ GoTo exitme
If Len(Label15.Caption) = 0 Then
Label17.Caption = "0" + Left(Label17.Caption, 1)
GoTo exitme
End If

Label17.Caption = Right(Label15.Caption, 1) +_ Left(Label17.Caption, 1)
Label15.Caption = Left(Label15.Caption,_ Len(Label15.Caption) - 1)
GoTo exitme
End If
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0: Exit_ Sub
If Label15.Caption = "00" Or Label15.Caption = "0" Then_ Label15.Caption = ""
junk = Label15.Caption + Label17.Caption
junk = junk + Chr(KeyAscii)
If Len(junk) = 1 Then Label17.Caption = "0" + junk: Exit Sub
If Len(junk) = 2 Then Label17.Caption = junk:_ Label15.Caption = "0": Exit Sub
Label17.Caption = Right(junk, 2)
If Len(junk) > 2 Then Label15.Caption = Left(junk,_ Len(junk) - 2)
exitme:
Shape1.Width = Label15.Width + Label16.Width +_ Label17.Width + 70
Shape1.Left = Label15.Left
End Sub
 

I've been playing with some logic for you and this is what I came up with (so far....)

Just one TextBox1 (with Right alignment) on the Form
Code:
Option Explicit

Private Sub Form_Load()
Text1.Text = "0.00"
Text1.SelStart = Len(Text1.Text)
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
    Case 8  [green]'Backspace
        'Need some logic here[/green]
    Case 48 To 57, 96 To 105[green]
        '49-57: 0 to 9 number keys above letters
        '96-105: 0 to 9 in key pad[/green]
        Text1.Text = Val(Text1.Text) * 10
    Case Else
        Exit Sub
End Select
Text1.Text = Format(Text1.Text, "##0.00")
Text1.SelStart = Len(Text1.Text)
End Sub
Needs some additional logic for Backspace key

Have fun.

---- Andy
 
That looks very interesting, short and sweet.
I will give it a try.
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top