关于多行填充一行代码问题。老师我的想法,一个表多行多列文本填充到另一表一行多列文本之中,举例说明,表A有7列,列名分别为年份列,月份列,格式为202308,科目代码,科目名称,科目余额_年初余额,科目余额_本月合计,科目余额_本年累计。表B为11列,年份列,月份列,行次1_年初余额,行次1_本月合计,行次1_本年累计,行次2_年初余额,行次2_本月合计,行次2_本年累计,行次3_年初余额,行次3_本月合计,行次3_本年累计。表A有3行内容,第1行月份202308,科目代码1001,第2行月份202308,科目代码1002,第3行月份202308,科目代码1003。填充至表B,表A的科目代码1001对应表B的行次1,A的科目代码1002对应表B的行次2,A的科目代码1003对应表B的行次3,这个对应关系必须手工指定。请问老师,用按钮,代码如何书写
Dim s As String = e.Form.Controls("年月").text '选择月份
For Each v As String() In DataTables("表A").GetValues("年份|月份", "月份='" & s & "'")
Dim dr As DataRow = DataTables("表B").AddNew()
If dr IsNot Nothing Then
dr("年份") = v(0)
dr("月份") = v(1)
dr("行次1_年初余额") = DataTables("表A").select("科目余额_年初余额","年份='" & v(0) & "' And 月份='" & v(1) & "' and 科目代码='1001'")
dr("行次1_本年累计") = DataTables("表A").select("科目余额_本年累计","年份='" & v(0) & "' And 月份='" & v(1) & "' and 科目代码='1001'")
dr("行次1_本月合计") = DataTables("表A").select("科目余额_本月合计","年份='" & v(0) & "' And 月份='" & v(1) & "' and 科目代码='1001'")
...
End If
Next
老师,请帮忙一下
[此贴子已经被作者于2024/6/22 8:09:00编辑过]
比如,行次1对应科目代码1001,行次2对应1002,行次3对应1003,行次4对应2001,总之对应关系用代码确定
find肯定可以,先看看用法:
http://www.foxtable.com/webhelp/topics/0396.htm,不明白用了这么多次了,还不懂怎么用!
代码该分行就分行,严格按语法使用,不是随便可以省略的
dim dr as ..... = 某表.find(......
if dr isnot nothing then
某处赋值 = dr("某列")
老师,还是出现错误提示,麻烦老师耐心看看
Cannot find column 年份 = '2023' And 月份 = '202308' and 科目代码 = '1001'.其代码如下:
Dim s As String = e.Form.Controls("年月").text '选择月份
For Each v As String() In DataTables("表A1").GetValues("年份|月份", "月份='" & s & "'")
Dim dr As DataRow = DataTables("表B1").AddNew()
dr("年份") = v(0)
dr("月份") = v(1)
Dim dr1 As DataRow = DataTables("表A1").Find("科目代码", "年份 = '" & v(0) & "' And 月份 = '" & v(1) & "' and 科目代码 = '1001'")
If dr1 IsNot Nothing Then
dr("行次1_年初余额") = dr1("科目余额_年初余额")
dr("行次1_本年累计") = dr1("科目余额_本年累计")
dr("行次1_本月合计") = dr1("科目余额_本月合计")
End If
Dim dr2 As DataRow = DataTables("表A1").Find("科目代码", "年份 = '" & v(0) & "' And 月份 = '" & v(1) & "' and 科目代码 = '1002'")
If dr2 IsNot Nothing Then
dr("行次2_年初余额") = dr2("科目余额_年初余额")
dr("行次2_本年累计") = dr2("科目余额_本年累计")
dr("行次2_本月合计") = dr2("科目余额_本月合计")
End If
Dim dr3 As DataRow = DataTables("表A1").Find("科目代码", "年份 = '" & v(0) & "' And 月份 = '" & v(1) & "' and 科目代码 = '2003'")
If dr3 IsNot Nothing Then
dr("行次3_年初余额") = dr3("科目余额_年初余额")
dr("行次3_本年累计") = dr3("科目余额_本年累计")
dr("行次3_本月合计") = dr3("科目余额_本月合计")
End If
Next
[此贴子已经被作者于2024/6/22 13:09:43编辑过]
Find的用法再看上10遍
语法:
Find(Filter,Sort,Index)
Filter: 条件表达式,请参考表达式的运算符和函数 和 条件表达式。
Sort: 可选参数,指定排序方式。
Index: 可选参数,指定返回第几个符合条件的行,0表示第一行。
Dim s As String = e.Form.Controls("年月").text '选择月份
For Each v As String() In DataTables("表A1").GetValues("年份|月份", "月份='" & s & "'")
Dim dr As DataRow = DataTables("表B1").AddNew()
dr("年份") = v(0)
dr("月份") = v(1)
Dim dr1 As DataRow = DataTables("表A1").Find("年份 = '" & v(0) & "' And 月份 = '" & v(1) & "' and 科目代码 = '1001'")
If dr1 IsNot Nothing Then
dr("行次1_年初余额") = dr1("科目余额_年初余额")
dr("行次1_本年累计") = dr1("科目余额_本年累计")
dr("行次1_本月合计") = dr1("科目余额_本月合计")
End If
Dim dr2 As DataRow = DataTables("表A1").Find("年份 = '" & v(0) & "' And 月份 = '" & v(1) & "' and 科目代码 = '1002'")
If dr2 IsNot Nothing Then
dr("行次2_年初余额") = dr2("科目余额_年初余额")
dr("行次2_本年累计") = dr2("科目余额_本年累计")
dr("行次2_本月合计") = dr2("科目余额_本月合计")
End If
Dim dr3 As DataRow = DataTables("表A1").Find("年份 = '" & v(0) & "' And 月份 = '" & v(1) & "' and 科目代码 = '2003'")
If dr3 IsNot Nothing Then
dr("行次3_年初余额") = dr3("科目余额_年初余额")
dr("行次3_本年累计") = dr3("科目余额_本年累计")
dr("行次3_本月合计") = dr3("科目余额_本月合计")
End If
Next
不好意思,让老师再三提醒才知道错在哪里了。代码现在正确了,谢谢老师
[此贴子已经被作者于2024/6/22 14:24:33编辑过]