Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

[5分]库存计算重复

菜鸟  发表于:2014-07-03 16:34:59  
复制

  On Error GoTo ErrorHandler
    Dim strSql As String
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim rstTmp As DAO.Recordset
    Dim blnTransBegin As Boolean

    Dim strstatus As String

    strstatus = DLookup("Status", "tbl_Wip_Wo_Dep_Master", "TrCode= '" & Me![TrCode] & "'")

    If strstatus = "已接收" Then
        MsgBox "已接收,不能重复接收!", vbInformation, "提示"
        Exit Sub
    End If

    If Me.Status <> "已接收" Then
        MsgBox "接收前要确认资料无误,并将态设为'已接收'!", vbCritical, "提示"
        Me.Status.SetFocus
        Exit Sub
    End If
    Me.cmdSave.Enabled = False
   
    DoCmd.SetWarnings False

    DoCmd.RunSQL "INSERT INTO tbl_Wip_Wo_Dep_Stock ( WoCode, Dep )" _
               & "SELECT Temp_tbl_Wip_Wo_Dep_Detail.WoCode, '" & Me.ToDep & "'" _
               & "FROM Temp_tbl_Wip_Wo_Dep_Detail;"

    DoCmd.SetWarnings True

 

    Dim strExpr As String

    If Not CheckRequired(Me) Then Exit Sub
   

    Set cnn = GetADOConnection()
    '启动事务
    cnn.BeginTrans
    blnTransBegin = True

 

    cnn.Execute " UPDATE dbo.tbl_Wip_Wo_Dep_Master SET Status= '" & Me![Status] & "'" _
              & " WHERE TrCode='" & TrCode & "'"

    '打开临时表记录集
    Set rstTmp = CurrentDb.OpenRecordset("Temp_tbl_Wip_Wo_Dep_Detail", , dbReadOnly)


    rstTmp.MoveFirst    '临时记录集移回到第一行即可重新使用,不需要重新打开
    Do Until rstTmp.EOF
        cnn.Execute " UPDATE dbo.tbl_Wip_Wo_Dep_Stock SET DepDate = " & Format$(Me![UpdateTime], "\'yyyy-mm-dd hh:nn:ss\'") _
                  & " WHERE WoCode = '" & rstTmp!WoCode & "' AND Dep='" & Me![ToDep] & "'"

        cnn.Execute " UPDATE dbo.tbl_Wip_Wo_Dep_Stock" _
                  & " SET Quantity = Quantity - " & Nz(rstTmp!Quantity, 0) _
                  & ",GrossWeight = GrossWeight - " & Nz(rstTmp!GrossWeight, 0) _
                  & ",StoneWeight = StoneWeight - " & Nz(rstTmp!StoneWeight, 0) _
                  & ",NetWeight = NetWeight - " & Nz(rstTmp!NetWeight, 0) _
                  & " WHERE Dep = '" & Me.FromDep & "' AND WoCode='" & rstTmp!WoCode & "'"

        cnn.Execute " UPDATE dbo.tbl_Wip_Wo_Dep_Stock" _
                  & " SET Quantity = Quantity + " & Nz(rstTmp!Quantity, 0) _
                  & ",GrossWeight = GrossWeight + " & Nz(rstTmp!GrossWeight, 0) _
                  & ",StoneWeight = StoneWeight + " & Nz(rstTmp!StoneWeight, 0) _
                  & ",NetWeight = NetWeight + " & Nz(rstTmp!NetWeight, 0) _
                  & " WHERE Dep = '" & Me.ToDep & "' AND WoCode='" & rstTmp!WoCode & "'"
        rstTmp.MoveNext
    Loop

    rstTmp.Close

    cnn.CommitTrans    '提交事务
    blnTransBegin = False '事务提交或回滚后要立即恢复标志变量,防止后面的代码出错造成错误代码不能正常判断

    MsgBox "保存成功!", vbInformation, "提示!"

    Call ChildFormRequery(strExpr)
    DoCmd.Close acForm, Me.name

 

ExitHere:
    Set rstTmp = Nothing
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub

ErrorHandler:
    If blnTransBegin Then
        cnn.RollbackTrans       '出错时如果已启动事务,则回滚撤销
        blnTransBegin = False
    End If
    MsgBox Err.Description, vbCritical, "Error #" & Err
    Resume ExitHere
End Sub

 

各位老师,能不能帮我看一下上面的代码,偶尔出现重复计算库存。有没有方法杜绝它发生!库侟 不定时就出现错误,,,,,

 

 

Top
煮江品茶 发表于:2014-07-05 17:07:11
谁闲的没事帮你查错。

总记录:1篇  页次:1/1 9 1 :