separate every 3 digits 
Author Message
 separate every 3 digits

I have a text box in my form. It should get a number as money. I want it to
separate every 3 digits dynamically.for example if the user enter "123", it
shouldn't do anything, but as soon as the user enter the fourth digit, for
example "4", it should change it to "1,234" and it should be continued to
upper digits, for example "12,345" or "123,456" or "1,234,567".I can do it
for 4 digit, but when the digits increase, I don't know what to do?
please help me!


Thu, 09 Dec 2010 20:07:00 GMT  
 separate every 3 digits



Quote:
>I have a text box in my form. It should get a number as money. I want it to
> separate every 3 digits dynamically.for example if the user enter "123",
> it
> shouldn't do anything, but as soon as the user enter the fourth digit, for
> example "4", it should change it to "1,234" and it should be continued to
> upper digits, for example "12,345" or "123,456" or "1,234,567".I can do it
> for 4 digit, but when the digits increase, I don't know what to do?
> please help me!

Text1.Text = Format(Money, "###,###.##")

/Henning



Thu, 09 Dec 2010 20:23:09 GMT  
 separate every 3 digits

Quote:

> I have a text box in my form. It should get a number as money. I want it to
> separate every 3 digits dynamically.for example if the user enter "123", it
> shouldn't do anything, but as soon as the user enter the fourth digit, for
> example "4", it should change it to "1,234" and it should be continued to
> upper digits, for example "12,345" or "123,456" or "1,234,567".I can do it
> for 4 digit, but when the digits increase, I don't know what to do?
> please help me!

try this

Private Sub Text1_Change()

    Text1 = Format$(Text1, "###,###")
    Text1.SelStart = Len(Text1)

End Sub



Thu, 09 Dec 2010 21:03:21 GMT  
 separate every 3 digits

Quote:

> I have a text box in my form. It should get a number as money. I want it to
> separate every 3 digits dynamically.for example if the user enter "123", it
> shouldn't do anything, but as soon as the user enter the fourth digit, for
> example "4", it should change it to "1,234" and it should be continued to
> upper digits, for example "12,345" or "123,456" or "1,234,567".I can do it
> for 4 digit, but when the digits increase, I don't know what to do?
> please help me!

If you want to show a currency sign, then change my previous snippet to

Private Sub Text1_Change()
  Text1 = Format$(Text1, "$ ###,###")
  Text1.SelStart = Len(Text1)

End Sub

if you want trailing zeros as well as the currency

Private Sub Text1_Change()
  Text1 = Format$(Text1, "$ ###,###.00")
  Text1.SelStart = Len(Text1) - 3
End Sub



Thu, 09 Dec 2010 21:20:34 GMT  
 separate every 3 digits

Quote:


> > I have a text box in my form. It should get a number as money. I want it to
> > separate every 3 digits dynamically.for example if the user enter "123", it
> > shouldn't do anything, but as soon as the user enter the fourth digit, for
> > example "4", it should change it to "1,234" and it should be continued to
> > upper digits, for example "12,345" or "123,456" or "1,234,567".I can do it
> > for 4 digit, but when the digits increase, I don't know what to do?
> > please help me!

> If you want to show a currency sign, then change my previous snippet to

> Private Sub Text1_Change()
>   Text1 = Format$(Text1, "$ ###,###")
>   Text1.SelStart = Len(Text1)

> End Sub

> if you want trailing zeros as well as the currency

> Private Sub Text1_Change()
>   Text1 = Format$(Text1, "$ ###,###.00")
>   Text1.SelStart = Len(Text1) - 3
> End Sub

It was perfect, thanks a lot.


Fri, 10 Dec 2010 14:40:00 GMT  
 separate every 3 digits

Quote:


>>   Text1 = Format$(Text1, "$ ###,###")
> It was perfect, thanks a lot.

FYI, all you really need is

    "#,0"

Or "$#,0.00", etc., as the case may be.

VB does the rest.



