|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
不好意思,初学VBA,所以问题可能令高手们取笑了。
我的意思是想在sheet(入库单)的6-17行输入内容,然后在表“库存单”中自动录入,如果输入的内容在表“库存单”中已经有了,则只改变库存量,如果没有,则增加新的记录。可现在运行时总提示说“在EOF戓BOF中有一个为真,或该记录已被删除”,不知道是什么原因。请高手指点迷津!!!在此先谢了!代码如下:
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim pthstr As String
Dim sql As String
pthstr = ThisWorkbook.Path & "\shuku.mdb"
cnn.Open "provider=microsoft.jet.oledb.4.0;data source=" & pthstr
sql = "select * from 库存单 where 字 like '" & Sheets("入库单").Range("h4").Value & "' "
rs.Open sql, cnn, adOpenKeyset, adLockOptimistic, adCmdText
For ii = 6 To 17
If Range("c" & ii).Value <> rs.Fields("科目").Value And Range("d" & ii).Value <> rs.Fields("年级").Value And Range("e" & ii).Value <> rs.Fields("书名").Value And Range("f" & ii).Value <> rs.Fields("册号").Value And Range("g" & ii).Value <> rs.Fields("版本").Value Then
rs.AddNew
rs.Fields("字").Value = IIf(Range("h4").Value = "", Null, Range("h4").Value)
rs.Fields("科目").Value = IIf(Range("c" & ii).Value = "", Null, Range("c" & ii).Value)
rs.Fields("年级").Value = IIf(Range("d" & ii).Value = "", Null, Range("d" & ii).Value)
rs.Fields("书名").Value = IIf(Range("e" & ii).Value = "", Null, Range("e" & ii).Value)
rs.Fields("册号").Value = IIf(Range("f" & ii).Value = "", Null, Range("f" & ii).Value)
rs.Fields("版本").Value = IIf(Range("g" & ii).Value = "", Null, Range("g" & ii).Value)
rs.Fields("库存数量").Value = IIf(Range("i" & ii).Value = "", Null, Range("i" & ii).Value)
rs.Fields("备注").Value = IIf(Range("k" & ii).Value = "", Null, Range("k" & ii).Value)
rs.Update
Set rs = Nothing
cnn.Close
Else
rs.AddNew
rs.Fields("字").Value = IIf(Range("h4").Value = "", Null, Range("h4").Value)
rs.Fields("科目").Value = IIf(Range("c" & ii).Value = "", Null, Range("c" & ii).Value)
rs.Fields("年级").Value = IIf(Range("d" & ii).Value = "", Null, Range("d" & ii).Value)
rs.Fields("书名").Value = IIf(Range("e" & ii).Value = "", Null, Range("e" & ii).Value)
rs.Fields("册号").Value = IIf(Range("f" & ii).Value = "", Null, Range("f" & ii).Value)
rs.Fields("版本").Value = IIf(Range("g" & ii).Value = "", Null, Range("g" & ii).Value)
rs.Fields("库存数量").Value = IIf(Range("i" & ii).Value = "", Null, Range("i" & ii).Value + rs.Fields("库存数量").Value)
rs.Fields("备注").Value = IIf(Range("k" & ii).Value = "", Null, Range("k" & ii).Value)
rs.Update
End If
Next ii
Set rs = Nothing
cnn.Close
[em04] |
|