限制录入内容不能“空”或限制必须是“字符”、“英文”、“数字”、“日期”等
时 间: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)
- 统计当月之前(不含当月)的记录...(03.11)
- 【Access Inputbo...(03.03)
- 按回车键后光标移动到下一条记录...(02.12)
- 【Access Dsum示例】...(02.07)
- Access对子窗体的数据进行...(02.05)
- 【Access高效办公】上月累...(01.09)
- 【Access高效办公】上月累...(01.06)
- 【Access Inputbo...(12.23)
- 【Access Dsum示例】...(12.16)

学习心得
最新文章
- 32位的Access软件转化为64...(04.12)
- 【Access高效办公】如何让vb...(04.11)
- 仓库管理实战课程(10)-入库功能...(04.08)
- Access快速开发平台--Fun...(04.07)
- 仓库管理实战课程(9)-开发往来单...(04.02)
- 仓库管理实战课程(8)-商品信息功...(04.01)
- 仓库管理实战课程(7)-链接表(03.31)
- 仓库管理实战课程(6)-创建查询(03.29)
- 仓库管理实战课程(5)-字段属性(03.27)
- 设备装配出入库管理系统;基于Acc...(03.24)