经典算法
Access限制录入不同类型的数据
2015-01-31 14:14:02
文件类型 : rar
文件版本 : Access2003
简要说明 : 我们做的应用程序很多时候都会限制数据录入的类型,比如说购买数量,我们不能随便输入文字,更不能输入负数和小数了。这时我们当然要限制其输入了,否则就会出错。

    我们做的应用程序很多时候都会限制数据录入的类型,比如说购买数量,我们不能随便输入文字,更不能输入负数和小数了。这时我们当然要限制其输入了,否则就会出错。     下面的示例是使用了类模块创建函数,方便调用限制不规则的录入。可以设置整数格式,小数格式,字符格式,email格式,电话格式等。     自己还可以根据自己的需要,改写符合自己需要的格式。达到控制文本的录入,尽量减少由于输入不规范带来的影响

 Private m_DataType As Integer   '定义数据类型
 Private WithEvents LimitTextBox As TextBox
 
 Enum DType
    MyChar = 0     '字符类型
    MyInt = 1      '整数
    MyDecimal = 2  '小数
    MyPhone = 3    '电话
    MyEmail = 4    'Email
    MyNone = 5     '常规
End Enum
 
 
Property Get DataType() As DType
    DataType = m_DataType
End Property
 
Property Let DataType(Value As DType)
    m_DataType = Value
End Property
 
Public Sub SetTextBoxType(LimitText As TextBox, LimitType As DType)
  '设置文本数据类型
    Set LimitTextBox = LimitText
    With LimitTextBox
        DataType = LimitType
       .OnKeyPress = "[Event Procedure]"
    End With
  
End Sub
Private Sub LimitTextBox_KeyPress(KeyAscii As Integer)
   
   Select Case m_DataType
      
      Case 0
         If Not IsChar(KeyAscii) = True Then KeyAscii = 0
      Case 1
          If Not IsInt(KeyAscii) = True Then KeyAscii = 0
      Case 2
          If Not IsDecimal(KeyAscii) = True Then KeyAscii = 0
      Case 3
           If Not IsPhone(KeyAscii) = True Then KeyAscii = 0
      Case 4
           If Not IsEmail(KeyAscii) = True Then KeyAscii = 0
      Case 5
          
    End Select
 
End Sub
 
 
Private Sub Class_Initialize()
    Set LimitTextBox = Nothing
End Sub
 
 
Private Function IsChar(ByVal A As Integer) As Boolean
    If (A < 97 Or A > 122) And (A < 65 Or A > 90) And (A <> 8) And (A <> 32) Then
        IsChar = False
       Else
        IsChar = True
    End If
End Function
Private Function IsInt(ByVal A As Integer) As Boolean
    If (A < 48 Or A > 57) And (A <> 8) Then
       
          IsInt = False
       Else
          IsInt = True
    End If
End Function
    
Private Function IsDecimal(ByVal KeyAscii As Integer) As Boolean
    If (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8 Or KeyAscii = Asc(".") Then
        IsDecimal = True
     Else
        IsDecimal = False
    End If
End Function
Private Function IsPhone(ByVal A As Integer) As Boolean
    If (A < 48 Or A > 57) And (A <> 8) And (A <> Asc("-")) Then
        IsPhone = False
       Else
        IsPhone = True
    End If
End Function
Private Function IsEmail(ByVal A As Integer) As Boolean
    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
        IsEmail = False
       Else
        IsEmail = True
    End If
End Function
Private Function IsProperDecimal(ByVal No As String) As Boolean
    Dim NoLen
    Dim DotFlag
    DotFlag = 0
 
    NoLen = Len(No)
    Dim I As Integer
    For I = 1 To NoLen
        If Mid(No, I, 1) = "." Then DotFlag = DotFlag + 1
    Next I
    If DotFlag > 1 Then IsProperDecimal = False Else IsProperDecimal = True
End Function