VBA做While循环

时间:2018-08-11 17:16:14

标签: access-vba

早上好

我正在建立一个数据库,该数据库将用于安排员工的工作分配。以下是我用来创建实际日常工作分配的代码。目的是,这将遍历公司中的每个员工,并且如果他们处于工作状态,还将读取他们分配的计划版本。然后,如果员工处于工作状态,则数据库将列出他们每天的工作任务。

我遇到的问题是,这只会读取表中的第一位员工,并给其他所有员工第一位员工应具有的工作分配。再次,目的是使代码一次查看每个员工,然后根据每个员工的状态和计划版本将正确的分配附加到tbl_employee_work_assignments。

请帮助我了解如何正确执行此操作?

非常感谢!

Private Sub btn_build_assignment_schedule_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset

    Set db = CurrentDb
    Set rs = db.OpenRecordset("tbl_employees")

    DoCmd.SetWarnings False
    DoCmd.RunSQL "DELETE * FROM tbl_employee_work_assignments"

    Do While Not rs.EOF
                Dim X As String
                Dim Y As String
                X = employee_schedule_version.Value
                Y = employee_status.Value
                DoCmd.SetWarnings False
                DoCmd.RunSQL "DELETE * FROM tbl_employee_work_assignments"
                Select Case True
                    Case X = 1 And Y = 1
                        db.Execute "qry_append_schedule_1"
                    Case X = 2 And Y = 1
                        db.Execute "qry_append_schedule_2"
                    Case X = 3 And Y = 1
                        db.Execute "qry_append_schedule_3"
                    Case X = 4 And Y = 1
                        db.Execute "qry_append_schedule_4"
                End Select
            rs.MoveNext
            Loop
    rs.Close

End Sub

1 个答案:

答案 0 :(得分:0)

基于所提供的非常有限的信息并做出一些假设,我冒昧地猜测您的代码应按照以下内容进行更改:

Private Sub btn_build_assignment_schedule_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset

    Set db = CurrentDb
    Set rs = db.OpenRecordset("tbl_employees")

    DoCmd.SetWarnings False
    DoCmd.RunSQL "DELETE FROM tbl_employee_work_assignments"

    With rs
        Do Until .EOF
            If !employee_status = "1" Then
                Select Case !employee_schedule_version
                    Case "1": db.Execute "qry_append_schedule_1"
                    Case "2": db.Execute "qry_append_schedule_2"
                    Case "3": db.Execute "qry_append_schedule_3"
                    Case "4": db.Execute "qry_append_schedule_4"
                End Select
            End If
            .MoveNext
        Loop
        .Close
    End With
End Sub

这假设employee_statusemployee_schedule_version是表tbl_employees中的字符串值字段。

在不了解表tbl_employees的结构以及查询qry_append_schedule_1qry_append_schedule_2等背后的SQL的情况下,很难提供建议。