循环代码不在表单中移动

时间:2019-06-17 18:52:24

标签: ms-access access-vba

我的数据库中有一个表格,该表格每周从查询中提取数据,以计算每周所需的部件零件,单击“完成”按钮后,所需的零件应移入或移出库存,但没有任何反应单击完成按钮时。该代码应循环并移动所有部分,但什么也没发生。

我已逐步检查是否有任何错误,并更正了一些语法错误,但这就是我所做的全部。

Private Sub Command96_Click()
    Dim ctl As Control
    Dim ctln
    Dim Qty As Double
    Dim db As DAO.Database
        Set db = CurrentDb
    Dim rs As DAO.Recordset
    For Each ctl In Me.Controls
        Select Case TypeName(ctl)
            Case "TextBox"
                Select Case ctl.ControlName
                    Case ctl Like "*Q"
                        ctln = Me.Controls(Right(ctl, Len(ctl) - 1))
                        If Not IsNull(DLookup("[In]", "[Inventory]", "[PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
                            num = DLookup("[In]", "[Inventory]", "[PartNum] = '" & ctln & "' AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "") + ctl
                        Else
                            num = ctl
                        End If
                        If Not IsNull(DLookup("[PartNum]", "[Inventory]", "[PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
                            CurrentDb.Execute "UPDATE [Inventory] " _
                                            & "SET [In] = " & num & " " _
                                            & "WHERE [PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "", dbFailOnError
                        Else
                            CurrentDb.Execute "INSERT INTO [Inventory] " _
                                            & "VALUES ('" & ctln & "'," & Me.YearNum & "," & Me.WeekNum & "," & num & ",0)", dbFailOnError
                        End If

                        num = 0

                        Set rs = db.OpenRecordset("SELECT UsedPartNum, (Quantity * " & ctl & ") AS Used FROM SubPartsUsed WHERE FinPartNum = '" & PartNum & "'", dbOpenDynaset)
                        If Not (rs.EOF And rs.BOF) Then
                            rs.MoveFirst
                            Do Until rs.EOF = True
                                If Not IsNull(DLookup("[Out]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
                                    num = DLookup("[Out]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "' AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "") + rs!Used
                                Else
                                    num = rs!Used
                                End If
                                If Not IsNull(DLookup("[PartNum]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
                                    CurrentDb.Execute "UPDATE [Inventory] " _
                                                    & "SET [Out] = " & num & " " _
                                                    & "WHERE [PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & ""
                                Else
                                    CurrentDb.Execute "INSERT INTO [Inventory] " _
                                                    & "VALUES ('" & rs!UsedPartNum & "'," & Me.YearNum & "," & Me.WeekNum & ",0," & num & ")"
                                End If
                                rs.MoveNext
                            Loop
                        End If
                        rs.Close
                        Set rs = Nothing
                End Select
        End Select

我希望这些零件将作为完整的子装配零件输入到库存中,并且构成它们的零件应从库存中删除。

2 个答案:

答案 0 :(得分:0)

代码由VBA的高级专业人士完成,许多执行代码的捷径。新VBA程序员可能很难更正此代码,所以我认为:

  • 第一步应该添加Debug.Print code executed here at line number XXX以便研究执行了哪些行,以及假设是否执行了这些行。

  • 此后,如果代码逻辑正常,则Debug.Print将生成所有SQL语句。因此,您可以通过在查询设计器中执行

  • 来检查其正确性

例如:

Private Sub Command96_Click()
Dim ctl As Control
Dim ctln
Dim Qty As Double
Dim db As DAO.Database
    Set db = CurrentDb
Dim rs As DAO.Recordset
Dim sSQL As String
For Each ctl In Me.Controls
    Select Case TypeName(ctl)
        Debug.Pring "looping through controls"
        Case "TextBox"
            Select Case ctl.ControlName
                Case ctl Like "*Q"
                    Debug.Pring "Control with Q letter is found"
                    ctln = Me.Controls(Right(ctl, Len(ctl) - 1))
                    If Not IsNull(DLookup("[In]", "[Inventory]", "[PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
                        Debug.Print "Num is DLookuped"
                        num = DLookup("[In]", "[Inventory]", "[PartNum] = '" & ctln & "' AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "") + ctl
                    Else
                        num = ctl
                    End If
                    If Not IsNull(DLookup("[PartNum]", "[Inventory]", "[PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
                        Debug.Print "Executing Update Query for not null dlookup"
                        sSQL = "UPDATE [Inventory] " _
                                        & "SET [In] = " & num & " " _
                                        & "WHERE [PartNum] = '" & ctln & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & ""
                        Debug.Print sSQL
                        CurrentDb.Execute sSQL, dbFailOnError

                    Else
                        Debug.Print "Executing Update Query for null dlookup"
                        sSQL = "INSERT INTO [Inventory] " _
                                        & "VALUES ('" & ctln & "'," & Me.YearNum & "," & Me.WeekNum & "," & num & ",0)"
                        Debug.Print sSQL
                        CurrentDb.Execute sSQL, dbFailOnError
                    End If

                    num = 0

                    Set rs = db.OpenRecordset("SELECT UsedPartNum, (Quantity * " & ctl & ") AS Used FROM SubPartsUsed WHERE FinPartNum = '" & PartNum & "'", dbOpenDynaset)
                    If Not (rs.EOF And rs.BOF) Then
                        Debug.Print "Beginning action for each record in PartNum select query"
                        rs.MoveFirst
                        Do Until rs.EOF = True
                            If Not IsNull(DLookup("[Out]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
                                Debug.Print "Executing Dlookup for element in PartNum select query"
                                num = DLookup("[Out]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "' AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "") + rs!Used

                            Else
                                num = rs!Used
                            End If
                            If Not IsNull(DLookup("[PartNum]", "[Inventory]", "[PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & "")) Then
                                Debug.Print "Executing Update for not null DLookup element in PartNum select query"
                                sSQL = "UPDATE [Inventory] " _
                                                & "SET [Out] = " & num & " " _
                                                & "WHERE [PartNum] = '" & rs!UsedPartNum & "'AND [YearNum] = " & Me.YearNum & " AND [WeekNum] = " & Me.WeekNum & ""
                                Debug.Print sSQL
                                CurrentDb.Execute sSQL
                            Else
                                Debug.Print "Executing Update for  null DLookup element in PartNum select query"
                                sSQL = "INSERT INTO [Inventory] " _
                                                & "VALUES ('" & rs!UsedPartNum & "'," & Me.YearNum & "," & Me.WeekNum & ",0," & num & ")"
                                Debug.Print sSQL
                                CurrentDb.Execute sSQL
                            End If
                            rs.MoveNext
                        Loop
                    End If
                    rs.Close
                    Set rs = Nothing
            End Select
    End Select

在这种情况下,您应该研究“即时”窗口(以Ctrl + G打开),查看执行计划是什么,生成了什么SQL文本,并检查所有这些。 / p>

否则,此代码中您的业务特定逻辑太多,并且很难理解程序行为。可能是由于业务逻辑导致了这种行为?很多很多问题

答案 1 :(得分:0)

一遍又一遍地按F9键,直到您发现问题所在。同样,利用“添加监视”来查看将哪些值传递给哪些变量。那应该有很大帮助。最后,如果这是由专业人员完成的,那么为什么要使用“ Command96_Click()”?当然,这不是问题,但也无济于事。