放在项目 maintablechanged 事件中:(你自己优化吧)
DataTables("表B").datarows.clear()
Dim n As Integer
For i As Integer = 0 To Tables("表A").count-1
If i = 0
Tables("表B").addnew()
Tables("表B").Rows(n)("起号") = Tables("表A").Rows(i)("起号")
Tables("表B").Rows(n)("序号") = n+1
n = n + 1
Else
If i = Tables("表A").count-1
Tables("表B").addnew()
Tables("表B").Rows(n-1)("止号") = Tables("表A").Rows(i)("止号")
Tables("表B").Rows(n-1)("全部单证张数") = Tables("表A").Compute("Sum(张数)","起号 >= '" & _
Tables("表B").Rows(n-1)("起号") & "' and 起号 <= '" & Tables("表A").Rows(i)("起号") & "' ")
Tables("表B").Rows(n-1)("售单张数") = Tables("表A").Compute("Sum(张数)","单证状态 = '售单' and 起号 >= '" & _
Tables("表B").Rows(n-1)("起号") & "' and 起号 <= '" & Tables("表A").Rows(i)("起号") & "' ")
Tables("表B").Rows(n-1)("退废单张数") = Tables("表A").Compute("Sum(张数)","(单证状态 = '退单' or 单证状态 = '废单') and 起号 >= '" & _
Tables("表B").Rows(n-1)("起号") & "' and 起号 < '" & Tables("表A").Rows(i)("起号") & "' ")
Tables("表B").Rows(n-1)("售单金额") = Tables("表A").Compute("Sum(保费)","单证状态 = '售单' and 起号 >= '" & _
Tables("表B").Rows(n-1)("起号") & "' and 起号 <= '" & Tables("表A").Rows(i)("起号") & "' ")
Tables("表B").Rows(n)("序号") = "合计"
Tables("表B").Rows(n)("全部单证张数") = Tables("表B").Compute("Sum(全部单证张数)")
Tables("表B").Rows(n)("售单张数") = Tables("表B").Compute("Sum(售单张数)")
Tables("表B").Rows(n)("退废单张数") = Tables("表B").Compute("Sum(退废单张数)")
Tables("表B").Rows(n)("售单金额") = Tables("表B").Compute("Sum(售单金额)")
Else
If val(Tables("表A").Rows(i)("起号")) - val(Tables("表A").Rows(i-1)("止号")) > 1
Tables("表B").addnew()
Tables("表B").Rows(n-1)("止号") = Tables("表A").Rows(i-1)("止号")
Tables("表B").Rows(n)("起号") = Tables("表A").Rows(i)("起号")
Tables("表B").Rows(n-1)("全部单证张数") = Tables("表A").Compute("Sum(张数)","起号 >= '" & _
Tables("表B").Rows(n-1)("起号") & "' and 起号 < '" & Tables("表B").Rows(n)("起号") & "' ")
Tables("表B").Rows(n-1)("售单张数") = Tables("表A").Compute("Sum(张数)","单证状态 = '售单' and 起号 >= '" & _
Tables("表B").Rows(n-1)("起号") & "' and 起号 < '" & Tables("表B").Rows(n)("起号") & "' ")
Tables("表B").Rows(n-1)("退废单张数") = Tables("表A").Compute("Sum(张数)","(单证状态 = '退单' or 单证状态 = '废单') and 起号 >= '" & _
Tables("表B").Rows(n-1)("起号") & "' and 起号 < '" & Tables("表B").Rows(n)("起号") & "' ")
Tables("表B").Rows(n-1)("售单金额") = Tables("表A").Compute("Sum(保费)","单证状态 = '售单' and 起号 >= '" & _
Tables("表B").Rows(n-1)("起号") & "' and 起号 < '" & Tables("表B").Rows(n)("起号") & "' ")
Tables("表B").Rows(n)("序号") = n+1
n = n + 1
End If
End If
End If
Next