Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    tahirsatti is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    14

    How to update subform records using loop

    I am using below mentioned code to update all records in subform but it update only first record.Please help to resolve this issue so that it will update all records.



    Dim tmprs As DAO.Recordset
    Dim fld As DAO.Field

    Set tmprs = [subform1].Form.RecordsetClone

    tmprs.MoveFirst
    While Not tmprs.EOF
    For Each fld In tmprs.Fields

    If [subform1].Form!Class_Id = 2 Then
    [subform1].Form!total = [subform1].Form![Sub_Total] * DSum("Qty", "Screen_size", "dt_id=[subform1].Form!dt_id")



    End If

    If [subform1].Form!Class_Id = 3 Then
    [subform1].Form!total = [subform1].Form![Sub_Total] * DSum("Qty", "Screen_size", "dt_id=[subform1].Form!dt_id")
    End If


    If [subform1].Form!Class_Id = 1 Then
    [subform1].Form!total = [subform1].Form![Sub_Total] * DSum("Qty", "Screen_size", "dt_id=[subform1].Form!dt_id")
    End If

    Next fld
    tmprs.MoveNext
    Wend

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Why are you saving calculated data? Especially aggregate calculation?

    Why are you hitting each field? Why the conditional? Concatenate variables. Reference to the form control in the DSum is a variable.

    Your code references and updates only the first record of subform, not the RecordsetClone records.

    With Me.subform1.Form.RecordsetClone
    While Not .EOF
    If .Class_ID = 1 Or .Class_ID = 2 Or .Class_ID = 3 Then
    !Total = Me.subform1.Form.Sub_Total * DSum("Qty", "Screen_size", "dt_id=" & Me.[subform1].Form!dt_id)
    End If
    .MoveNext
    Wend
    End With
    Last edited by June7; 05-09-2013 at 11:27 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    tahirsatti is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    14
    Thanks for your response,Please tell me how to update all records in subform?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I already gave you code to do that. Update the RecordsetClone records.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    tahirsatti is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    14
    I use your above mentioned code but it is only update first row not all records.

  6. #6
    tahirsatti is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    14
    If i click on second row and press button then above code update second row.Please modify the code so that it will update all rows on single click.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Did not know you had attempted code. I did do an edit of the post because I forgot couple of lines. Look at it again.

    I don't know why you have the conditional criteria but I incorporated it in the code to the best of my understanding.

    Still don't understand why you are saving aggregate data. Review http://allenbrowne.com/AppInventory.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    tahirsatti is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    14
    I am using below mentioned code

    With Me.[screendetailsubform].Form.RecordsetClone
    While Not .EOF
    If .Class_Id = 1 Or .Class_Id = 2 Or .Class_Id = 3 Then
    Me.[screendetailsubform].Form!total = Me.[screendetailsubform].Form!Sub_Total * DSum("Qty", "Screen_size", "screen_detail_id=" & [screendetailsubform].Form!Screen_detail_id)
    End If
    .MoveNext
    Wend
    End With


    But still only updating first record.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Because you are updating the textbox on the form, not the field in recordset.

    Try:

    .total = Me.[screendetailsubform].Form!Sub_Total * DSum("Qty", "Screen_size", "screen_detail_id=" & [screendetailsubform].Form!
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    tahirsatti is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    14
    When i use your below mentioned code than following error apperears.

    " Runtime error 438 Object does not support this property or methoed"

    Complete code is given below

    Private Sub Command168_Click()
    With Me.[screendetailsubform].Form.RecordsetClone
    While Not .EOF
    If .Class_Id = 1 Or .Class_Id = 2 Or .Class_Id = 3 Then
    .total = Me.[screendetailsubform].Form!Sub_Total * DSum("Qty", "Screen_size", "screen_detail_id=" & [screendetailsubform].Form!Screen_detail_id)
    End If
    .MoveNext
    Wend
    End With
    End Sub

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The very same sort of procedure works for me.

    Which line triggers the error?

    If you want to provide your db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    tahirsatti is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    14
    Thanks for your response.same .total lines triggers the error when i chenge .total with db field name then edit update message apperas.New code is given below.Please note that total is the name of tcost db field at form.Below mentioned code update all records but with the value of first row.


    Private Sub Command168_Click()
    With Me.[screendetailsubform].Form.RecordsetClone
    While Not .EOF
    .MoveFirst
    If .Class_Id = 1 Or .Class_Id = 2 Or .Class_Id = 3 Then
    .Edit
    .tcost = Me.[screendetailsubform].Form!Sub_Total * DSum("Qty", "Screen_size", "screen_detail_id=" & [screendetailsubform].Form!Screen_detail_id)
    End If
    .Update
    .MoveNext
    Wend
    End With
    End Sub

  13. #13
    tahirsatti is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    14
    Sir.if possible please help to resolve above mentioned issue.Why i am unable to update all records?.

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    What is the Sub_Total value calculated from?

    If you want expeditious help, provide the database for analysis?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    tahirsatti is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    14
    Sub total is the value of sum of three other fields i.e
    Sub_Total=[Style_Pr]+[Increase_Cost]+[Price_rng_pr]

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 10
    Last Post: 01-29-2013, 08:59 AM
  2. Replies: 1
    Last Post: 06-15-2012, 10:47 AM
  3. Coded Loop v SQL Update Query Performance
    By bginhb in forum Programming
    Replies: 5
    Last Post: 01-21-2012, 03:02 PM
  4. Loop through records
    By sam10 in forum Programming
    Replies: 12
    Last Post: 07-07-2011, 02:30 PM
  5. Loop through subform records
    By asmith in forum Forms
    Replies: 6
    Last Post: 10-06-2010, 10:31 AM

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