以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  请教狐狸爸爸:关于从身份证号码中自动提取出生年月、性别、年龄代码问题  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=19828)

--  作者:lipiti
--  发布时间:2012/5/22 15:47:00
--  请教狐狸爸爸:关于从身份证号码中自动提取出生年月、性别、年龄代码问题
DataColChanged 事件

If e .DataCol . Name = "身份证号码" Then \'如果更改的是身份证号码列
    If e .DataRow . IsNull( "身份证号码" ) Then \'身份证号码是否为空
        e .DataRow ( "出生日期" ) = Nothing \'如果为空,则清除出生日期
        e .DataRow ( "性别") = Nothing
        e .DataRow ( "年龄") = Nothing 
    Else
        \'否则从身份证号码列中提取出生日期
        e .DataRow ( "出生日期" ) = ReadBirthday ( e. DataRow ("身份证号码" ))
        e .DataRow ( "性别") = ReadSex (e .DataRow ( "身份证号码" ))
        e .DataRow ( "年龄") = Date .Today . Year - e .DataRow ( "出生日期" ).Year
    End If
End If

狐狸爸爸帮忙看看以上代码写得对不对,或者有没有更换的代码,万分感谢!



--  作者:狐狸爸爸
--  发布时间:2012/5/22 16:10:00
--  

上面的代码,有问题吗?

我看不出有问题啊。


--  作者:lipiti
--  发布时间:2012/5/22 22:57:00
--  
呵呵,自动计算的年龄需要具体到 年月 日 比如今天是2012-05-22,在1982-05-22出生的年龄为30岁,在 1982-05-23出生的年龄为29岁;
后面找了几个狐狸爸爸之前回复的例子,终于可以了,把代码发布出来跟大家一起分享一下。

If e .DataCol . Name = "身份证号码" Then \'如果更改的是身份证号码列
    If e .DataRow . IsNull( "身份证号码" ) Then \'身份证号码是否为空
        e .DataRow ( "出生日期" ) = Nothing \'如果为空,则清除出生日期
        e .DataRow ( "性别") = Nothing
    Else
        \'否则从身份证号码列中提取出生日期
        e .DataRow ( "出生日期" ) = ReadBirthday ( e. DataRow ("身份证号码" ))
        e .DataRow ( "性别") = ReadSex (e .DataRow ( "身份证号码" ))
    End If
End If
If e.DataCol.name = "出生日期" Then \'如果更改的是出生日期列
    If e.DataRow.IsNull("出生日期") Then \'出生日期是否为空
        e.DataRow("年龄") = Nothing \'如果为空,则清除年龄
    Else
        \'否则从出生日期列中提取出年龄
        Dim n As Integer= Date.Today.Year - e.DataRow("出生日期").Year
        If e.DataRow("出生日期").AddMonths(n*12) > Date.Today Then
            n = n -1
        End If
        e.DataRow("年龄") = n
    End If
End If