Results 1 to 15 of 15
  1. #1
    cksm4 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    24

    Loops


    I need to loop the below code so that it runs on all rows in the subform:

    If Me.TblEquipmentInventory_subform!Return = True Then
    Me.TblEquipmentInventory_subform!EmployeeID = Null
    End If

    Any ideas? (I know this is basic... but I am new to VBA and learning) Thanks!

    In case ur interested, I am using this in order to allow the user to check a yes/no box next to an item assigned to an employee and delete the employee ID if the item is no longer assigned to them.

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    I would check the help articles on the "recordsetclone" property. the other thing too, is that your syntax is incorrect:
    Code:
    Me.TblEquipmentInventory_subform.form!Return
    OR
    Code:
    Me.TblEquipmentInventory_subform.form.controls("Return")

  3. #3
    cksm4 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    24
    Thanks for your reply! I have looked up the "recordsetclone" property and am still unable to get it to work?? The code will work for the first record but then stops. I need it to continue to the next record in the subform.

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by cksm4 View Post
    Thanks for your reply! I have looked up the "recordsetclone" property and am still unable to get it to work?? The code will work for the first record but then stops. I need it to continue to the next record in the subform.
    if it is working for the first only, then you need to move to last and first to open the whole dataset. Don't ask me why this is necessary, it just is.
    Code:
    rs.movelast
    rs.movefirst
    does that make sense? Once you get that down, the loop should be fine.

  5. #5
    cksm4 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    24
    Thanks for the reply. I think this is way over my level of VBA understanding. Every I have tried returns errors. Do I use a "Do Until .EOF" statement?

  6. #6
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by cksm4 View Post
    Thanks for the reply. I think this is way over my level of VBA understanding. Every I have tried returns errors. Do I use a "Do Until .EOF" statement?
    you stated earlier that whatever you are doing is working on the first record. that's the number one symptom of not using those two lines of code. what you said earlier implies that you are using code already to modify your dataset.

    and yes, you are right. in general, to modify all records in a dataset, you do this:
    Code:
    rs.movelast
    rs.movefirst
    
    do until rs.eof
         'DO SOMETHING
              rs.movenext
    loop
    make sense?

  7. #7
    cksm4 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    24
    Here is the code I have been using. It still runs on the first record and then stops:

    Set Rs = CurrentDb.OpenRecordset("SELECT * FROM TblEquipmentInventory WHERE (TblEquipmentInventory.Return)=True")
    Rs.MoveLast
    Rs.MoveFirst
    Do Until Rs.EOF
    If Me.TblEquipmentInventory_subform!Return = True Then
    Me.TblEquipmentInventory_subform!EmployeeID = Null
    End If
    Rs.MoveNext
    Loop
    DoCmd.Requery

    Thanks for your help on this... I really appreciated the quick responses!

  8. #8
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by cksm4 View Post
    Here is the code I have been using. It still runs on the first record and then stops:

    Set Rs = CurrentDb.OpenRecordset("SELECT * FROM TblEquipmentInventory WHERE (TblEquipmentInventory.Return)=True")
    Rs.MoveLast
    Rs.MoveFirst
    Do Until Rs.EOF
    If Me.TblEquipmentInventory_subform!Return = True Then
    Me.TblEquipmentInventory_subform!EmployeeID = Null
    End If
    Rs.MoveNext
    Loop
    DoCmd.Requery

    Thanks for your help on this... I really appreciated the quick responses!
    ck,

    apparently you didn't read the second post. read that again first. your subform control referencing is incorrect. and..are you SURE it's actually working on the first rec? I'm inclined to think it's not working at all and that the first rec field is null to start with, because the syntax is wrong.

  9. #9
    cksm4 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    24
    I have tried your correction to the subform control:

    Set Rs = CurrentDb.OpenRecordset("SELECT * FROM TblEquipmentInventory WHERE (TblEquipmentInventory.Return)=True")
    Rs.MoveLast
    Rs.MoveFirst
    Do Until Rs.EOF
    If Me.TblEquipmentInventory_subform.Form!Return = True Then
    Me.TblEquipmentInventory_subform.Form!EmployeeID = Null
    End If
    Rs.MoveNext
    Loop
    DoCmd.Requery

    I still get the same response. TblEquipmentInventory lists all the items in the inventory. I use the field EmployeeID to assign an item to a employee. I then use the TblEquipmentInventory_Subform to list the equipment assigned to a specific employee (using EmployeeID as the parent/child). I added the "Return" yes/no field to allow a user to check multiple items from the TblEquipmentInventory_Subform that need to be unassigned (returned to inventory). So basically this code needs to find records from TblEquipmentIntentory where the Return field value is Yes... and then delete the EmployeeID from that record.

    It works on the first record as I see it disappear from the TblEquipmentInventor_Subform (as the EmployeeID is null and no longer a parent/child relationship to the current record).

    Thanks for your time helping me with this.

  10. #10
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    upload the file and I'll take a look for ya

    give me specific instructions as to what to look for and what to do in order to duplicate the error. I don't have too much time to browse today as I'm working on another project at home.

  11. #11
    cksm4 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    24
    No error occurs with the code posted. It just stops after successfully deleting the value of the EmployeeID field in the first record despite there being additional records where the value of the Return field is set to "yes". I want all records with the value of Return = yes to have the value of EmployeeID be null. Do you still want me to upload?

  12. #12
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by cksm4 View Post
    No error occurs with the code posted. It just stops after successfully deleting the value of the EmployeeID field in the first record despite there being additional records where the value of the Return field is set to "yes". I want all records with the value of Return = yes to have the value of EmployeeID be null. Do you still want me to upload?
    yes. otherwise i wouldn't ask. see here on how: https://www.accessforums.net/faq.php...b3_attachments

  13. #13
    cksm4 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    24
    Thanks! File attached

  14. #14
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    put this behind the button:
    Code:
    Dim rs As DAO.Recordset
    Set rs = Me.TblEquipmentInventory_subform.Form.RecordsetClone
    
    With rs
       .MoveLast
       .MoveFirst
          Do Until .EOF
             If !Return = -1 Then
                .Edit
                   !EmployeeID = Null
                .Update
             End If
                   .MoveNext
          Loop
       .Close
    End With
    
    Me.TblEquipmentInventory_subform.Form.Requery
    Me.Requery
    
    Set rs = Nothing
    take care.

  15. #15
    cksm4 is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    24
    That did it! Well written and it makes sense now that I see it. Greaty appreciated. Have a good remainder of the weekend

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

Similar Threads

  1. Beginner - 3 tables in 3 loops
    By dsasorin in forum Database Design
    Replies: 2
    Last Post: 03-20-2010, 06:22 PM
  2. Help with Loops
    By rotorque in forum Access
    Replies: 0
    Last Post: 01-07-2009, 05:06 PM

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