设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

[模块/函数] 限制输入类型(类模块)

[复制链接]
跳转到指定楼层
1#
发表于 2011-10-10 20:27:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
         限制输入类型的示例
  1. Private m_DataType As Integer   '定义数据类型
  2. Private WithEvents LimitTextBox As TextBox

  3. Enum DType
  4.     MyChar = 0     '字符类型
  5.     MyInt = 1      '整数
  6.     MyDecimal = 2  '小数
  7.     MyPhone = 3    '电话
  8.     MyEmail = 4    'Email
  9.     MyNone = 5     '常规
  10. End Enum


  11. Property Get DataType() As DType
  12.     DataType = m_DataType
  13. End Property

  14. Property Let DataType(Value As DType)
  15.     m_DataType = Value
  16. End Property

  17. Public Sub SetTextBoxType(LimitText As TextBox, LimitType As DType)
  18.   '设置文本数据类型
  19.     Set LimitTextBox = LimitText
  20.     With LimitTextBox
  21.         DataType = LimitType
  22.        .OnKeyPress = "[Event Procedure]"
  23.     End With
  24.   
  25. End Sub
  26. Private Sub LimitTextBox_KeyPress(KeyAscii As Integer)
  27.    
  28.    Select Case m_DataType
  29.       
  30.       Case 0
  31.          If Not IsChar(KeyAscii) = True Then KeyAscii = 0
  32.       Case 1
  33.           If Not IsInt(KeyAscii) = True Then KeyAscii = 0
  34.       Case 2
  35.           If Not IsDecimal(KeyAscii) = True Then KeyAscii = 0
  36.       Case 3
  37.            If Not IsPhone(KeyAscii) = True Then KeyAscii = 0
  38.       Case 4
  39.            If Not IsEmail(KeyAscii) = True Then KeyAscii = 0
  40.       Case 5
  41.          
  42.     End Select

  43. End Sub


  44. Private Sub Class_Initialize()
  45.     Set LimitTextBox = Nothing
  46. End Sub


  47. Private Function IsChar(ByVal A As Integer) As Boolean
  48.     If (A < 97 Or A > 122) And (A < 65 Or A > 90) And (A <> 8) And (A <> 32) Then
  49.         IsChar = False
  50.        Else
  51.         IsChar = True
  52.     End If
  53. End Function
  54. Private Function IsInt(ByVal A As Integer) As Boolean
  55.     If (A < 48 Or A > 57) And (A <> 8) Then
  56.       
  57.           IsInt = False
  58.        Else
  59.           IsInt = True
  60.     End If
  61. End Function
  62.    
  63. Private Function IsDecimal(ByVal KeyAscii As Integer) As Boolean
  64.     If (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8 Or KeyAscii = Asc(".") Then
  65.         IsDecimal = True
  66.      Else
  67.         IsDecimal = False
  68.     End If
  69. End Function
  70. Private Function IsPhone(ByVal A As Integer) As Boolean
  71.     If (A < 48 Or A > 57) And (A <> 8) And (A <> Asc("-")) Then
  72.         IsPhone = False
  73.        Else
  74.         IsPhone = True
  75.     End If
  76. End Function
  77. Private Function IsEmail(ByVal A As Integer) As Boolean
  78.     If (A < 97 Or A > 122) And (A < 65 Or A > 90) And (A <> 8) And (A < 48 Or A > 57) And A <> Asc("-") And A <> Asc("@") And A <> Asc(".") Then
  79.         IsEmail = False
  80.        Else
  81.         IsEmail = True
  82.     End If
  83. End Function
  84. Private Function IsProperDecimal(ByVal No As String) As Boolean
  85.     Dim NoLen
  86.     Dim DotFlag
  87.     DotFlag = 0

  88.     NoLen = Len(No)
  89.     Dim I As Integer
  90.     For I = 1 To NoLen
  91.         If Mid(No, I, 1) = "." Then DotFlag = DotFlag + 1
  92.     Next I
  93.     If DotFlag > 1 Then IsProperDecimal = False Else IsProperDecimal = True
  94. End Function


复制代码
完整示例



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享分享 分享淘帖 订阅订阅
2#
发表于 2011-10-10 20:43:23 | 只看该作者
有些日了没见到老兄新作了,收藏了,多谢分享。
3#
发表于 2011-10-10 22:20:51 | 只看该作者
今天大请客
4#
发表于 2011-10-11 09:00:29 | 只看该作者
谢谢分享
收下
5#
发表于 2011-10-11 11:30:23 | 只看该作者
收藏了 谢谢分享
6#
发表于 2011-10-12 08:15:29 | 只看该作者
多谢分享。
7#
发表于 2011-10-12 11:58:16 | 只看该作者
好东西一定要收藏

点击这里给我发消息

8#
发表于 2012-11-14 09:21:11 | 只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-26 19:52 , Processed in 0.124126 second(s), 33 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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