不知道是VB问题还是SQL 语句还是ACCESS的问题请高人指点
表ListId DId OId mount
363 81 70 5000
364 81 80 1600
365 81 78 1800
367 81 800 1800
370 81 74 1200
371 81 71 1200
372 81 60 13000
373 81 68 13000
374 81 62 3300
表Draw
Id CreateDate DepId Flag
81 2018-9-29 17 0
表Store
OId Name mount
70 c1 8000
80 c2 3000
78 c3 5000
800 c4 6000
74 c5 7000
71 c6 3000
60 c7 20000
68 c8 20000
62 c9 4000
Draw.Id=List.DId
List.OId=Store.OId
将表List中的DId为81的行mount字段数字对表Store里的mount数字进行加减运算,但是运行了5个后数据库自动关闭了
VB提示:错误3704 ,对象关闭时不允许使用
代码如下
List类模块代码
'按照领用单号读取所有明细,并更改库存数量
Public Sub UpdateStore(ByVal TmpId As Long)
Dim rs As New ADODB.Recordset '定义结果集对象
Dim lAmount As Integer '领用数量
Dim TmpOId As Long '生产辅料编号
'设置SELECT语句,读取编号为TmpId的记录
SqlStmt = "SELECT * FROM DrawList WHERE DId=" + Trim(TmpId)
'将结果集读取到rs中
Set rs = QueryExt(SqlStmt)
Do While Not rs.EOF
MyStore.mount = 0 - rs.Fields(3) '领用数量
MyStore.UpdateAmount (rs.Fields(2)) '生产辅料编号
rs.MoveNext
Loop
End Sub
Store类模块代码
'更新库存数量
Public Sub UpdateAmount(ByVal OriOId As Long)
SqlStmt = "UPDATE Store Set mount=mount+" + Trim(mount) _
+ " WHERE OId=" + Trim(Str(OriOId))
SQLExt (SqlStmt)
End Sub
'审核结算 当表Draw中Flag为0时允许
Private Sub Cmd_Check_Click()
If Adodc1.Recordset.EOF Then
MsgBox "请选择记录"
Exit Sub
End If
On Error Resume Next
If Adodc1.Recordset.Fields(4) = 0 Then
'确认后更改领用单标志Flag值为1
MyDraw.UpdateFlag (Adodc1.Recordset.Fields(0))
'按照领用单明细更改当前库存数量
MyList.UpdateStore (Adodc1.Recordset.Fields(0))
DataRefresh
Else
MsgBox "已经领用"
End If
End Sub
模块DbFunc代码
'== 标记数据库是否连接 ==
Private IsConnect As Boolean
'== 标记执行Connect()函数后,访问数据库的次数 ==
Private Connect_Num As Integer
Private cnn As ADODB.Connection '连接数据库的Connection对象
Private rs As ADODB.Recordset '保存结果集的Recordset对象
'连接数据库
Private Sub Connect()
'如果连接标记为真,则返回。否则会出错
If IsConnect = True Then
Exit Sub
End If
'关键New用于创建新对象cnn
Set cnn = New ADODB.Connection
'设置连接字符串ConnectionString属性
cnn.ConnectionString = Conn
'打开到数据库的连接
cnn.Open
'判断连接的状态
If cnn.State <> adStateOpen Then
'如果连接不成功,则显示提示信息,退出程序
MsgBox "数据库连接失败"
End
End If
'设置连接标记,表示已经连接到数据库
IsConnect = True
End Sub
'断开与数据库的连接
Private Sub Disconnect()
Dim rc As Long
'如果连接标记为假,标明已经断开连接,则直接返回
If IsConnect = False Then
Exit Sub
End If
'关闭连接
cnn.Close
'释放cnn
Set cnn = Nothing
'设置连接标记,表示已经断开与数据库的连接
IsConnect = False
End Sub
'使用Connect_Num控制数据库连接
Public Sub DB_Connect()
Connect_Num = Connect_Num + 1
Connect
End Sub
Public Sub DB_Disconnect()
If Connect_Num >= CONNECT_LOOP_MAX Then
Connect_Num = 0
Disconnect
End If
End Sub
'强制关闭api方式访问的数据库,计数器复位
Public Sub DBapi_Disconnect()
Connect_Num = 0
Disconnect
End Sub
'执行数据库操作语句
Public Sub SQLExt(ByVal TmpSQLstmt As String)
'创建Command对象cmd
Dim cmd As New
'连接到数据库
DB_Connect
'设置cmd的ActiveConnection属性,指定与其关联的数据库连接
Set cmd.ActiveConnection = cnn
'设置要执行的命令文本
= TmpSQLstmt
'MsgBox TmpSQLstmt
'执行命令
cmd.Execute
'清空cmd对象
Set cmd = Nothing
'断开与数据库的连接
DB_Disconnect
End Sub
'执行数据库查询语句
Public Function QueryExt(ByVal TmpSQLstmt As String) As ADODB.Recordset
'创建Recordset对象rst
Dim rst As New ADODB.Recordset
'连接到数据库
DB_Connect
'设置rst的ActiveConnection属性,指定与其关联的数据库连接
Set rst.ActiveConnection = cnn
'设置游标类型
rst.CursorType = adOpenDynamic
'设置锁定类型
rst.LockType = adLockOptimistic
'打开记录集
rst.Open TmpSQLstmt
'返回记录集
Set QueryExt = rst
End Function
不能全部冲减Store表中mount库存数据,只能结算到表List中DId为81的5条数据,到第6条时提示对象库关闭时不允许操作