找到两列不同表的总和,然后减去它们的总和

时间:2014-02-16 20:33:35

标签: sql vb6 ms-access-2007

嗨我想做一个查询,首先找到不同表的两列的总和然后减去它 例 这是一个视觉基础程序

Dim large_tbl As String
Dim sell_large As String

large_tbl = "SELECT Sum(No_Of_Bottle) FROM add_cotton where Cateogry='Large'"
sell_large = "SELECT Sum(Quantity) FROM Sell_Detail where Cateogry='Large'"

Adodc2.RecordSource = large_tbl - sell_large

请帮忙 它显示错误类型未命中匹配

1 个答案:

答案 0 :(得分:2)

两个变量large_tblsell_large是字符串 - 就VB而言只是文本。您需要连接到数据源(即Sql Server)并执行查询以获得数值结果。

你要求计算机做的是从“blah”中减去“blah”并执行它。

我猜你的意思更像是:

Dim oCNX as ADODB.Connection

:----Put code in here to open the connection to your database

Dim resultA as ADODB.Recordset
Dim resultB as ADODB.Recordset

Dim large_tbl As String
Dim sell_large As String

large_tbl = "SELECT Sum(No_Of_Bottle) FROM add_cotton where Cateogry='Large'"
sell_large = "SELECT Sum(Quantity) FROM Sell_Detail where Cateogry='Large'"

Set resultA = oCNX.Execute(large_tbl)
Set resultB = oCNX.Execute(sell_large)

Do while not oRS.Eof()

   Debug.Print "Result =" & (resultA(1).Value - resultB(1).Value)

   oRS.MoveNext
Loop

或类似的东西。