【问题标题】:Store two Queries result in third variable将两个查询结果存储在第三个变量中
【发布时间】:2014-03-20 11:17:20
【问题描述】:

这段代码有什么问题:

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

类型不匹配错误

我尝试将其结果转换为其他数据类型,但它不起作用。谁能帮帮我?

【问题讨论】:

标签: database vb6 ms-access-2007


【解决方案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

以上是粗略的提纲,可以整理一下。

【讨论】:

  • currentdb是什么意思?请在我的程序帮助中未显示该选项。
  • 你用的是什么程序? CurrentDb 在 MS Access 模块中可用。
  • 我使用visual basic 6.0作为前端,访问作为后端
  • 还有一个问题,您使用 DAO 连接 db,ADODB 或 adodc 怎么样?我对 DAO 不太了解,所以我的首要任务是 adodc,请 ADODB 帮助
  • 使用 MS Access support.microsoft.com/kb/148361DAO 几乎总是更快
【解决方案2】:

错误的原因是因为声明:

s = ((Sell_tbl) - (Stock_Bottle))

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

【讨论】:

  • 谢谢,我知道了导致失败的原因
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多