Foxtable(狐表)用户栏目专家坐堂 → 计算代码有问题


  共有6724人关注过本帖树形打印复制链接

主题:计算代码有问题

帅哥哟,离线,有人找我吗?
cks
  1楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:童狐 帖子:265 积分:1837 威望:0 精华:0 注册:2012/11/6 17:17:00
计算代码有问题  发帖心情 Post By: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编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
狐狸爸爸
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:47448 积分:251048 威望:0 精华:91 注册:2008/6/17 17:14:00
  发帖心情 Post By: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
  3楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:童狐 帖子:265 积分:1837 威望:0 精华:0 注册:2012/11/6 17:17:00
  发帖心情 Post By:2013/3/8 9:49:00 [只看该作者]

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


 回到顶部