Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

限制录入内容不能“空”或限制必须是“字符”、“英文”、“数字”、“日期”等

时 间:2012-07-24 13:05:06
作 者:hjs   ID:4770  城市:泸州
摘 要:得到很多老师的帮助,觉得这个很好,基本原创,希望简化!
正 文:

'字符校验
'* 功能:限制输入的字符
'* 使用:if iszf(me!bh,wsa21,0,100,1,"内容",)=false exit sub
'* 说明:获取焦点字段,校验的内容,内容起始长度,内容结束长度,校验类别(1:混合,2:混合×;3:英文,4:英文和数字,5:数字长度,6:数字大小),提示内容)

Public Function Iszf(ctl As Control, ByVal strcode As String, ByVal strcodecda As Long, ByVal strcodecdb As Long, ByVal strCodelb As String, ByVal strCodets As String) As Boolean

Dim strTemp As String
Dim i, J As Integer
Dim cw As Boolean
strcode = Nz(strcode)

If strcode = "" Then
MsgBox "请输入[" & strCodets & "]!  ", 0 + 64, "提示"
ElseIf strcode Like "*'*" Then
MsgBox "[" & strCodets & "] 内容含有非法字符[']!  ", 0 + 64, "提示"
Call sFindStr(ctl, "'")
Else
    Select Case strCodelb
    Case "1"
        If chkGb(strcode) > strcodecdb Then
        MsgBox "[" & strCodets & "] 内容长度不能大于" & strcodecdb / 2 & "字符!  ", 0 + 64, "提示"
        Else
        Iszf = True
        End If
       
    Case "2"
        If strcode Like "*×*" Then
        MsgBox "[" & strCodets & "] 内容含有需完善内容“×”!  ", 0 + 64, "提示"
        Call sFindStr(ctl, "×")
        ElseIf chkGb(strcode) > strcodecdb Then
        MsgBox "[" & strCodets & "] 内容长度不能大于" & strcodecdb / 2 & "字符!  ", 0 + 64, "提示"
        Else
        Iszf = True
        End If
   
    Case "3"

        For i = 1 To Len(strcode)
        strTemp = Mid(strcode, i, 1)
        J = Asc(strTemp)
        If (90 >= J And J >= 65) or (122 >= J And J >= 97) Then
            cw = True
        Else
            cw = False
        End If
        Next
       
        If cw = False Then
        MsgBox "[" & strCodets & "] 内容只能是英文字母!  ", 0 + 64, "提示"
        ElseIf Len(strcode) < strcodecda or Len(strcode) > strcodecdb Then
        MsgBox "[" & strCodets & "] 内容长度为" & strcodecda & "-" & strcodecdb & "个英文字符!  ", 0 + 64, "提示"
        Else
        Iszf = True
        End If
       
    Case "4"

        For i = 1 To Len(strcode)
        strTemp = Mid(strcode, i, 1)
        J = Asc(strTemp)
        If (90 >= J And J >= 65) or (122 >= J And J >= 97) or (57 >= J And J >= 48) Then
            cw = True
        Else
            cw = False
        End If
        Next
       
        If cw = False Then
        MsgBox "[" & strCodets & "] 内容只能是英文字母或数字!  ", 0 + 64, "提示"
        ElseIf Len(strcode) < strcodecda or Len(strcode) > strcodecdb Then
        MsgBox "[" & strCodets & "] 内容长度为" & strcodecda & "-" & strcodecdb & "个英文字符或数字!  ", 0 + 64, "提示"
        Else
        Iszf = True
        End If
      
    Case "5"
        If Not IsNumeric(strcode) Then
        MsgBox "[" & strCodets & "] 请输入阿拉伯数字!  ", 0 + 64, "提示"
        ElseIf Len(strcode) < strcodecda or Len(strcode) > strcodecdb Then
        MsgBox "[" & strCodets & "] 内容长度为" & strcodecda & "-" & strcodecdb & "个数字!  ", 0 + 64, "提示"
        Else
        Iszf = True
        End If
    Case "6"
        If Not IsNumeric(strcode) Then
        MsgBox "[" & strCodets & "] 请输入阿拉伯数字!  ", 0 + 64, "提示"
        ElseIf strcode < strcodecda or strcode > strcodecdb Then
        MsgBox "[" & strCodets & "] 数值为大于" & strcodecda & "小于" & strcodecdb & "的数字!  ", 0 + 64, "提示"
        Else
        Iszf = True
        End If

    Case Else
   
    End Select
