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
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
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?
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
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.
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
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.
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350