Fri, 10 Dec 2010 15:23:18 GMT  
 separate every 3 digits

released on Sun, 22 Jun 2008 22:33:21 +0930 bearing the
following fruit:

Quote:

>> I have a text box in my form. It should get a number as money. I want it to
>> separate every 3 digits dynamically.for example if the user enter "123", it
>> shouldn't do anything, but as soon as the user enter the fourth digit, for
>> example "4", it should change it to "1,234" and it should be continued to
>> upper digits, for example "12,345" or "123,456" or "1,234,567".I can do it
>> for 4 digit, but when the digits increase, I don't know what to do?
>> please help me!

>try this

>Private Sub Text1_Change()

>    Text1 = Format$(Text1, "###,###")
>    Text1.SelStart = Len(Text1)

Personally I would format it AFTER the input.

While the code above will work 99% of the time, it will get
annoying if I need to correct a digit that is not at the end
of the number.

Plus I don't like things to changing as I type. Being a
keyboard user who navigates mainly with the keyboard it can
really throw you off your pace.

--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde



Fri, 10 Dec 2010 16:15:07 GMT  
 separate every 3 digits

Quote:



> >>   Text1 = Format$(Text1, "$ ###,###")
> > It was perfect, thanks a lot.

> FYI, all you really need is

>     "#,0"

> Or "$#,0.00", etc., as the case may be.

> VB does the rest.

Thank you Steve, It worked very well, thanks from all of you.


Fri, 10 Dec 2010 19:24:00 GMT  
 separate every 3 digits

Quote:



>>>   Text1 = Format$(Text1, "$ ###,###")
>> It was perfect, thanks a lot.

> FYI, all you really need is

>     "#,0"

> Or "$#,0.00", etc., as the case may be.

> VB does the rest.

(Graham picks up gun, aims at head, and pulls the trigger)

It's not until someone comes back and tells you something like that,
that you realise what you've been doing for 25 years can be simplified.

I've been grazing with the moo-cows for far too long.



Fri, 10 Dec 2010 21:20:46 GMT  
 separate every 3 digits

Quote:

> released on Sun, 22 Jun 2008 22:33:21 +0930 bearing the
> following fruit:


>>> I have a text box in my form. It should get a number as money. I want it to
>>> separate every 3 digits dynamically.for example if the user enter "123", it
>>> shouldn't do anything, but as soon as the user enter the fourth digit, for
>>> example "4", it should change it to "1,234" and it should be continued to
>>> upper digits, for example "12,345" or "123,456" or "1,234,567".I can do it
>>> for 4 digit, but when the digits increase, I don't know what to do?
>>> please help me!
>> try this

>> Private Sub Text1_Change()

>>    Text1 = Format$(Text1, "###,###")
>>    Text1.SelStart = Len(Text1)

> Personally I would format it AFTER the input.

> While the code above will work 99% of the time, it will get
> annoying if I need to correct a digit that is not at the end
> of the number.

> Plus I don't like things to changing as I type. Being a
> keyboard user who navigates mainly with the keyboard it can
> really throw you off your pace.

> --
> Jan Hyde

> https://mvp.support.microsoft.com/profile/Jan.Hyde

I always format after the event, too.
and validate after ALL data is in, and before I save it

I was just showing the OP what code would do what he wanted, even though
it goes against what I was brung (sic) up on, about validation, or the
problems with backspacing or doing a copy/insert.

Let's hope the OP finds this out before he gets a customer griping about
his program.

Graham



Fri, 10 Dec 2010 21:28:54 GMT  
 separate every 3 digits

released on Mon, 23 Jun 2008 22:50:46 +0930 bearing the
following fruit:

Quote:



>>>>   Text1 = Format$(Text1, "$ ###,###")
>>> It was perfect, thanks a lot.

>> FYI, all you really need is

>>     "#,0"

>> Or "$#,0.00", etc., as the case may be.

>> VB does the rest.

>(Graham picks up gun, aims at head, and pulls the trigger)

