Option Explicit
\'===================================================================
\'函数名称: IsIDNumber
\'功能描述: 根据GB11643-1999编码规则验证18位身份证号码是否有效
\'输入参数: 参数1:IDNumber 必需的,18位身份证号码
\'返回参数: 有效返回True,无效返回False
\'兼 容 性:
\'使用示例: IsIDNumber("370284197901130819") \'返回True
\'相关调用:
\'作 者: 红尘如烟
\'创建日期: 20010-4-25
\'===================================================================
Function IsIDNumber(ByVal IDNumber As String) As Boolean
Const W As String = "79058421637905842" \'加权因子
Const C As String = "10X98765432" \'校验码
Dim S As Integer, i As Integer, T As Integer
IDNumber = UCase(Trim(IDNumber))
If Len(IDNumber) <> 18 Then Exit Function
If Not IsNumeric(Mid(IDNumber, 1, 17)) Then Exit Function
If Not IDNumber Like "*[0-9X]" Then Exit Function
For i = 1 To 17
T = Mid(W, i, 1)
If T = 0 Then T = 10
S = S + Mid(IDNumber, i, 1) * T
Next
T = S Mod 11
If Right(IDNumber, 1) = Mid(C, T + 1, 1) Then IsIDNumber = True
End Function
Private Sub Form_Load()
End Sub
Private Sub txtAge_BeforeUpdate(Cancel As Integer)
End Sub
Private Sub txtIDNumber_AfterUpdate()
If Nz(Me.txtIDNumber) = "" Then Exit Sub
\'验证输入的是否为一个有效的身份证号码
If Not IsIDNumber(Nz(Me.txtIDNumber)) Then
MsgBox "无效的身份证号码!", vbInformation
Me.txtIDNumber = Null
Me.txtBirthday = Null
Me.txtSex = Null
Me.txtNativePlace = Null
Else
\'从表中取得籍贯
Me.txtNativePlace = DLookup("[FAddress]", "tblNativePlaceList", "[FNumber]=" & Left(Me.txtIDNumber, 6))
\'提取出生日期(第7-14位)
Me.txtBirthday = DateSerial(Mid(Me.txtIDNumber, 7, 4), Mid(Me.txtIDNumber, 11, 2), Mid(Me.txtIDNumber, 13, 2))
\'提取性别(第17位,奇数为男,偶数为女)
Me.txtSex = IIf(Mid(Me.txtIDNumber, 17, 1) Mod 2 = 0, "女", "男")
\'根据出生日期计算年龄,精确到年
\' Me.txtAge = DateDiff("yyyy", Me.txtBirthday, Date)
\'根据出生日期计算年龄,精确到月
Me.txtAge = DateDiff("m", Me.txtBirthday, Date) \\ 12
\'根据出生日期计算年龄,精确到天
\' Me.txtAge = DateDiff("d", Me.txtBirthday, Date) \\ 365.25
End If
End Sub