限制录入内容不能“空”或限制必须是“字符”、“英文”、“数字”、“日期”等
时 间: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交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access对子窗体数据进行批...(10.30)
- 最精简的组合框行来源数据快速输...(10.25)
- Access仿平台的多值选择器...(10.24)
- 【Access日期区间段查询】...(10.22)
- 【Access源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
学习心得
最新文章
- Access快速开发平台--对上传...(11.22)
- Access快速开发平台企业版--...(11.18)
- 不会用多表联合查询,多表查询没结果...(11.16)
- 【案例分享】主键字段值含有不间断空...(11.16)
- Access快速开发平台--后台D...(11.14)
- 微软Access邀测新Monaco...(11.12)
- Access列表框左右互选、列表框...(11.11)
- 高效率在导入数据前删除记录(11.10)
- Access报价单转订单示例代码(11.08)
- Access系统自带的日期选择器不...(11.08)