Member Avatar for deleted1234
Private Sub Text1_Change()
    Text1 = Format(Text1, "#,###")
    Text1.SelStart = Len(Text1.Text)
End Sub

This code formats the textbox value to include a comma for every 3 digits to the left. Problem is, it doesn't allow to type decimals like 3,000,000.50

What can I do?

Recommended Answers

All 17 Replies

Then try using

Private Sub Text1_Change()
      Text1 = Format(Text1, "#,##0.00")
      Text1.SelStart = Len(Text1.Text)
      End Sub

Hope this helps

Then try using

Private Sub Text1_Change()
      Text1 = Format(Text1, "#,##0.00")
      Text1.SelStart = Len(Text1.Text)
      End Sub

Hope this helps

Then try using

1.
      Private Sub Text1_Change()
      Text1 = Format(Text1, "#,##0.00")
      Text1.SelStart = Len(Text1.Text)
      End Sub

Hope this helps

Although Kinwang is 100% correct, it will NOT work under the Change event, rather use the LostFocus event.

Private Sub Text1_LostFocus()

Text1 = Format(Text1, "#,##0.00")
Text1.SelStart = Len(Text1.Text)
End Sub

When you need to return the value again use -

dim xValue As Integer

xValue = CDbl(Text1.Text) 'This will remove the commas and see the valua as an integer
Member Avatar for deleted1234

I already have a LostFocus event that formats the amount but what I really need is the Change event so that it formats the amount as you type. Unfortunately, what Kinwang suggested doesn't work.

Any other ideas? Thanks by the way.

Your problem is that your cursor will be placed at the end of the last two digits in the change event. This is exactly what you do not need. I know there is code to manipulate the cursor, but do you really want to go that route?

Member Avatar for deleted1234

Yes please.

use maskedbox component.

Member Avatar for deleted1234

Which specific component do I need to add? I can't find maskedbox. Thank you!

Hi Shinehah, Sorry that I am only replying to you now. I have tried several ways to manipulate the cursor, but I think that it will be prone to errors. I have used the masked edit control before, which I see Ryan suggested as well.

On your components tab, right click to add the component, select "Microsoft Masked Edit Control" and add the control to your form. Here are some properties that you will find helpfull with the control, you'll see that it is very similar to a textbox control, just controlling what gets entered into the control.

To clear the Text property when you have a mask defined, you first need to set the Mask property to an empty string, and then the CtlText property to an empty string -

MaskedEdit1.Mask = ""
MaskedEdit1.Text = ""

On your form load event, you can set the Mask property (What You See) to the following-

MaskedEdit1.Mask = "#,###.##" 'The # character allows for only numbers to be added as to where the questionmark (?) allows for only text. 
'So, if you want to add a dollar sign ($) in front of the amount you will use it as -
MaskedEdit1.Mask = "?#,###.##"

You can learn much more on the control at the following MS vb6 site -

http://msdn.microsoft.com/en-us/library/11405hcf(VS.71).aspx

I hope that this solves your problem, good luck.

Labq5, please read all the posts and you will see that we have already covered all possible string functions. When formatting in the change event, the cursor can not be controlled with the format chosen, resulting in incorrect data entered into the textbox.

The maskededit is the only control that can be utilized in this concept.

Member Avatar for deleted1234

Andre I tried working with the Mask Control as you suggested. These are some issues I encountered.
- It can not be aligned right justified.
- If I define mask as "#,###.##" it doesn't allow amount greater than 9,999.99 or if I define "#,###,###.##" and I need an amount less than the format it leaves extra commas that I do not need. It allows to type decimals only when you filled up the entire mask or format.

I'm trying to work on a different approach using keypress or keyup. I'll see what I can do.

Thanks for all your help!

hi there i just have a problem with my program i used the keycodes of vb wherein every time i enter a letter from A-Z it will display each letter together with a special character " | ". Im done with this but my problem is, the letters were displaying from right to left. please help me with this.

here is my code:

If KeyAscii = 13 Or KeyAscii = 9 Then
fname_txt.SetFocus
surname_txt.Text = StrConv(surname_txt.Text, 1)
End If

If KeyAscii <= 90 Or KeyAscii >= 65 Then
surname_txt.Text = Format(" " & "|" & " " & surname_txt.Text & " ")
End If

Thanks in advance!

@gelaisg18, this post is already 2 YEARS old, please open your own thread with your question, code and problem. We will gladly help from there, thanx.

My bad, just saw your new thread, I will reply from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.