设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3378|回复: 8
打印 上一主题 下一主题

Enter value as a percent

[复制链接]
跳转到指定楼层
1#
发表于 2005-9-20 19:47:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
文章标题: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编辑过]

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-3 02:30 , Processed in 0.072919 second(s), 24 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表