Results 1 to 13 of 13
  1. #1
    MLangendorf is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    50

    Trouble with macro within a subform

    Anyone that can help,



    I have a macro within a form that performs a calculation and then populates a separate field with the answer. It works fine in the form but a virtually identical macro will not perform and populate within a subform on that same form. Below is my macro (very simple) and the error message that I am getting.




    Click image for larger version. 

Name:	macro.png 
Views:	10 
Size:	12.2 KB 
ID:	26711












    Click image for larger version. 

Name:	2016-12-12_12-45-09.png 
Views:	10 
Size:	74.3 KB 
ID:	26710

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    As SubForms are *not* listed in the Forms collection, perhaps this link will help: http://access.mvps.org/access/forms/frm0031.htm

  3. #3
    MLangendorf is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    50
    Unfortunately this did not help me out much. I tried to use the information but it didn't work.
    I am not experienced in writing code. I can put together a macro that will usually get the job done but trying to figure out how to develop the expression to do the calculation within the macro tool itself is proving difficult for me. I'm sure it has something to do with the Parent and Child form issue.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    What is the name of the MainForm?

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    This macro is running on the Form [Frm_Clinical Data] right? If that's true then it is not necessary to use the Forms collection.
    Here's the code that would do the calculation:
    Me.BMI = Round((Me.Weight*703)/(Me.Height^2),1)

  6. #6
    MLangendorf is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    50
    let me try to explain this. First, use my screen shots above for reference.
    The right hand side of the screen is my subform affectionately named Subfrm_ClinicalFUData
    This is where I am trying to run a macro that is living behind the scenes of the Weight field. So, when I enter a new weight it is suppose to look at the Height (which, even though it shows on the subform it is actually a field on the main form (which, to answer your question is named Frm_Clinical Data).
    In other words, it is trying to usedata from two different forms (or a parent form and a child form (subform)) to calculate the BMI field.

    Now, on the left hand side of the screen shot, this is my main form. This calculation is made on that form as well and works perfectly fine.

    I will try your code and see what happens.

    thanks again for trying to help me sort this out.

  7. #7
    MLangendorf is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    50
    This is what mine looks like but it doesn't seem quite right to me.
    So, I am entering the weight into a field on the subform, the Height that is being used is a field on the main form, and the outcome field (BMI) is on the subform

    Private Sub Weight_AfterUpdate()
    End Sub
    Private Sub Weight_Click()
    Me.BMI = Round((Me.Weight * 703) / (Me.Height ^ 2), 1)
    End Sub

  8. #8
    MLangendorf is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    50
    I think that I may have resolved it. Give me a few and I will reply again with my findings.

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The code I posted will not work if it is using values from both a MainForm and a SubForm.

  10. #10
    MLangendorf is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    50
    After looking at the code, I realized something didn't look quite right.
    This is what was there when I first tried to do the Code Builder
    Private Sub Weight_AfterUpdate()
    End Sub
    Private Sub Weight_Click()
    Me.BMI = Round((Me.Weight * 703) / (Me.Height ^ 2), 1)
    End Sub

    Then I went back in and backed some stuff out and ended with this: which then worked perfectly.
    Private Sub Weight_Afterupdate()
    Me.BMI = Round((Me.Weight * 703) / (Me.Height ^ 2), 1)

    Thanks so much for your guidance RG
    End Sub

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Outstanding! Thanks for posting back with your success and solution.
    Have you used the Thread Tools at the top of the Thread to mark this thread as Solved?

  12. #12
    MLangendorf is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jun 2016
    Posts
    50
    Yes sir!
    Just did it.

  13. #13
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Thank you for your cooperation.

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

Similar Threads

  1. Trouble with DLOOKUP in a Subform
    By Mubashir sabir in forum Forms
    Replies: 2
    Last Post: 08-21-2015, 01:38 PM
  2. Subform Requery Trouble
    By ztpkng in forum Forms
    Replies: 2
    Last Post: 06-19-2014, 04:02 PM
  3. Export / Import form and subform trouble
    By Roy Hallowell in forum Import/Export Data
    Replies: 5
    Last Post: 06-03-2014, 06:23 PM
  4. Replies: 8
    Last Post: 02-04-2013, 11:32 AM
  5. UI Macro Trouble
    By beetgirl in forum Access
    Replies: 0
    Last Post: 07-06-2011, 10:17 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