|
非常感谢tzh1600的帮助,但是我是想要在那个合计查询之前先汇总一下,然后再列出所有的三单记录。
我现在用新建表,然后删除的方法,不知道有没有其他办法。
- Private Sub CommandButton1_Click()
- Sheet1.Range("A1:L100").ClearContents
- Dim sql1, sql2, sql
- Dim sum, sName
- cnn.Open "Provider=Microsoft.Jet.Oledb.4.0;data Source=" & ThisWorkbook.Path & "\data.MDB "
- sql1 = "select 销售清单号,公司名称,sum(销售金额) as 金额, 付款情况 from [XSD] where 付款情况 like 'NotPaid' group by 公司名称,销售清单号,付款情况"
- sql = " SELECT 销售清单号,公司名称,金额,付款情况 INTO XSD_bake FROM (" & sql1 & ")"
- cnn.Execute (sql)
- sql = "SELECT XSD_bake.公司名称, XSD_bake.金额, A.金额, B.金额, C.金额, XSD_bake.金额 + A.金额+ B.金额+ C.金额 AS hj, XSD_bake.销售清单号, A.销售清单号, B.销售清单号, C.销售清单号, XSD_bake.付款情况 FROM ((XSD_bake INNER JOIN XSD_bake AS A ON XSD_bake.公司名称 = A.公司名称) INNER JOIN XSD_bake AS B ON A.公司名称 = B.公司名称) INNER JOIN XSD_bake AS C ON B.公司名称 = C.公司名称 WHERE (XSD_bake.金额 + A.金额 + B.金额+ C.金额 = 6000) And (A.销售清单号 > XSD_bake.销售清单号) And (B.销售清单号 > A.销售清单号) And (C.销售清单号 > B.销售清单号) ORDER BY XSD_bake.公司名称, XSD_bake.销售清单号;"
- rst.Open sql, cnn, adOpenKeyset, adLockOptimistic
- If rst.EOF = False Then
- For i = 1 To rst.RecordCount
- Sheet1.Cells(i, 1) = rst.Fields(0).Value
- Sheet1.Cells(i, 2) = rst.Fields(1).Value
- Sheet1.Cells(i, 3) = rst.Fields(2).Value
- Sheet1.Cells(i, 4) = rst.Fields(3).Value
- Sheet1.Cells(i, 5) = rst.Fields(4).Value
- Sheet1.Cells(i, 6) = rst.Fields(5).Value
- Sheet1.Cells(i, 7) = rst.Fields(6).Value
- Sheet1.Cells(i, 8) = rst.Fields(7).Value
- Sheet1.Cells(i, 9) = rst.Fields(8).Value
- Sheet1.Cells(i, 10) = rst.Fields(9).Value
- Sheet1.Cells(i, 11) = rst.Fields(10).Value
- rst.MoveNext
- Next i
- End If
- rst.Close: Set rst = Nothing
- sql = "DROP TABLE " & "XSD_bake"
- cnn.Execute (sql)
- cnn.Close: Set cnn = Nothing
- End Sub
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|