将两个查询存储为第三个变量

时间:2014-02-19 20:49:44

标签: database vb6 ms-access-2007

此代码出了什么问题:

Visual Basic 6.0,具有访问权限2007

Private Sub Command1_Click()
Dim Sell_tbl, Stock_Bottle, res As String


Sell_tbl = "SELECT Sum((Quantity)*12) FROM Sell_Detail Where Cateogry='Large'"
Stock_Bottle = "Select Sum(No_Of_Bottle) FROM Add_Bottle Where Cateogry='Large'"

res = ((Sell_tbl) - (Stock_Bottle))

Adodc1.RecordSource = Sell_tbl
Adodc1.Refresh
Adodc1.Caption = Adodc1.RecordSource
End Sub

类型不匹配错误

我尝试将其结果转换为其他数据类型,但它不起作用。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

这些都不是记录集,每个都是一个字符串:

Sell_tbl = "SELECT Sum((Quantity)*12) FROM Sell_Detail Where Cateogry='Large'"
Stock_Bottle = "Select Sum(No_Of_Bottle) FROM Add_Bottle Where Cateogry='Large'"

您需要以下内容:

Dim Sell_tbl As DAO.Recordset
Dim Stock_Bottle As DAO.Recordset

Set Sell_tbl = CurrentDB.Openrecordset _
    ("SELECT Sum((Quantity)*12) As Qty FROM Sell_Detail Where Cateogry='Large'")
Set Stock_Bottle = CurrentDB.Openrecordset _
    ("Select Sum(No_Of_Bottle) As Btl FROM Add_Bottle Where Cateogry='Large'")

res = Sell_tbl!Qty - Stock_Bottle!Btl

以上是粗略的概述,它可以做整理。

答案 1 :(得分:0)

错误的原因是声明:

s = ((Sell_tbl) - (Stock_Bottle))

如果您在该行上方查看,则将两个字符串变量设置为SQL - 这是文本而不是数字。 您需要使用这些sql字符串打开记录集,然后获取结果,然后执行数学运算。

答案 2 :(得分:0)

这就是我想要的......

Private Sub Command2_Click()
Dim con As New ADODB.Connection
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
     & App.Path & "\add_entry.mdb;Persist Security Info=False"

Dim rs As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
Dim result_hold As Integer

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'"

rs.Open large_tbl, con, adOpenDynamic, adLockOptimistic
rs1.Open sell_large, con, adOpenDynamic, adLockOptimistic

result_hold = CInt(rs.Fields(0).Value) - CInt(rs1.Fields(0).Value)
Text1.Text = CStr(result_hold)
End Sub

'如果你需要检索整个列使用循环或等等。但有一件事是记住两个来源 '永远不会附加单格......