限制录入内容不能“空”或限制必须是“字符”、“英文”、“数字”、“日期”等-hjs
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


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

发表时间:2012/7/24 13:05:06 评论(1) 浏览(9996)  评论 | 加入收藏 | 复制
   
摘 要:得到很多老师的帮助,觉得这个很好,基本原创,希望简化!
正 文:

'字符校验
'* 功能:限制输入的字符
'* 使用: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群(群号:198465573)
 
 相关文章
access数据库窗体中设置有效性规则示例  【褚玉春  2008/6/23】
表中两个字段对比的有效性规则示例  【褚玉春  2009/5/8】
【菜鸟入门】通过有效性规则限制输入数据类型的示例\如何限定文本框控...  【Access软件网整理  2011/1/5】
access里组合框只允许输入男女的方法\access里输入男女的...  【麥田  2011/12/2】
【Word版教程】access有效性规则的写法文档资料下载  【麥田  2011/12/20】
常见问答
技术分类
相关资源
文章搜索
关于作者

hjs

文章分类

文章存档

友情链接