求助!datagrid列值求和
各位大神,我有一个正在做的小软件,基本做完了就差最后一步,需要将查询与筛选出来显示在datagrid控件上的数据的某一列求值在text6中,之前有找人做过,但是感觉不行,我的想法是不管datagrid控件中的数据有一丁点的变化,那一列的求和值都能随着datagrid的变化而变化。求救,附上附件。
说明书统计.zip
(18.8 KB)
Public Sub DataGridReredsh(SqlWhere As String) On Error Resume Next Dim sql As String, sql2 As String If Len(SqlWhere) > 0 Then sql2 = "where " & SqlWhere '如果传入了条件,那么组合上条件动词,否则为空 sql = "select sum(客户总价) as s1 ,sum(加工商总价) as s2 from SMS " & sql2 '生成求和的SQL If rs.State <> adStateClosed Then '如果rs没有关闭的情况下,先关掉 rs.Close DoEvents '防止超时未关闭 rs End If rs.CursorLocation = adUseClient rs.Open sql, conn, adOpenKeyset, adLockOptimistic '打开 rs Text6.Text = rs.Fields("s1") '读值 Text7.Text = rs.Fields("s2") rs.Close '用完关 DoEvents sql = "select * from SMS " & sql2 '生成显示数据用的 rs rs.CursorLocation = adUseClient rs.Open sql, conn, adOpenKeyset, adLockOptimistic Set DataGrid1.DataSource = rs.DataSource DataGrid1.Refresh End Sub
Private Sub Command4_Click() On Error GoTo ToExit '打开错误陷阱 '------------------------------------------------ DataGridReredsh "" '刷新数据,并统计 MsgBox "数据刷新成功!", 64, "提示" '------------------------------------------------ Exit Sub '---------------- ToExit: Resume Next End Sub
Private Sub Form_Load() On Error GoTo ToExit '打开错误陷阱 '------------------------------------------------ Combo1.AddItem "彩雅丰" Combo1.AddItem "富康美" Combo1.AddItem "杰事杰" Combo1.AddItem "浩晨" Combo1.Text = Combo1.List(0) Call KKK(conn) ' If rs.State <> adStateClosed Then ' rs.Close ' End If ' rs.CursorLocation = adUseClient ' rs.Open "select * from SMS where 工单 like '%" + Text1.Text + "%' and 加工商 like '%" + Combo1.Text + "%'", conn, adOpenKeyset, adLockOptimistic ' Set DataGrid1.DataSource = rs.DataSource DataGridReredsh (" 工单 like '%" + Text1.Text + "%' and 加工商 like '%" + Combo1.Text + "%'") '------------------------------------------------ Exit Sub '---------------- ToExit: Resume Next End Sub
[此贴子已经被作者于2016-11-26 12:09编辑过]