Consulting

Results 1 to 18 of 18

Thread: Object Required error

  1. #1
    VBAX Regular
    Joined
    Apr 2012
    Posts
    13
    Location

    Exclamation Object Required error

    Hi,

    i have writtern this visual basics code for the user to enter there age and there resting heart rate and it works out there trainning heart rate
    the only problem is i cant get it to work

    (PLEASE NOTE I AM USING WORD 2010 TO WRITE THE CODE WITH VISUAL BASIC UNDER THE DEVELOPER TAB INSIDE OF WORD)

    i have created a form and 2 buttons and 2 textboxes
    but when i enter for exmple 20 and 123
    it gives me a error Run-Time Error '424': object required
    this error is given to me every time i put any number in it

    the code is
    HTML Code:
    Private Sub CommandButton1_Click()
     Dim flag As Boolean
     flag = True
     Dim age As Integer
     age = TextBox1.Text
     Dim restingRate As Integer
     restingRate = TextBox2.Text
     txtOutput.Text = CStr(TrainingHeartRate(age, restingRate, flag))
     End Sub
     Private Sub CommandButton2_Click()
     Dim flag As Boolean
     flag = True
     Dim age As Integer
     age = TextBox1.Text
     Dim restingRate As Integer
     restingRate = TextBox2.Text
     txtOutput.Text = CStr(TrainingHeartRate(age, restingRate, flag))
     End Sub
     
    Function TrainingHeartRate(ByVal age As Integer, ByVal restingRate As Integer, ByVal flag As Boolean) As Integer
     Dim HRreserve, UpperTrainRate, LowTrainRate As Integer
     Dim maxHR As Integer
     maxHR = 220 - age
     HRreserve = maxHR - restingRate
     
    LowTrainRate = CInt(HRreserve * 0.5 + restingRate)
     UpperTrainRate = CInt(HRreserve * 0.85 + restingRate)
     If flag = True Then
     TrainingHeartRate = UpperTrainRate
     Else
     TrainingHeartRate = LowTrainRate
     End If
     End Function

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "i have created a form and 2 buttons and 2 textboxes"

    TWO textboxes.

    Your code uses THREE textboxes (TextBox1, TextBox2 and txtOutput). Thus..."Run-Time Error '424': object required". ONE of the textboxes (in your code) is not there...and is required, since you coded it.

    If I duplicate your userform with these three, your code works fine.

    Some comments.

    General good practice is to declare all your variable at the top, like this:[vba] Private Sub CommandButton2_Click()
    Dim flag As Boolean
    Dim age As Integer
    Dim restingRate As Integer

    flag = True
    age = TextBox1.Text
    restingRate = TextBox2.Text
    txtOutput.Text = CStr(TrainingHeartRate(age, restingRate, flag))
    End Sub[/vba]Also, as your are using the SAME variables in all the procedures, why not use the same ones as Public, like this:[vba]Option Explicit
    Public flag As Boolean
    Public age As Integer
    Public restingRate As Integer[/vba]That way your procedure do not have to declare them at all, like this:[vba] Private Sub CommandButton2_Click()
    flag = True
    age = TextBox1.Text
    restingRate = TextBox2.Text
    txtOutput.Text = CStr(TrainingHeartRate(age, restingRate, flag))
    End Sub[/vba]
    Check your userform. It works if you actually use the correct number of textboxes. Correctly named of course. One last thing, generally speaking it is better to name controls (textboxes) with a useful name.

    Instead of TextBox1 use txtAge.[vba]age = txtAge.Text[/vba]
    Last edited by fumei; 05-13-2012 at 08:25 PM.

  3. #3
    VBAX Regular
    Joined
    Apr 2012
    Posts
    13
    Location
    Quote Originally Posted by fumei
    "i have created a form and 2 buttons and 2 textboxes"

    TWO textboxes.

    Your code uses THREE textboxes (TextBox1, TextBox2 and txtOutput). Thus..."Run-Time Error '424': object required". ONE of the textboxes (in your code) is not there...and is required, since you coded it.

    If I duplicate your userform with these three, your code works fine.

    Some comments.

    General good practice is to declare all your variable at the top, like this:[vba] Private Sub CommandButton2_Click()
    Dim flag As Boolean
    Dim age As Integer
    Dim restingRate As Integer

    flag = True
    age = TextBox1.Text
    restingRate = TextBox2.Text
    txtOutput.Text = CStr(TrainingHeartRate(age, restingRate, flag))
    End Sub[/vba]Also, as your are using the SAME variables in all the procedures, why not use the same ones as Public, like this:[vba]Option Explicit
    Public flag As Boolean
    Public age As Integer
    Public restingRate As Integer[/vba]That way your procedure do not have to declare them at all, like this:[vba] Private Sub CommandButton2_Click()
    flag = True
    age = TextBox1.Text
    restingRate = TextBox2.Text
    txtOutput.Text = CStr(TrainingHeartRate(age, restingRate, flag))
    End Sub[/vba]
    Check your userform. It works if you actually use the correct number of textboxes. Correctly named of course. One last thing, generally speaking it is better to name controls (textboxes) with a useful name.

    Instead of TextBox1 use txtAge.[vba]age = txtAge.Text[/vba]
    oh god :l thanks lol i have been sitting here for hours trying to work out why it wasnt working lol

    but what i am trying to do it
    the person enters there age for example 20 and there resting heart rate is 70
    there trainning heart rate should be 148 but its giving me a answer of 180 how can i fix this so it gives me a answer of 148

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "should be 148 but its giving me a answer of 180 how can i fix this so it gives me a answer of 148"

    With an age = 20, and resting rate of 70...

    maxHR = 220 - age (= 200)
    HRreserve = maxHR - restingRate (= 130)

    LowTrainRate = CInt(HRreserve * 0.5 + restingRate)
    UpperTrainRate = CInt(HRreserve * 0.85 + restingRate)

    If flag = True Then
    TrainingHeartRate = UpperTrainRate = 130 * 0.85 = 110.5 + 70 = 180.5

    You have set the flag as true...the answer is UpperTrainRate, which = 180 (it is an integer)

    If the flag is False...the answer is LowerTrainRate = 130 * 0.5 = 65 + 70 = 135

    NEITHER returns 148. You fix it by doing better math. As you have it, the answer of 180 is totally correct. Hey, YOU coded it.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    BTW: if you see that "Run-Time Error '424': object required", it almost always means you have missed something. Or rather you are telling VBA to do something, action something, that is not there. Thus...object required.

  6. #6
    VBAX Regular
    Joined
    Apr 2012
    Posts
    13
    Location
    Quote Originally Posted by fumei
    "should be 148 but its giving me a answer of 180 how can i fix this so it gives me a answer of 148"

    With an age = 20, and resting rate of 70...

    maxHR = 220 - age (= 200)
    HRreserve = maxHR - restingRate (= 130)

    LowTrainRate = CInt(HRreserve * 0.5 + restingRate)
    UpperTrainRate = CInt(HRreserve * 0.85 + restingRate)

    If flag = True Then
    TrainingHeartRate = UpperTrainRate = 130 * 0.85 = 110.5 + 70 = 180.5

    You have set the flag as true...the answer is UpperTrainRate, which = 180 (it is an integer)

    If the flag is False...the answer is LowerTrainRate = 130 * 0.5 = 65 + 70 = 135

    NEITHER returns 148. You fix it by doing better math. As you have it, the answer of 180 is totally correct. Hey, YOU coded it.
    well what do i have to change? i am only begining coding and dont really no what to change i have tryed a few things like changing the flag to false and got 135 how do i add 10 to the flag so i get 145?

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    well what do i have to change? i am only begining coding and dont really no what to change i have tryed a few things like changing the flag to false and got 135 how do i add 10 to the flag so i get 145?
    Huh? Why would you want to add an artificial number? That would mean you would add 10 to ANY combination of age and resting rate? That makes no sense whatsoever.

    What do you have to change? I told you. Better math. How do you know 148 is the correct number? BTW, you stated 148 first. Is it now 145?

    Your formula is incorrect (if 145 or 148 is the RIGHT number). Work backwards. Obviously if 145 or 148 is correct,

    LowTrainRate = CInt(HRreserve * 0.5 + restingRate)
    UpperTrainRate = CInt(HRreserve * 0.85 + restingRate)

    are WRONG. Make them right. I can not tell you what is the correct formula.

    "i am only begining coding". This has little to do with VBA coding. Your formula is wrong. I mean if 145 is correct, is that the Upper or Lower rate?

  8. #8
    VBAX Regular
    Joined
    Apr 2012
    Posts
    13
    Location
    Quote Originally Posted by fumei
    Huh? Why would you want to add an artificial number? That would mean you would add 10 to ANY combination of age and resting rate? That makes no sense whatsoever.

    What do you have to change? I told you. Better math. How do you know 148 is the correct number? BTW, you stated 148 first. Is it now 145?

    Your formula is incorrect (if 145 or 148 is the RIGHT number). Work backwards. Obviously if 145 or 148 is correct,

    LowTrainRate = CInt(HRreserve * 0.5 + restingRate)
    UpperTrainRate = CInt(HRreserve * 0.85 + restingRate)

    are WRONG. Make them right. I can not tell you what is the correct formula.

    "i am only begining coding". This has little to do with VBA coding. Your formula is wrong. I mean if 145 is correct, is that the Upper or Lower rate?
    its 148 the answer needs to be
    so i need to change one of these
    LowTrainRate = CInt(HRreserve * 0.5 + restingRate)
    UpperTrainRate = CInt(HRreserve * 0.85 + restingRate)

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Correct. You need to change your formula. I can not say where.

    If the 0.5/0.85 is correct, then HRreserve needs to change.

    If HRreserve is correct (220 - age - restingRate), then the 0.5/0.85 needs to change.

    Your call.

    Yes, you could add a constant (like the 10 you mention), if that would work for every combination. So again, what makes 148 the correct answer?
    Last edited by fumei; 05-13-2012 at 11:13 PM.

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Are you a student? Are you doing this (and the Birthday question) for school?

  11. #11
    VBAX Regular
    Joined
    Apr 2012
    Posts
    13
    Location
    Quote Originally Posted by fumei
    Correct. You need to change your formula. I can not say where.

    If the 0.5/0.85 is correct, then HRreserve needs to change.

    If HRreserve is correct (220 - age - restingRate), then the 0.5/0.85 needs to change.

    Your call.

    Yes, you could add a constant (like the 10 you mention), if that would work for every combination. So again, what makes 148 the correct answer?
    ok thanks i will try work it out
    but why cant u tell me the answer?

  12. #12
    VBAX Regular
    Joined
    Apr 2012
    Posts
    13
    Location
    Quote Originally Posted by fumei
    Are you a student? Are you doing this (and the Birthday question) for school?
    no i am just trying to learn visual basics and a friend of mine sent me his assignments he had to do when he did a course on it a few years ago

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "but why cant u tell me the answer?"

    How can I tell you the answer? I do not know the answer. YOU have some formula - and BTW you did NOT answer MY question as to why 148 is correct - and the formula is wrong. I explained that one of either HRReserve or the 0.5/0.85 multiplication has to change to get that 148.

    Which has to change? How would I know? I do not know what the correct formula is.

    If you got the assignments from your friend, ask HIM for the answers (if he/she knows).

    Again, this is not a coding issue. It is a basic math issue. I have no idea (as you do not answer) why 148 is the right answer. I have no idea what gets you a Upper (or Lower) rate. Figure out the math (formula) needed and then use that.

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I will ask a third time. And if you do not answer I will not respond again. Why is 148 the right answer.

  15. #15
    VBAX Regular
    Joined
    Apr 2012
    Posts
    13
    Location
    Quote Originally Posted by fumei
    I will ask a third time. And if you do not answer I will not respond again. Why is 148 the right answer.

    the answer is right because it says on the document my friend gave me from a course he did a few years ago when the user enters the age of 20 and resting heart rate of 70 the training heart rate should come up as 148

  16. #16
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Sorry, that is simply not good enough. I have explained what needs to be done. The formula you have returns 180 or 135. That is the math. If the correct answer is 148, YOU have to find out the correct formula. There are two possible (maybe three if you use a constant) solutions.

    Either HRreserve must be changed (220 - age - resting rate);

    OR

    0.5 (or 0.85) must be changed.

    This is Grade 8 math. So...change one of those.

    Come up with something that will give you the 148. Ask your friend. But asking over and ver here is wasting our time.

  17. #17
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    This is done with Excel, but using the updated Karvonen Formula, I get a training range of 150 - 175, which is close to the 148 expected / anticipate / desired result.

    As Gerry, says "The math is the math"

    [VBA]
    Option Explicit
    'http://exercise.about.com/cs/fitnesstools/g/karvonen.htm
    'Definition: The Karvonen Formula is a mathematical formula that helps you determine
    'your target heart rate zone. The formula involves using your maximum heart rate (MHR)
    'minus your age to come up with a target heart rate range (which is a percentage of your
    'MHR). Staying within this range will help you work most effectively during
    'your cardio workouts.

    'Below is an example of the Karvonen formula for a 23 year old person with a resting
    'heart rate of 65 beats per minute (*to get your resting heart rate, take your pulse
    'for one full minute when you first wake up in the morning or after you've resting
    'for a while). This formula also includes an updated calculation of maximum heart rate
    '(the previous formula was 220 - age, which has now been shown to be inaccurate):
    '206.9 - (0.67 x 23 (age)) = 191
    '191 - 65 (resting heart rate) = 126
    '126 * 65% (low end of heart rate zone) OR 85% (high end) = 82 OR 107

    '82 + 65 (resting heart rate) = 147
    '107 + 65 (rhr) = 172
    'The target heart rate zone for this person would be 147 to 172

    Function TrainingHeartRate(ByVal age As Long, ByVal restingRate As Double, ByVal flag As Boolean) As Long
    Dim HRreserve As Double, UpperTrainRate As Double, LowTrainRate As Double
    Dim maxHR As Double

    maxHR = 206.9 - (0.67 * age)
    HRreserve = maxHR - restingRate

    LowTrainRate = CLng(HRreserve * 0.65 + restingRate)
    UpperTrainRate = CInt(HRreserve * 0.85 + restingRate)

    If flag = True Then
    TrainingHeartRate = UpperTrainRate
    Else
    TrainingHeartRate = LowTrainRate
    End If
    End Function
    'the person enters their age for example 20 and their resting heart rate is 70
    'their training heart rate should be 148 but it's giving me a answer of 180
    'how can i fix this so it gives me a answer of 148
    'I get 175 and 150 using the revised formula
    Sub drv()

    MsgBox TrainingHeartRate(20, 70, True)
    MsgBox TrainingHeartRate(20, 70, False)

    End Sub
    [/VBA]

    Paul

  18. #18
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Frankly Paul I strongly disagree with you posting that. The OP is being, IMO, either very lazy or has very poor grasp of basic math - or as I suspect, both. I posted repeatedly that the OP needs to adjust the math. And I STRONGLY feel that they did NOTHING. And handing it to them is a not a good thing.

    That being said, Ajaman, can you see what I was talking about. YOU had:

    maxHR = 220 - age
    HRreserve = maxHR - restingRate

    LowTrainRate = CInt(HRreserve * 0.5 + restingRate)
    UpperTrainRate = CInt(HRreserve * 0.85 + restingRate)


    Paul posted:

    maxHR = 206.9 - (0.67 * age)
    HRreserve = maxHR - restingRate

    LowTrainRate = CLng(HRreserve * 0.65 + restingRate)
    UpperTrainRate = CInt(HRreserve * 0.85 + restingRate)


    Can you see the minor changes? Do you understand them???

    I do wish Paul had not posted though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •