Results 1 to 8 of 8
  1. #1
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107

    Calculated field in recordset

    Hi, I have a calculated field named RemainingStock in a subform called MyItems
    When I try to print the values of RemainingStock, it shows an error "Item not found in this collection". But if I try to print any other non-calculated field, everything works fine.

    I am using the following code:

    Code:
    Dim rs As Recordset
    Set rs = MyItems.Form.RecordsetClone
    rs.MoveFirst
    Do Until rs.EOF
       MsgBox rs!RemainingStock
       rs.MoveNext
    Loop
    rs.Close
    Any help please?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    So it would be unbound and not in the recordset?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    in the query is the calc field:
    RemainingStock: field1+field4
    or
    RemainingStock: forms!fMyForm!txtBox



  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    If you do the calculation in the subform itself (in the control source of the textbox control named RemainingStock) then it will not be part of the recordset. You can redo the calculation in your loop using the recordset fields or do it in the subform's record source query as suggested by Ranman.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107
    Quote Originally Posted by ranman256 View Post
    in the query is the calc field:
    RemainingStock: field1+field4
    or
    RemainingStock: forms!fMyForm!txtBox


    In the form, it is a dlookup value

  6. #6
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107
    Quote Originally Posted by Gicu View Post
    If you do the calculation in the subform itself (in the control source of the textbox control named RemainingStock) then it will not be part of the recordset. You can redo the calculation in your loop using the recordset fields or do it in the subform's record source query as suggested by Ranman.

    Cheers,
    Yes, I tried this method, I added my dlookup equation inside the loop and it worked, but I think it is time consuming because the computer is making the calculations twice. One time in the calculated field and another time inside the loop.
    So, I was thinking, the values that I want are already displayed in the calculated field, so can I have a direct access to them instead of recalculating them?

  7. #7
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107
    Quote Originally Posted by Welshgasman View Post
    So it would be unbound and not in the recordset?
    It is a dlookup equation in a subform.

  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You're not really showing or explaining what you're trying to do, but you can try to reference the control itself (Me.RemainingStock) but not in a loop like you have in the original post; the control does not get calculated until the record is made current and the previous value is long forgotten. So I don't think you would have any other option other than the one discussed, do it in the record source query or again in your loop. And I wouldn't worry about the wasted resources with nowadays computer power unless your dLookup is based on hundreds of thousand of records or more....
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 4
    Last Post: 02-09-2019, 05:38 PM
  2. Replies: 1
    Last Post: 12-10-2017, 01:47 AM
  3. Replies: 1
    Last Post: 05-22-2017, 05:59 AM
  4. Replies: 2
    Last Post: 12-30-2014, 01:32 PM
  5. Replies: 1
    Last Post: 11-13-2009, 03:03 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