以文本方式查看主题

-  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=29491)

--  作者:cks
--  发布时间:2013/3/7 17:47:00
--  计算代码有问题

我创建了一个员工表,希望的是在身份证号码列中填入身份证号码后自动生成地方信息、出生日期、年龄,另当我填入了入职日期后相对的计算服务天数,但代码还有年龄、服务天数没有计算出来,求师傅、师兄指点代码错误处:

 

代码:

If e.DataCol.Name = "身份证号码" Then \'如果更改的是身份证号码列
    If e.DataRow.IsNull("身份证号码") Then \'身份证号码是否为空
        e.DataRow("出生日期") = Nothing \'如果为空,则清除出生日期
        e.DataRow("姓别") = Nothing
        e.DataRow("年龄") = Nothing
        e.DataRow("籍贯") = Nothing
        e.DataRow("服务天数") = Nothing
    Else  If ValidPIN(e.DataRow("身份证号码")) = True Then
        \'否则从身份证号码列中提取出生日期
        e.DataRow("出生日期") = ReadBirthday(e.DataRow("身份证号码"))
        e.DataRow("姓别") = ReadSex(e.DataRow("身份证号码"))
        Dim st1 As String = Tables("员工表").Current("身份证号码")
        Dim st2 As String = st1.SubString(0,6)
        Dim dr As DataRow = DataTables("地区信息").Find("身份证区位码 = \'" & st2 & "\'")
        e.DataRow("籍贯") = dr("各地省市区")
    Else
        MessageBox.Show("您输入的身份证有误 , 或身份证可能是假的 !","警告",MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Error)
        e.DataRow("出生日期") = Nothing \'如果为空,则清除出生日期
        e.DataRow("姓别") = Nothing
        e.DataRow("身份证号码") = Nothing
    End If
End If

 

Select Case e.DataCol.name
    Case "入职日期","离职日期"
        If e.DataRow.IsNull("入职日期") Then
            e.DataRow("服务天数") = Nothing
        Else If e.DataRow.IsNull("出生日期") = False Then
            Dim tl As TimeSpan = Date.today - CDate(e.DataRow("出生日期"))
            e.DataRow("年龄") =  (Math.Round(tl.TotalDays)/365)
            If e.DataRow.Isnull("离职日期") Then
                Dim tp As TimeSpan = Date.today - CDate(e.DataRow("入职日期"))
                e.DataRow("服务天数") =  Math.Round(tp.TotalDays)
            Else
                Dim tt As TimeSpan = cdate(e.DataRow("离职日期")) - cdate(e.DataRow("入职日期"))
                e.DataRow("服务天数") = math.round(tt.Totaldays)
            End If
        End If
End Select

 

注:我是把代码写在表事件DataColChanged

[此贴子已经被作者于2013-3-7 17:49:51编辑过]

--  作者:狐狸爸爸
--  发布时间:2013/3/7 18:29:00
--  

逻辑问题,例如这一段:

 

Select Case e.DataCol.name
    Case "入职日期","离职日期"
        If e.DataRow.IsNull("入职日期") Then
            e.DataRow("服务天数") = Nothing
        Else If e.DataRow.IsNull("出生日期") = False Then
            Dim tl As TimeSpan = Date.today - CDate(e.DataRow("出生日期"))
            e.DataRow("年龄") =  (Math.Round(tl.TotalDays)/365)
            If e.DataRow.Isnull("离职日期") Then
                Dim tp As TimeSpan = Date.today - CDate(e.DataRow("入职日期"))
                e.DataRow("服务天数") =  Math.Round(tp.TotalDays)
            Else
                Dim tt As TimeSpan = cdate(e.DataRow("离职日期")) - cdate(e.DataRow("入职日期"))
                e.DataRow("服务天数") = math.round(tt.Totaldays)
            End If
        End If
End Select

 

应该改为:

 

Select Case e.DataCol.name
    Case "入职日期","离职日期"
        If e.DataRow.IsNull("入职日期") Then
            e.DataRow("服务天数") = Nothing
        Else
            If e.DataRow.Isnull("离职日期") Then
                Dim tp As TimeSpan = Date.today - CDate(e.DataRow("入职日期"))
                e.DataRow("服务天数") =  Math.Round(tp.TotalDays)
            Else
                Dim tt As TimeSpan = cdate(e.DataRow("离职日期")) - cdate(e.DataRow("入职日期"))
                e.DataRow("服务天数") = math.round(tt.Totaldays)
            End If
        End If
        If e.DataRow.IsNull("出生日期") = False Then
            Dim tl As TimeSpan = Date.today - CDate(e.DataRow("出生日期"))
            e.DataRow("年龄") =  (Math.Round(tl.TotalDays)/365)
        End If
End Select


--  作者:cks
--  发布时间:2013/3/8 9:49:00
--  

嗯嗯,找到原因,多謝狐爸