这段代码语法没错,就是没达到预期的功能。Private Sub Command1_Click()
         Dim conn As New ADODB.Connection
         Dim strUpdate As String
         Dim connstring As String
                    '先判断sale表中有与purduct表对应的记录
         adoKCPD.Recordset.Find "商品名称='sale.商品名称'" 
                     '没有的话    
            If adoKCPD.Recordset.EOF Then                    
   
     strUpdate = "update product set 库存 = (select sum(数量) from purchase where " & _
               "商品名称=product.商品名称)"
              Else
                     ’有对应的记录
      strUpdate = "update product set 库存 = (select sum(数量) from purchase where " & _
                "商品名称=product.商品名称) - (select sum(数量) from sale where " & _
               " 商品名称=product.商品名称)"               End If
   connstring = "Provider=SQLOLEDB.1;Password=ecc;Persist Security " _
                & "Info=True;User ID=sa;Initial Catalog=PurchaseandSale;Server=(local)"
   If conn.State <> 1 Then            '打开数据库
        conn.Open (connstring)
    End If
    conn.Execute (strUpdate)            '执行库存盘点操作
    conn.Close
   adoKCPD.Refresh                     '刷新库存End Sub根据运行结果分析:好象是这段代码没运行
       strUpdate = "update product set 库存 = (select sum(数量) from purchase where " & _
                "商品名称=product.商品名称) - (select sum(数量) from sale where " & _
               " 商品名称=product.商品名称)"
或者是其它的原因。帮我解决问题者,一定重分答谢,急啊!!!

解决方案 »

  1.   

    怎么沒有GROUP by ???
           strUpdate = "update product set 库存 = (select sum(数量) from purchase where " & _
                    "商品名称=product.商品名称 group by 商品名稱) - (select sum(数量) from sale where " & _
                   " 商品名称=product.商品名称 group by 商品名稱)"
      

  2.   

    of123() :代码中的UPDATE,应该没有跨表吧!,而出现的问题中也不太能说明是跨表问题
    xiaohua_app(xiaohua):按你的方法修改后,也不能解决问题Leftie(左手,为人民币服务) :你提的建议,是我应该改进的地方,但我首先还是想解决这个问题。感谢你们对我问题的关注,希望能继续帮我看看。
      

  3.   

    没有运行?
    SQL SERVER返回错误信息没有?
      

  4.   

    update跨表也应该没问题,只要update的是同一个表的
      

  5.   

    仔细看了下,发现楼主该不会你说的那个是else下面的,if else 结构只发生一个,再看看有错吗
      

  6.   

    adoKCPD.Recordset.Find "商品名称='sale.商品名称'" 
    =============================================
    换成SELECT语句,判断试试
      

  7.   

    wumylove1234(毁于随):没有任何的错误提示,我是从数据库表中看出错误的!
            我是这样分析出错误的:
                      如果在sale 表中有对应记录,就应该会运行如下的语句,
           strUpdate = "update product set 库存 = (select sum(数量) from purchase where " & _
                    "商品名称=product.商品名称) - (select sum(数量) from sale where " & _
                   " 商品名称=product.商品名称)"
            但是,在库存中得到的结果并没有执行这个语句,而都是执行
                strUpdate = "update product set 库存 = (select sum(数量) from purchase where " & _
                   "商品名称=product.商品名称)"         得到的结果。
      aiur2000(闭关失败,走火入魔,开关拉!) :如果运行了的话,不应该会得到那样的结果。不过可能也有别的原因。不过我不太清楚。
    yuanbowr(明月无心) :我不太善于用SELECT写这样的代码。
       谢谢你们能帮我看一下。
      

  8.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[product]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[product]
    GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[purchase]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[purchase]
    GO
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sale]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[sale]
    GO
    create table product
    (goodsid int,
    goodsnum float)create table purchase
    (goodsid int,
    goodsnum float)create table sale
    (goodsid int,
    goodsnum float)insert into product values(1,0)
    insert into product values(2,0)
    insert into purchase values(1,10)
    insert into purchase values(2,20)
    insert into sale values(1,5)
    insert into sale values(2,10)update product set goodsnum=(select sum(goodsnum) from purchase where goodsid=product.goodsid)
    -(select sum(goodsnum) from sale where goodsid=product.goodsid)
    select * from product
    drop table product
    drop table purchase
    drop table salegoodsid     goodsnum                                              
    ----------- ----------------------------------------------------- 
    1           5.0
    2           10.0(所影响的行数为 2 行)
    从结果上看,上面的SQL是没有问题的.你再看看问题在哪吧.