Results 1 to 6 of 6
  1. #1
    Saved is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2018
    Posts
    2

    VBA For Each Loop

    Good Evening,



    Please forgive me for such a rudimentary question, but I am very new to coding.

    I have an access database that schedules job tasks based on scheduled work orders. I need to write a loop that will look at each work order and based on status and assigned team, execute an append query. The append query moves a predetermined set of job tasks to a master daily work schedule.

    Below are two separate loops that I have been trying to use. Both look at one work order and then exits. Can someone help me, please?


    Dim lastrow
    lastrow = rs.EOF

    For i = lastrow To 0 Step -1
    If order_status.Value = "1" _
    And assigned_team.Value = 1 Then
    db.Execute "qry-append-team-1"
    ElseIf order_status.Value = "1" _
    And assigned_team.Value = 2 Then
    db.Execute "qry-append-team-2"
    ElseIf order_status.Value = "1" _
    And assigned_team.Value = 3 Then
    db.Execute "qry-append-team-3"
    ElseIf order_status.Value = "1" _
    And assigned_team.Value = 4 Then
    db.Execute "qry-append-team-4"
    End If
    Next I







    With rs
    Do Until .EOF
    If !order_status = "1" Then
    Select Case !assigned_team
    Case "1": db.Execute "qry-append-team-1"
    Case "2": db.Execute "qry-append-team-2"
    Case "3": db.Execute "qry-append-team-3"
    Case "4": db.Execute "qry-append-team-4"
    End Select
    End If
    .MoveNext
    Loop
    .Close
    End With

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The second is more common syntax, but you don't show what the recordset is based on. Also, your queries don't refer any value from the recordset, so they just execute the same values. They may be failing silently as well. Add:

    db.Execute "qry-append-team-1", dbFailOnError
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Saved is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2018
    Posts
    2

    VBA For Each Loop

    Quote Originally Posted by pbaldy View Post
    The second is more common syntax, but you don't show what the recordset is based on. Also, your queries don't refer any value from the recordset, so they just execute the same values. They may be failing silently as well. Add:

    db.Execute "qry-append-team-1", dbFailOnError



    pbaldy,

    Thanks for a hasty reply. I added the dbFailOnError. And.....now i run into a completely different unintended result. When I try to add a second assignment to Team 1, ("qry-append-team-1") it gives the first team with this assignment a second identical work assignment and gives the second team with this assignment two identical work assignments. Both should only have one.

    The recordset is based on a table that contains the discreet work orders.

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


    Set db = CurrentDb
    Set rs = db.OpenRecordset("tbl_work_orders")
    DoCmd.SetWarnings False
    DoCmd.RunSQL "DELETE FROM tbl_work_assignments"

    With rs
    Do Until .EOF
    If !order_status = "1" Then
    Select Case !assigned_team
    Case "1": db.Execute "qry-append-team-1", dbFailOnError
    Case "2": db.Execute "qry-append-team-1", dbFailOnError
    Case "3": db.Execute "qry-append-team-1", dbFailOnError
    Case "4": db.Execute "qry-append-team-1", dbFailOnError
    End Select
    End If
    .MoveNext
    Loop
    .Close
    End With

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well, you're looping the entire table, so every time you do this you'll add records for previously worked on records, won't you? It sounds like your queries need to take the work order from the loop into account somehow. Options include executing SQL from code, or populating a form control and having the queries get the value from there. Can you attach a sample db here and describe the intended result?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Quit picking on team 1.
    Code:
    With rs
    Do Until .EOF
    If !order_status = "1" Then
    Select Case !assigned_team
    Case "1": db.Execute "qry-append-team-1", dbFailOnError
    Case "2": db.Execute "qry-append-team-2", dbFailOnError
    Case "3": db.Execute "qry-append-team-3", dbFailOnError
    Case "4": db.Execute "qry-append-team-4", dbFailOnError
    End Select
    End If
    .MoveNext
    Loop
    .Close
    End With

  6. #6
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Code:
    DoCmd.SetWarnings False
    DoCmd.RunSQL "DELETE FROM tbl_work_assignments"
    Also note that when you set warnings to false you should also explicitly turn them back on. And be careful that they get turned back on before any error occurs.
    Better to use the .execute method.

    Code:
    Dim strSql as string
    strSql = "Delete * from  tbl_work_assignments"
    db.Execute strSql,dbFailOnError
    
    

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 03-07-2017, 02:49 PM
  2. Replies: 12
    Last Post: 06-05-2015, 04:27 PM
  3. Replies: 13
    Last Post: 08-20-2014, 09:17 AM
  4. Replies: 17
    Last Post: 04-07-2014, 07:48 PM
  5. Replies: 3
    Last Post: 03-10-2013, 07:04 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums