設(shè)為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊

QQ登錄

只需一步,快速開始

返回列表 發(fā)新帖
查看: 3682|回復(fù): 7
打印 上一主題 下一主題

[模塊/函數(shù)] 限制輸入類型(類模塊)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2011-10-10 20:27:06 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
         限制輸入類型的示例
  1. Private m_DataType As Integer   '定義數(shù)據(jù)類型
  2. Private WithEvents LimitTextBox As TextBox

  3. Enum DType
  4.     MyChar = 0     '字符類型
  5.     MyInt = 1      '整數(shù)
  6.     MyDecimal = 2  '小數(shù)
  7.     MyPhone = 3    '電話
  8.     MyEmail = 4    'Email
  9.     MyNone = 5     '常規(guī)
  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.   '設(shè)置文本數(shù)據(jù)類型
  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


復(fù)制代碼
完整示例



本帖子中包含更多資源

您需要 登錄 才可以下載或查看,沒有帳號?注冊

x
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享分享 分享淘帖 訂閱訂閱
2#
發(fā)表于 2011-10-10 20:43:23 | 只看該作者
有些日了沒見到老兄新作了,收藏了,多謝分享。
3#
發(fā)表于 2011-10-10 22:20:51 | 只看該作者
今天大請客
4#
發(fā)表于 2011-10-11 09:00:29 | 只看該作者
謝謝分享
收下
5#
發(fā)表于 2011-10-11 11:30:23 | 只看該作者
收藏了 謝謝分享
6#
發(fā)表于 2011-10-12 08:15:29 | 只看該作者
多謝分享。
7#
發(fā)表于 2011-10-12 11:58:16 | 只看該作者
好東西一定要收藏

點擊這里給我發(fā)消息

8#
發(fā)表于 2012-11-14 09:21:11 | 只看該作者
謝謝分享
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則

QQ|站長郵箱|小黑屋|手機版|Office中國/Access中國 ( 粵ICP備10043721號-1 )  

GMT+8, 2025-7-13 05:03 , Processed in 0.112732 second(s), 33 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復(fù) 返回頂部 返回列表