文章标题:Enter value as a percent
文章来源:http://allenbrowne.com/casu-16.html
原文:
When you set a field's Format property to "ercent" and enter 7, Access interprets it as 700%.
How do you get it to interpret it as 7% without having to type the percent sign for every entry?
Use the AfterUpdate event of the control to divide by 100.
But then if the user did type "7%", your code changes it to 0.07%.
We need to divide by 100 only if the user did not type the percent sign.
To do that, examine the Text property of the control.
Unlike the control's Value, the Text property is the text as you see it.
(In Access, unlike pure VB, the Text is available only for the control that has focus.)
Using the code
To save the function in your database:
Choose the Modules tab of the Database window.
Click New to open a module.
Copy the code below, and paste into your module.
Save the module with a name such as "Module1".
To apply to a text box named "Text23":
Open the form in design view.
Right-click the text box, and choose Properties.
Set the After Update property of the text box t
=MakePercent([Text23]) Public Function MakePercent(txt As TextBox)
On Error GoTo Err_Handler
'Purpose: Divide the value by 100 if no percent sign found.
'Usage: Set the After Update property of a text box named Text23 t
' =MakePercent([Text23])
If Not IsNull(txt) Then
If InStr(txt.Text, "%") = 0 Then
txt = txt / 100
End If
End If
Exit_Handler:
Exit Function
Err_Handler:
If Err.Number <> 2185 Then 'No Text property unless control has focus.
MsgBox "Error " & Err.Number & " - " & Err.Description
End If
Resume Exit_Handler
End Function 译文:用百分比输入值
当你设置一个领域的格式很可能要用到百分号然后输入7。ACCESS认为的意思是700%。
在没所以条目都没百分号时怎么样让它认为是7%?
在使用更新后事件中除控件使用100%外。可是当用户需要7%时,你的编码要改为0.07%。我们需要100%如果用户不需要使用百分号。
要那样做,调试所以文本框控件属性。
不像控件的值,文本框所有性质就是你看到的文本框。
在ACCESS里不像纯VB,这里的文本仅仅只能够用到控件。
使用编码
保存你的数据库的功能。
1. 选择数据库窗体顶部的模块。
2. 点击新建并打开模块。
3. 复制下面的编码,然后粘贴到你的模块里。
4. 保存模块并命名为“模块1”
提供一个文本框并命名为“text23”:
1. 打开窗体设计模式。
2. 正确点击文本框,然后选择道具。
3. 设计更新后事件属性到:=MakePercent([Text23])
Public Function MakePercent(txt As TextBox)
On Error GoTo Err_Handler
'Purpose: Divide the value by 100 if no percent sign found.
'Usage: Set the After Update property of a text box named Text23 t
' =MakePercent([Text23])
If Not IsNull(txt) Then
If InStr(txt.Text, "%") = 0 Then
txt = txt / 100
End If
End If
Exit_Handler:
Exit Function
Err_Handler:
If Err.Number <> 2185 Then 'No Text property unless control has focus.
MsgBox "Error " & Err.Number & " - " & Err.Description
End If
Resume Exit_Handler
End Function
翻译人:飞天业
[此贴子已经被作者于2005-9-20 12:06:00编辑过]
|