>It's not until someone comes back and tells you something like that,
>that you realise what you've been doing for 25 years can be simplified.

>I've been grazing with the moo-cows for far too long.

It's not always best to simplfy it though, for me if it
documents your intention better or makes it clearer then I'd
use the longer format. I'd bet there are a lot of folks out
there that wouldn't know what the shorter version does.

--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde



Fri, 10 Dec 2010 21:46:33 GMT  
 separate every 3 digits

Quote:

>> Personally I would format it AFTER the input.

> I always format after the event, too.

There is also something to be said for unformatting it before input.

For instance, a text box showing "$14,257.25" might be changed to "145257.254"
in the GotFocus event, and back to  "$14,257.25" in the LostFocus event. This
lets a user see the "real" value, and eliminates futzing around with the extra
characters when the user is editing or the program is parsing.



Fri, 10 Dec 2010 22:29:17 GMT  
 separate every 3 digits


Quote:



>>>>   Text1 = Format$(Text1, "$ ###,###")
>>> It was perfect, thanks a lot.

>> FYI, all you really need is

>>     "#,0"

>> Or "$#,0.00", etc., as the case may be.

>> VB does the rest.

> (Graham picks up gun, aims at head, and pulls the trigger)

> It's not until someone comes back and tells you something like that, that
> you realise what you've been doing for 25 years can be simplified.

Brevity isn't always good.  I knew about that "shortcut" but consciously
have always chosen not to use it because I think the longer format string is
clearer and therefore makes your code better. In 6 months or maybe even a
year, you might revisit that code, see that format string and say "what the
f'ing is that?" Never a good thing to find yourself saying about your own
code.  <g>

The only time you should, IMO, consider a shortcut or a "simplification" or
a one-liner (sorry Rick) is if it results in a measurable performance
increase...and even then sometimes the sacrifice of good, meaningful code is
not worth it.

--
Mike
Microsoft MVP Visual Basic



Sat, 11 Dec 2010 08:15:29 GMT  
 separate every 3 digits

Quote:

> Brevity isn't always good.  I knew about that "shortcut" but
> consciously have always chosen not to use it because I think the
> longer format string is clearer and therefore makes your code better.
> In 6 months or maybe even a year, you might revisit that code, see
> that format string and say "what the f'ing is that?" Never a good
> thing to find yourself saying about your own code.  <g>

Yeah, yeah, self documenting code, yada, yada. :)

I seem to see it often enough that "#,0" doesn't strike me as the least bit
ambiguous. But, I will admit that "#,##0" is only two more keystrokes. :)

It is worth noting that "#,##0" is usually better than "#,###"; the latter will
give you a blank if the value is 0.

I guess I don't expect format codes to be totally self explanatory. The
following one does something useful, but you would not know what it was just by
looking at it: "0,"

Steve



Sat, 11 Dec 2010 11:59:27 GMT  
 separate every 3 digits
Try FormatNumber()


Quote:
>I have a text box in my form. It should get a number as money. I want it to
> separate every 3 digits dynamically.for example if the user enter "123",
> it
> shouldn't do anything, but as soon as the user enter the fourth digit, for
> example "4", it should change it to "1,234" and it should be continued to
> upper digits, for example "12,345" or "123,456" or "1,234,567".I can do it
> for 4 digit, but when the digits increase, I don't know what to do?
> please help me!



Sun, 12 Dec 2010 09:54:57 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Replacing digits[space] with digits[non-breaking space]

2. 4-digit years-ccyy in Cdate() (returned as 2-digit years-yy)

3. separating pages into separate documents

4. check digit calculation

5. 24 digit software lock codes...

6. Two Digit Years in NT4 and Acc97

7. does not print digits to the right if $.00

8. Rounding 2 digits

9. Setting AutoNumber to any Number/digits

10. Rounding currency to 2 decimal digits.

11. 4 digit Integer

12. LIKE operator - any number of digits (not *)

 

 
Powered by phpBB® Forum Software