End If
If Not Iszf Then ctl.SetFocus
End Function

'混合字符的长度
Public Function chkGb(strGB As String) As Integer
Dim ByteGB() As Byte
ByteGB = StrConv(strGB, vbFromUnicode)
chkGb = UBound(ByteGB) + 1
End Function

'过程功能:查找文本框中字符串
 'Call sFindStr(TxtStr, TxtFindStr)
 '------------------------------------------------------
Sub sFindStr(objTextBox As TextBox, strSearch As String)
    Dim intWhere As Integer
    With objTextBox
        '指定一字符串在另一字符串中最先出现的位置
        intWhere = InStr(.Value, strSearch)
        If intWhere Then
           '查找
           .SetFocus
           .SelStart = intWhere - 1
           .SelLength = Len(strSearch)
        Else
           '如未有指定字符,提示.
            'MsgBox "未找到你所查找的字符串!", vbOKOnly + vbInformation, "系统提示:"
        End If
    End With
End Sub

'短日期: 2010-11-18
'  使用: IF ISDRQ(ME!RQ,"日期")
Public Function IsDrq(ctl As Control, ByVal strCodets As String) As Boolean
Dim strcode As String
strcode = UCase(Trim(Nz(ctl)))
If Len(strcode) = 8 Then
strcode = Left(strcode, 4) & "-" & Mid(strcode, 5, 2) & "-" & Right(strcode, 2)
End If
If strcode = "" Then
MsgBox "请输入[" & strCodets & "]!  ", 0 + 64, "提示"
ElseIf Len(strcode) <> 10 Then
MsgBox "[" & strCodets & "] 日期格式不正确!" & vbNewLine & "例:2008年10月2日输入:" & vbCrLf & "    20081002 或 2008-10-02!  ", 0 + 64, "提示"
ElseIf Not IsDate(strcode) Then
MsgBox "[" & strCodets & "] 不是日期格式!  ", 0 + 64, "提示"
Else
IsDrq = True
ctl = Format(strcode, "yyyy-mm-dd")
End If
If Not IsDrq Then ctl.SetFocus
End Function

'长日期 2010-11-18 12:28
Public Function IsCrq(ctl As Control, ByVal strCodets As String) As Boolean
Dim strcode As String
strcode = UCase(Trim(Nz(ctl)))
If Len(strcode) = 12 Then
strcode = Left(strcode, 4) & "-" & Mid(strcode, 5, 2) & "-" & Mid(strcode, 7, 2) & " " & Mid(strcode, 9, 2) & ":" & Right(strcode, 2)
End If
If strcode = "" Then
MsgBox "请输入[" & strCodets & "]!  ", 0 + 64, "提示"
ElseIf Len(strcode) <> 16 Then
MsgBox "[" & strCodets & "] 日期格式不正确!" & vbCrLf & "例:2008年10月2日15时5分输入:" & vbCrLf & "    200810021502 或 2008-10-02 15:02 !  ", 0 + 64, "提示"
ElseIf Not IsDate(strcode) Then
MsgBox "[" & strCodets & "] 不是日期格式!  ", 0 + 64, "提示"
Else
IsCrq = True
ctl = Format(strcode, "yyyy-mm-dd hh:nn")
End If
If Not IsCrq Then ctl.SetFocus
End Function



Access软件网QQ交流群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助