这段代码语法没错,就是没达到预期的功能。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.商品名称)"
或者是其它的原因。帮我解决问题者,一定重分答谢,急啊!!!
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.商品名称)"
或者是其它的原因。帮我解决问题者,一定重分答谢,急啊!!!
解决方案 »
- vb文本框只能输入数字和小数点
- 使用shell运行外部程序,可否设置该外部程序的“起始位置”?
- 能给一个例子吗?例如:对COMBO控件,让其不执行任何事件,不能用ENABLED=FALSE,同时颜色不能为灰色.
- 我提供FTP让大家上传资料进行学习交流~ftp://pdc.csmud.com
- 怎样列举出SQL中所有用户建立的数据库
- 请问大家这是什么控件?
- 请教:如何将PictureBox中的图形以二进制形势保存在Access数据库中,急,在线等候,回复必送分!
- 谁有这样的例子,我给500分
- vb ado 字符串查询
- 具体解释一下这段程序的作用
- 怎么把这些数据弄进数据库里
- ‘高价’收购一SQL语句~~~:)
strUpdate = "update product set 库存 = (select sum(数量) from purchase where " & _
"商品名称=product.商品名称 group by 商品名稱) - (select sum(数量) from sale where " & _
" 商品名称=product.商品名称 group by 商品名稱)"
xiaohua_app(xiaohua):按你的方法修改后,也不能解决问题Leftie(左手,为人民币服务) :你提的建议,是我应该改进的地方,但我首先还是想解决这个问题。感谢你们对我问题的关注,希望能继续帮我看看。
SQL SERVER返回错误信息没有?
=============================================
换成SELECT语句,判断试试
我是这样分析出错误的:
如果在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写这样的代码。
谢谢你们能帮我看一下。
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是没有问题的.你再看看问题在哪吧.