Enter a percentage directly into a text box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form with a text box and the format set to percentage.

If the user enters .2 then the textbox will display 20% (as expected).

I would like to adjust this so that the user can type 20 and the textbox
will display 20%.

Can I run an event tocatch the input and divide by 100....

Bruce.
 
Yes, use the AfterUpdate event of the text box to divide by 100.

Save the function below in a standard module. The call it from a text box
named "Amount" by setting its After Update property to:
=MakePercent([Amount])

The function only divides by 100 if the user did not type the % sign.

Public Function MakePercent(ctl As Control)
If InStr(ctl.Text, "%") = 0 Then
ctl = ctl / 100
End If
End Function
 
Hi Allen,

I tried what you suggested but I havn't got something right.

I get tyhe following run-time error 2185. 'You cant reference a a property
or method for a control unless the control has the focus'.

I guess because I am typing the number and presing enter. The focus is
tabbing to another field. Any suggestions?

The second issue I have is I am already using After Update on this textbox
as an event procedure. What do I modify in =MakePercent([Amount]) so that it
will call the function from the event procedure rather the the control
property.

Your advice is appreciated.

Allen Browne said:
Yes, use the AfterUpdate event of the text box to divide by 100.

Save the function below in a standard module. The call it from a text box
named "Amount" by setting its After Update property to:
=MakePercent([Amount])

The function only divides by 100 if the user did not type the % sign.

Public Function MakePercent(ctl As Control)
If InStr(ctl.Text, "%") = 0 Then
ctl = ctl / 100
End If
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Bruce said:
I have a form with a text box and the format set to percentage.

If the user enters .2 then the textbox will display 20% (as expected).

I would like to adjust this so that the user can type 20 and the textbox
will display 20%.

Can I run an event tocatch the input and divide by 100....

Bruce.
 
At the time the control's AfterUpdate event runs, it still has focus, so I'm
not sure where you are running this from.

To add the line to your existing eventprocedure, just add the line:
Call MakePercent(Me.[Amount])
again assuming that "Amount" is the name of the text box.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Bruce said:
Hi Allen,

I tried what you suggested but I havn't got something right.

I get tyhe following run-time error 2185. 'You cant reference a a property
or method for a control unless the control has the focus'.

I guess because I am typing the number and presing enter. The focus is
tabbing to another field. Any suggestions?

The second issue I have is I am already using After Update on this textbox
as an event procedure. What do I modify in =MakePercent([Amount]) so that
it
will call the function from the event procedure rather the the control
property.

Your advice is appreciated.

Allen Browne said:
Yes, use the AfterUpdate event of the text box to divide by 100.

Save the function below in a standard module. The call it from a text box
named "Amount" by setting its After Update property to:
=MakePercent([Amount])

The function only divides by 100 if the user did not type the % sign.

Public Function MakePercent(ctl As Control)
If InStr(ctl.Text, "%") = 0 Then
ctl = ctl / 100
End If
End Function


Bruce said:
I have a form with a text box and the format set to percentage.

If the user enters .2 then the textbox will display 20% (as expected).

I would like to adjust this so that the user can type 20 and the
textbox
will display 20%.

Can I run an event tocatch the input and divide by 100....

Bruce.
 
Back
Top