标题:二表SUM后合并,为何得出错误结果?
只看楼主
cwa9958
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:247
专家分:1228
注 册:2006-6-25
得分:0 
回复 8楼 xxwyyf007
根据你的要求,做个例子。

程序代码:
Private Sub Command2_Click()
'填写表头
    MSHFlexGrid3.Cols = 5
    MSHFlexGrid3.Col = 1
    MSHFlexGrid3.Row = 0
    MSHFlexGrid3.Text = "编号"
    MSHFlexGrid3.Col = 2
    MSHFlexGrid3.Text = "规格"
    MSHFlexGrid3.Col = 3
    MSHFlexGrid3.Text = "已生产"
    MSHFlexGrid3.Col = 4
    MSHFlexGrid3.Text = "已包装"
    
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\rc.mdb"
   '//////////////////////////
   Set rs1 = New ADODB.Recordset
        rs1.Open "select 编号,规格,sum(数量) as 已生产 from sc group by  编号,规格 ", cn, adOpenStatic, adLockOptimistic
        If rs1.RecordCount > 0 Then
        Set MSHFlexGrid1.DataSource = rs1
        
        MSHFlexGrid3.Rows = rs1.RecordCount + 1   '设置表格行数
        
    Else
         Exit Sub
    End If
   '///////////////////////////
   Set rs2 = New ADODB.Recordset
        rs2.Open "select 编号,sum(数量) as 已包装 from bz group by  编号 ", cn, adOpenStatic, adLockOptimistic
        If rs2.RecordCount > 0 Then
        Set MSHFlexGrid2.DataSource = rs2
    Else
         Exit Sub
    End If
    '///////////////////////////
    Dim i As Integer, j As Integer
    Do While Not rs1.EOF
        i = i + 1
        MSHFlexGrid3.Row = i
        For j = 0 To 2
            MSHFlexGrid3.Col = j + 1
            MSHFlexGrid3.Text = rs1(j)
            
        Next
        MSHFlexGrid3.Col = 4
        rs2.MoveFirst
        Do While Not rs2.EOF
            If rs1(0) = rs2(0) Then
                MSHFlexGrid3.Text = rs2(1)
                Exit Do
               Else
                MSHFlexGrid3.Text = 0
            End If
            rs2.MoveNext
        Loop
        
        rs1.MoveNext
    
    Loop

  
    rs1.Close
    rs2.Close

    cn.Close
    Set rs1 = Nothing
    Set rs2 = Nothing

    Set cn = Nothing
End Sub
收到的鲜花
  • xxwyyf0072020-08-13 21:24 送鲜花  5朵  
2020-08-13 11:06
xxwyyf007
Rank: 2
等 级:论坛游民
帖 子:24
专家分:14
注 册:2016-4-26
得分:0 
回复 11楼 cwa9958
测试通过,非常的感谢!!!
2020-08-13 21:23
xxwyyf007
Rank: 2
等 级:论坛游民
帖 子:24
专家分:14
注 册:2016-4-26
得分:0 
回复 10楼 风吹过b
感谢提供的方法,我再学习一下!
2020-08-13 21:25
xxwyyf007
Rank: 2
等 级:论坛游民
帖 子:24
专家分:14
注 册:2016-4-26
得分:0 
可以结贴了,但提示“零分帖无需结贴”,那我也不会弄了
2020-08-13 21:28
nmcfbgttyl
Rank: 2
等 级:论坛游民
帖 子:11
专家分:15
注 册:2013-1-2
得分:0 
回复 4楼 风吹过b
老师,这样查询丢掉了 只有生产数量,没有已包装数量的 A03  和 没有生产数量,只有已包装数量的 B02 ,也就是说不同时在两表中的  都没出现:


这应该怎样改呢?

[此贴子已经被作者于2020-11-18 20:49编辑过]

2020-11-18 20:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-502661-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.145968 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved