以文本方式查看主题

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

--  作者:nbsugu_z
--  发布时间:2017/7/24 12:26:00
--  求助
列为:A单位本期  A单位上期   B单位本期  B单位上 期  ............
行为月份 如 1月 2月 3月......
这样 一张数据表,  要做的是每增加一行(月份),本月有上期数据=上月的本期数。 每个 单位都能自动取上月的本期数作为当月的上期数。

代码如何写?
参照流水帐写出过,也成功过,但是要每个单位都写上代码(计算公式),这样单位多就麻烦 了,有没有更简单的方法,能自动把所有列的 上期数自动
[此贴子已经被作者于2017/7/24 12:31:21编辑过]

--  作者:有点甜
--  发布时间:2017/7/24 14:30:00
--  

DataColChanted

 

If e.DataCol.name = "月份" Then
    Dim fdr As DataRow = e.DataTable.Find("月份 < \'" & e.DataRow("月份") & "\'", "月份 desc")
   
    If fdr IsNot Nothing Then
        For Each dc As DataCol In e.DataTable.datacols
            If dc.Name Like "*上期*" Then
                e.DataRow(dc.name.replace("上期", "本期")) = fdr(dc.name)
            End If
        Next
    End If
End If


--  作者:nbsugu_z
--  发布时间:2017/7/25 10:19:00
--  
谢谢,试用后比我原来简单多了,尽管有点不太懂。如果再增加多列,分别是每个单位的本期减上期(统计增减差额),代码要如何加呢?
--  作者:有点甜
--  发布时间:2017/7/25 10:31:00
--  

If e.DataCol.name Like "*本期*" OrElse e.DataCol.name Like "*上期*" Then

    Dim name As String = e.Datacol.Name.Replace("本期", "").Replace("上期", "")
    e.DataRow(name & "差额") = e.DataRow(name & "本期") - e.DataRow(name & "上期")
End If

[此贴子已经被作者于2017/7/25 10:31:35编辑过]

--  作者:nbsugu_z
--  发布时间:2017/7/25 12:51:00
--  
解决,谢谢老师
--  作者:nbsugu_z
--  发布时间:2017/7/25 13:07:00
--  
按上面的方法设计了一个电表抄表计算。二个数据表
 表A为各电表编号 全数字,如1123,列名为:编号、价格
表B:列名为:  月份   D1123dj    d1124dj  d1125dj .........
现在要每增加一个月份(增加一行)后,表B的对应的单价能从表A中获取。
因表B列名不能以数字开头,加了一个“D”,因为以后还有相应倍率的列行增加,所以单价后面加了个“ DJ"

现在写了如下代码,提示未将对象引用设置到对象实例,进入死环,请指教
If e.DataCol.name = "月" Then
    If e.DataCol.name Like "*dj*"  Then

    Dim name1 As String = Left(e.DataCol.Name.Replace("dj", ""),1) \'去掉后二位dj和第一位字符得到编号
    Dim drb As DataRow
                drb = DataTables("表A").Find("编号 = \'name1\'") \'找到表识别中编号为name1的行
                e.DataRow( "d"  &  name1 & "dj")  = drb("价格")   \'自动从"表识别"取数
     End if 
end if

以上 一个一个取数我已经会做的,现在想快速点,因为表B的电表列名去掉第一个字符和最后二个字符 就是表A的编号,有这个关系在,想直接一次性解决,减少代码,因为电表有很多。

[此贴子已经被作者于2017/7/25 13:28:08编辑过]

--  作者:有点蓝
--  发布时间:2017/7/25 15:37:00
--  
If e.DataCol.name = "月" Then
    For Each c As DataCol In e.DataTable.DataCols
        If c.name Like "*dj*"  Then
            Dim name1 As String = Left(e.DataCol.Name.Replace("dj", ""),1) \'去掉后二位dj和第一位字符得到编号
            Dim drb As DataRow
            drb = DataTables("表A").Find("编号 = \'" & name1 & "\'") \'找到表识别中编号为name1的行
            If drb IsNot Nothing Then
                e.DataRow(c.name)  = drb("价格")   \'自动从"表识别"取数
            End If
        End If
    Next
End If

--  作者:nbsugu_z
--  发布时间:2017/7/25 16:16:00
--  
运行后取不到价格

--  作者:有点甜
--  发布时间:2017/7/25 16:41:00
--  
以下是引用nbsugu_z在2017/7/25 16:16:00的发言:
运行后取不到价格

 

运行下面的代码,执行,弹出什么?

 

If e.DataCol.name = "月" Then
    For Each c As DataCol In e.DataTable.DataCols
msgbox(c.name)
        If c.name Like "*dj*"  Then
            Dim name1 As String = Left(e.DataCol.Name.Replace("dj", ""),1) \'去掉后二位dj和第一位字符得到编号
            Dim drb As DataRow
msgbox(name1)
            drb = DataTables("表A").Find("编号 = \'" & name1 & "\'") \'找到表识别中编号为name1的行
            If drb IsNot Nothing Then
msgbox(drb("价格"))
                e.DataRow(c.name)  = drb("价格")   \'自动从"表识别"取数
            End If
        End If
    Next
End If

--  作者:nbsugu_z
--  发布时间:2017/7/25 17:07:00
--  
If e.DataCol.name = "月" Then
    For Each c As DataCol In e.DataTable.DataCols
msgbox(c.name)     一直在弹出所有列名,  一直按确定,关不了机,只有电脑重启,下面的代码好象没执行
        If c.name Like "*dj*"  Then
            Dim name1 As String = Left(e.DataCol.Name.Replace("dj", ""),1) \'去掉后二位dj和第一位字符得到编号
            Dim drb As DataRow
msgbox(name1)
            drb = DataTables("表A").Find("编号 = \'" & name1 & "\'") \'找到表识别中编号为name1的行
            If drb IsNot Nothing Then
msgbox(drb("价格"))
                e.DataRow(c.name)  = drb("价格")   \'自动从"表识别"取数
            End If
        End If
    Next
End If

一直在弹出所有列名,  一直按确定,重复弹出所有列名,关不了机,只有电脑重启,正面的代码好象没执行
[此贴子已经被作者于2017/7/25 17:09:41编辑过]