Page 1 of 4 1234 LastLast
Results 1 to 40 of 144

Thread: [RESOLVED] Runtime error '91': Object variable or with block not set

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Resolved [RESOLVED] Runtime error '91': Object variable or with block not set

    Please help out with a clue on how to resolve the below problems. I have attached the file for a reference.
    Code:
    1. If i click on the execute button I get a run time error '91' with these lines, which refers to the output forms: frmoffcalculatedoutput.lblanodeperimerter.Caption = anodeperimerter
    It worked fine with the onshore form but don't know why is not working with this one.
    2. I get runtime error '11': Division by zero with this line in the code:
    Select Case cboorientation.Text
    Case "Bracelet Anode"
    spacingfactor = 1 + (((CDbl(txtenvironmentalresist.Text) * Math.Log(0.66 * numberofanodes))) / (3.142 * anoderesistance * CDbl(txtbanodespacing.Text)))
    Case "Slender Sled Anode"
    spacingfactor = 1 + (((CDbl(txtenvironmentalresist.Text) * Math.Log(0.66 * numberofanodes))) / (3.142 * anoderesistance * CDbl(txtsanodespacing.Text)))
    3. The frame "Framebraceletanode" won't display at run time even though it declared with the below code:
    Private Sub cboorientation_GotFocus()
    cboorientation.Clear
    With cboorientation
    .AddItem "Bracelet Anode "
    .AddItem "Slender Sled Anode"
    End With
    End Sub
    Attached Files Attached Files

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Sorry, thought i had the right form posted. I have reattached the file with the same thread title. Thanks and very grateful.

  4. #4

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    Okay I have all the parts now. When I run the program the Cathodic Protection form shows up. Please tell me step by step how to recreate the problem.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    During run-time, when debugging the code i insert break points and when i get an error with a line I change the line that has the problem to a comment and continue pressing the F5 key to identify other sections that might probably have run time error. Thanks

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    On the cathodic protection form, I just click on the cathodic protection link and a drop down menu appears having onshorecpsystems and offshorecpsystems. I click on the offshorecpsystems and it pops up the GUI for the offshorecpsystems. I put in my values and click on the calculate command

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    You are trying to refer to labels that don't exist. For example frmoffcalculatedoutputç.lblAnodePerimeter, frmoffcalculatedoutput.lblanodebedresistance, and frmoffcalculatedoutput.lbldesigndrivingvoltage.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Thanks for that. But I have those labels on the output for. Don't just understand why is not connecting. Even the frmoffcalculatedoutput.Show line which was suppose to display the result form display the same message. How do I fix it since I have the results and the output labels?

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    Quote Originally Posted by Ehwenomare View Post
    Thanks for that. But I have those labels on the output for. Don't just understand why is not connecting. Even the frmoffcalculatedoutput.Show line which was suppose to display the result form display the same message. How do I fix it since I have the results and the output labels?
    If the "output form" you are referring to is frmoffcalculatedoutput then those labels I referred to are NOT on that form.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Thank you MartinLiss. You are very good. I found out were I was having that error and it turned out to be that I declared frmoffcalculatedoutput as form at the beginning of the line. I had to delete that line and lo and behold here comes my output form with the figures in it except for these lines:
    Code:
    Case "Bracelet Anode"
    spacingfactor = 1 + (((CDbl(txtenvironmentalresist.Text) * Math.Log(0.66 * numberofanodes))) / (3.142 * anoderesistance * CDbl(txtbanodespacing.Text)))
    Case "Slender Sled Anode"
    spacingfactor = 1 + (((CDbl(txtenvironmentalresist.Text) * Math.Log(0.66 * numberofanodes))) / (3.142 * anoderesistance * CDbl(txtsanodespacing.Text)))
    
    
    and these ones:
    
    
    Private Sub cboorientation_GotFocus()
    cboorientation.Clear
    With cboorientation
    .AddItem "Bracelet Anode "
    Still giving me problems. Thanks for your time

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Found out that the problem with my application could be related to the two frames:
    1.Framebraceletanode
    2.Frameslendersledanode
    which belong to the braclet anode and the slender anode. Trying to separate the frames I found that it appears that the braclet anode frame is integrated to the slender anode frame. I tried deleting and creating another frame but still experiencing same problem. Tried creating another project I still get same thing. Maybe I have checked a property box that may be giving this error. Do you have any clue to this problem?Thanks for helping

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    The text in your combo box is "Bracelet Anode " and you are checking for "Bracelet Anode".

    That mistake happens here.


    Code:
    Private Sub cboorientation_GotFocus()
    cboorientation.Clear
    With cboorientation
    .AddItem "Bracelet Anode " ' Notice you have a space after Anode
    .AddItem "Slender Sled Anode"
    End With
    End Sub
    Do you know how to use the Debug feature of VB?

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Yeah i fixed that and still same problem. Don't have a clue how.

  15. #15
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    I just noticed that you are filling the combobox every time it gets focus. Why not just do this in Form_Load?

    Code:
    With cboorientation
    .AddItem "Bracelet Anode "
    .AddItem "Slender Sled Anode"
    End With
    I hope you don't mind but I found a few other things that you should change.

    1.

    I gave you this code that uses Select Case.
    Code:
    Function anodemas() As Double
    Call mcurrequirement
    Select Case cboorientation.Text
    Case "Bracelet Anode"
    anodemas = (mcurrequirement * CDbl(cbodesignlife.Text) * 8760) / (CDbl(txtbenercab.Text) * CDbl(txtbanoutilisfac.Text))
    Case "Slender Sled Anode"
    anodemas = (mcurrequirement * CDbl(cbodesignlife.Text) * 8760) / (CDbl(txtsenercab.Text) * CDbl(txtsanoutilisfac.Text))
    Case Else
        ' you may not need the 'Else' case
        MsgBox "unexpected value"
        End Select
        End Function
    When I did that I didn't know that your combo box contained just two items. In that case this would be better.

    Code:
    Function anodemas() As Double
    Call mcurrequirement
    If cboorientation.Text = "Bracelet Anode" Then
        anodemas = (mcurrequirement * CDbl(cbodesignlife.Text) * 8760) / (CDbl(txtbenercab.Text) * CDbl(txtbanoutilisfac.Text))
    Else
        anodemas = (mcurrequirement * CDbl(cbodesignlife.Text) * 8760) / (CDbl(txtsenercab.Text) * CDbl(txtsanoutilisfac.Text))
    End If
    End Function
    2.

    You really should indent your code. This is difficult to read.

    Code:
    'Calculation of anode area
    Function anodearea() As Double
    Const Pi = 3.142
    If (cbocpstype.Text = "Bracelet Anode") Then
    If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
    Call anodediameter
    anodearea = Pi * anodediameter * CDbl(txtbanodelength.Text)
    Else
    anodearea = Pi * CDbl(txtbanodediameter.Text) * CDbl(txtbanodelength.Text)
    End If
    End If
    If (cbocpstype.Text = "Slender Sled Anode") Then
    If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
    Call anodediameter
    anodearea = Pi * anodediameter * CDbl(txtsanodelength.Text)
    Else
    anodearea = Pi * CDbl(txtsanodediameter.Text) * CDbl(txtsanodelength.Text)
    End If
    End If
    End Function
    This isn't

    Code:
    'Calculation of anode area
    Function anodearea() As Double
    Const Pi = 3.142
    If (cbocpstype.Text = "Bracelet Anode") Then
        If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
            Call anodediameter
            anodearea = Pi * anodediameter * CDbl(txtbanodelength.Text)
        Else
            anodearea = Pi * CDbl(txtbanodediameter.Text) * CDbl(txtbanodelength.Text)
        End If
    End If
    If (cbocpstype.Text = "Slender Sled Anode") Then
        If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
            Call anodediameter
            anodearea = Pi * anodediameter * CDbl(txtsanodelength.Text)
        Else
            anodearea = Pi * CDbl(txtsanodediameter.Text) * CDbl(txtsanodelength.Text)
        End If
    End If
    End Function
    or just
    Code:
    'Calculation of anode area
    Function anodearea() As Double
    Const Pi = 3.142
    If (cbocpstype.Text = "Bracelet Anode") Then
        If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
            Call anodediameter
            anodearea = Pi * anodediameter * CDbl(txtbanodelength.Text)
        Else
            anodearea = Pi * CDbl(txtbanodediameter.Text) * CDbl(txtbanodelength.Text)
        End If
    Else
        If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
            Call anodediameter
            anodearea = Pi * anodediameter * CDbl(txtsanodelength.Text)
        Else
            anodearea = Pi * CDbl(txtsanodediameter.Text) * CDbl(txtsanodelength.Text)
        End If
    End If
    End Function
    When I indent this code it points out an error. Do you see the error?
    Code:
    'Calculation of anode resistance
    Function anoderesistance() As Double
    Const Pi = 3.142
    Call anodearea
    If (cbocpstype.Text = "Bracelet Anode") Then
        anoderesistance = (0.315 * CDbl(txtenvironmentalresist.Text)) / Math.Sqr(anodearea)
        If (cbocpstype.Text = "Slender Sled Anode") Then
            If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
                Call anodediameter
                anoderesistance = (CDbl(txtenvironmentalresist.Text) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * anodediameter)) - 1)) / (2 * Pi * CDbl(txtsanodelength.Text))
            Else
                anoderesistance = (CDbl(txtenvironmentalresist.Text) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * CDbl(txtsanodediameter.Text))) - 1)) / (2 * Pi * CDbl(txtsanodelength.Text))
            End If
        End If
    End If
    End Function

  16. #16

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Oh Yeah. You are very correct. That line is a bane in the ass. Found it was returning a null value but couldn't figure it out and it just divide the numbers resulting to a run-time error of division by zero. Thanks for spotting it out and also for the infor for the combo boxes. You are a good teacher.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    The frame for the bracelet anode doesn't.

  19. #19

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    I figured it out how to attach the file again without having to create a new thread. Thanks
    Attached Files Attached Files
    Last edited by Ehwenomare; Jul 23rd, 2011 at 11:42 AM. Reason: Adding attachment

  21. #21
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    I found the error. In several places you have cboorientation.Clear. I know that I suggested that you move the filling of the combo from the GotFocus event to Load, and that's the right place for it, but you need to remove those 'Clear' lines.

    BTW there are at least two ways you could have found the problem.
    1. The way I did it was that I saw that you were loading the combo but it was showing up blank so I looked at all the occurrences of cboorientation to see where it was used and I found the Clear statements.

    2. Another way would have been to set up a Watch. I assume that you don't know how to do that and if you want to learn how and a lot more about debugging you should read my VB6 Debug Tutorial

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Thanks MartinLiss for the handful of details. Were you able get the Framebraceletanode frame dispayed? I can only see the frame for the slender anodes. Please could you afford a step step guide. Got those clear things removed but still can only have one frame displayed. Thanks

  23. #23
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    Code:
    Private Sub cboorientation_Click()
    
    offshorecpsystems.Height = 11025
    
    If (cboorientation.Text = "Bracelet Anode") Then
       Framebraceletanode.Enabled = True
       Framebraceletanode.Visible = True 'You didn't have this line
       Frameslendersledanode.Enabled = False
       Frameslendersledanode.Visible = False
    Else
       Framebraceletanode.Enabled = False
       Framebraceletanode.Visible = False
       Frameslendersledanode.Enabled = True
       Frameslendersledanode.Visible = True
    End If
    
    End Sub
    I added the line you were missing and I also 'balanced' the code so that the same things were done for the Slender Anode as for the Bracelet Anode.

    I just want to point out that you probably don't ever have to worry about enabling and disabling those frames since you are already making them visible and invisible.

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Yeah! million thanks. I would never had been able to figure it out. Words of mouth can't express how grateful I am. You will forever be cherished in my heart from generation to generation. My thesis is dedicated to you cz you made it possible. Can never repay you for all you have done, especially for you care and time. Before I sign of this thread I would please like you to resolve the anoderesistance line for me. It still gives me a zero. Thanks MartinLiss. Forever remembered.

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Please also look at this line for me if is ok. Thanks
    Code:
        Function LCC() As Double
            Dim n As Double
        n = txtlifespan.Text
            Dim i As Double
            Dim t As Double
            Dim LCCC As Double
    
            t = (CDbl(txtdiscountcost.Text) / 12)
            LCCC = CDbl(txtinitialcost.Text) + CDbl(txtinstalcommcost.Text) + CDbl(txtenergycost.Text) + CDbl(txtoperatingcost.Text) + CDbl(txtmaintenancecost.Text) + CDbl(txtdowncost.Text) + CDbl(txtenvironmentalcost.Text) + CDbl(txtdecomcost.Text) + CDbl(txtreplacementcost.Text) - CDbl(txtsalvagecost.Text)
    
            For i = 0 To n
                LCC = LCCC / ((1 + t) ^ i)
    
            Next
    
        End Function
    Last edited by Ehwenomare; Jul 23rd, 2011 at 06:50 PM. Reason: error

  26. #26
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    Quote Originally Posted by Ehwenomare View Post
    Yeah! million thanks. I would never had been able to figure it out. Words of mouth can't express how grateful I am. You will forever be cherished in my heart from generation to generation. My thesis is dedicated to you cz you made it possible. Can never repay you for all you have done, especially for you care and time. Before I sign of this thread I would please like you to resolve the anoderesistance line for me. It still gives me a zero. Thanks MartinLiss. Forever remembered.
    You're welcome. Please describe what you mean by "please… resolve the anode resistance line".

  27. #27
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    Code:
    Function LCC() As Double
            Dim n As Double
            Dim i As Double
            Dim t As Double
            Dim LCCC As Double
    
            t = (CDbl(txtdiscountcost.Text) / 12)
            LCCC = CDbl(txtinitialcost.Text) + CDbl(txtinstalcommcost.Text) + CDbl(txtenergycost.Text) + CDbl(txtoperatingcost.Text) + CDbl(txtmaintenancecost.Text) + CDbl(txtdowncost.Text) + CDbl(txtenvironmentalcost.Text) + CDbl(txtdecomcost.Text) + CDbl(txtreplacementcost.Text) - CDbl(txtsalvagecost.Text)
    
            For i = 0 To n
                LCC = LCCC / ((1 + t) ^ i)
    
            Next
    
        End Function
    This function has several problems.

    1. Where is 'n' defined? Unless it's a global variable and is defined at the top of a form or code module it will always be 0.
    2. Since the name of the function is LCC, when you calculate LCC its value will be returned by the function. However it looks like you are trying to calculate it in a loop, but no matter what the value of 'n' is the the calculation of LCC so doesn't change, so why the loop?

    Maybe if you explain in english what it is that you want to do in this function maybe I can help.

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Code:
    'Calculation of anode resistance
    Function anoderesistance() As Double
    Const Pi = 3.142
    Call anodearea
    If (cbocpstype.Text = "Bracelet Anode") Then
        anoderesistance = (0.315 * CDbl(txtenvironmentalresist.Text)) / Math.Sqr(anodearea)
        If (cbocpstype.Text = "Slender Sled Anode") Then
            If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
                Call anodediameter
                anoderesistance = (CDbl(txtenvironmentalresist.Text) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * anodediameter)) - 1)) / (2 * Pi * CDbl(txtsanodelength.Text))
            Else
                anoderesistance = (CDbl(txtenvironmentalresist.Text) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * CDbl(txtsanodediameter.Text))) - 1)) / (2 * Pi * CDbl(txtsanodelength.Text))
            End If
        End If
    End If
    End Function
    Code:
     Function LCC() As Double
            Dim n As Double
        n = txtlifespan.Text
            Dim i As Double
            Dim t As Double
            Dim LCCC As Double
    
            t = (CDbl(txtdiscountcost.Text) / 12)
            LCCC = CDbl(txtinitialcost.Text) + CDbl(txtinstalcommcost.Text) + CDbl(txtenergycost.Text) + CDbl(txtoperatingcost.Text) + CDbl(txtmaintenancecost.Text) + CDbl(txtdowncost.Text) + CDbl(txtenvironmentalcost.Text) + CDbl(txtdecomcost.Text) + CDbl(txtreplacementcost.Text) - CDbl(txtsalvagecost.Text)
    
            For i = 0 To n
                LCC = LCCC / ((1 + t) ^ i)
    
            Next
    
        End Function
    I am trying to calculate the life cycle cost of a system for time of design to decommisiong using the net present value equation were i am trying to estimate future cost for an estimated number of years by converting it to present value to estimate if the project is feasible to embark on or not. Thanks

  29. #29
    Junior Member
    Join Date
    Jul 2011
    Posts
    26

    Re: Runtime error '91': Object variable or with block not set

    Hi MartinLiss,

    I have been going through your responses to this thread and I have been particularly impressed with them.

    I was wondering if you would be able to look into a similar code written using visual basics 2010. The errors are similar even though not totally the same.

  30. #30
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    Quote Originally Posted by nji2rhyme View Post
    Hi MartinLiss,

    I have been going through your responses to this thread and I have been particularly impressed with them.

    I was wondering if you would be able to look into a similar code written using visual basics 2010. The errors are similar even though not totally the same.
    Thanks. I'm sorry, but I know nothing about VS 2010, however if you post in the .Net forum I'm sure you'll get great help.

  31. #31
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    Quote Originally Posted by Ehwenomare View Post
    Code:
    'Calculation of anode resistance
    Function anoderesistance() As Double
    Const Pi = 3.142
    Call anodearea
    If (cbocpstype.Text = "Bracelet Anode") Then
        anoderesistance = (0.315 * CDbl(txtenvironmentalresist.Text)) / Math.Sqr(anodearea)
        If (cbocpstype.Text = "Slender Sled Anode") Then
            If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
                Call anodediameter
                anoderesistance = (CDbl(txtenvironmentalresist.Text) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * anodediameter)) - 1)) / (2 * Pi * CDbl(txtsanodelength.Text))
            Else
                anoderesistance = (CDbl(txtenvironmentalresist.Text) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * CDbl(txtsanodediameter.Text))) - 1)) / (2 * Pi * CDbl(txtsanodelength.Text))
            End If
        End If
    End If
    End Function
    Code:
     Function LCC() As Double
            Dim n As Double
        n = txtlifespan.Text
            Dim i As Double
            Dim t As Double
            Dim LCCC As Double
    
            t = (CDbl(txtdiscountcost.Text) / 12)
            LCCC = CDbl(txtinitialcost.Text) + CDbl(txtinstalcommcost.Text) + CDbl(txtenergycost.Text) + CDbl(txtoperatingcost.Text) + CDbl(txtmaintenancecost.Text) + CDbl(txtdowncost.Text) + CDbl(txtenvironmentalcost.Text) + CDbl(txtdecomcost.Text) + CDbl(txtreplacementcost.Text) - CDbl(txtsalvagecost.Text)
    
            For i = 0 To n
                LCC = LCCC / ((1 + t) ^ i)
    
            Next
    
        End Function
    I am trying to calculate the life cycle cost of a system for time of design to decommisiong using the net present value equation were i am trying to estimate future cost for an estimated number of years by converting it to present value to estimate if the project is feasible to embark on or not. Thanks
    I'm sorry to say that the math concept is way over my head. However I was pointing out before that you have a problem here:
    Code:
    If (cbocpstype.Text = "Bracelet Anode") Then
        anoderesistance = (0.315 * CDbl(txtenvironmentalresist.Text)) / Math.Sqr(anodearea)
        If (cbocpstype.Text = "Slender Sled Anode") Then
            If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
    Think about what that code is saying. It says that if (cbocpstype.Text = "Bracelet Anode") then do something, and then if (cbocpstype.Text = "Slender Sled Anode") then do something additional. cbocpstype can't have two values at the same time so you need to change the code to
    Code:
    If cbocpstype.Text = "Bracelet Anode" Then ' note that the parens aren't necessary
        ' do something
    Else
        ' Slender Sled Anode has been selected so do something else
    End If

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Hi MartinLiss, T tried debugging the line giving me a problem and it was shocking at my findings. I have attached what I did as a reference. I created a new form with combo boxes and textboxes. Added the code to the form. When I executed the code choosing bracelet anode on the combo box I got a value but on choosing slender anode i get a result of zero. I added a break point and to my amazement found out that the compiler skips the command for the bracelet anode. Please do you know why and why is it that at every execution I make the combo box just keep on duplicating. I have the same problem with my other code of the combo duplicating the bacelet anode and the slender anode
    Code:
    Private Sub cboorientation_GotFocus()
        With cboorientation
            .AddItem "Bracelet Anode"
            .AddItem "Slender Sled Anode"
        End With
    
    End Sub
    
    Private Sub cmdCalculate_Click()
    txtanoderesistance.Text = anoderesistance
    End Sub
    'Calculation of anode resistance
    Function anoderesistance() As Double
        Const Pi = 3.142
        If (cboorientation.Text = "Bracelet Anode") Then
            anoderesistance = (0.315 * CDbl(txtenvironmentalresist.Text)) / Math.Sqr(CDbl(txtanodearea.Text))
            If (cboorientation.Text = "Slender Sled Anode") Then
            If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
                    
                    anoderesistance = (CDbl(txtenvironmentalresist.Text) / (2 * Pi * CDbl(txtsanodelength.Text))) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * CDbl(txtanodediameter.Text))) - 1)
               Else
                anoderesistance = (CDbl(txtenvironmentalresist.Text) / (2 * Pi * CDbl(txtsanodelength.Text))) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * CDbl(txtsanodediameter.Text))) - 1)
                End If
            End If
            'Exit Function
        End If
    End Function
    Attached Files Attached Files

  33. #33
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Runtime error '91': Object variable or with block not set

    This coded has the same problem as the one that I tried to explain at the bottom of post #31. Let me try to explain it this way.

    Your current code with some added comments:
    VB Code:
    1. Function anoderesistance() As Double
    2.     Const Pi = 3.142
    3.     If (cboorientation.Text = "Bracelet Anode") Then
    4.         ' If cboorientation.Text = "Bracelet Anode" the next line is executed, otherwise we go to the line
    5.         ' below that I marked with HERE
    6.         anoderesistance = (0.315 * CDbl(txtenvironmentalresist.Text)) / Math.Sqr(CDbl(txtanodearea.Text))
    7.         ' Since we alrready know that cboorientation.Text = "Bracelet Anode", this next line can NEVER be true
    8.         If (cboorientation.Text = "Slender Sled Anode") Then
    9.         If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
    10.                
    11.                 anoderesistance = (CDbl(txtenvironmentalresist.Text) / (2 * Pi * CDbl(txtsanodelength.Text))) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * CDbl(txtanodediameter.Text))) - 1)
    12.            Else
    13.             anoderesistance = (CDbl(txtenvironmentalresist.Text) / (2 * Pi * CDbl(txtsanodelength.Text))) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * CDbl(txtsanodediameter.Text))) - 1)
    14.             End If
    15.         End If
    16.         'Exit Function
    17.     End If 'HERE
    18. End Function

    I don't know that math involved but it looks to me like the function should be written this way.

    VB Code:
    1. Function anoderesistance() As Double
    2.     Const Pi = 3.142
    3.     If (cboorientation.Text = "Bracelet Anode") Then
    4.         anoderesistance = (0.315 * CDbl(txtenvironmentalresist.Text)) / Math.Sqr(CDbl(txtanodearea.Text))
    5.     Else
    6.         'cboorientation.Text is not "Bracelet Anode" so it must be "Slender Sled Anode"
    7.         If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then
    8.             anoderesistance = (CDbl(txtenvironmentalresist.Text) / (2 * Pi * CDbl(txtsanodelength.Text))) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * CDbl(txtanodediameter.Text))) - 1)
    9.         Else
    10.             anoderesistance = (CDbl(txtenvironmentalresist.Text) / (2 * Pi * CDbl(txtsanodelength.Text))) * (2.3 * Math.Log((4 * CDbl(txtsanodelength.Text)) / (0.5 * CDbl(txtsanodediameter.Text))) - 1)
    11.         End If
    12.     End If
    13. End Function


    BTW why are you still doing this?
    Code:
    Private Sub cboorientation_GotFocus()
        With cboorientation
            .AddItem "Bracelet Anode"
            .AddItem "Slender Sled Anode"
        End With
    
    End Sub

  34. #34

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: Runtime error '91': Object variable or with block not set

    Thank you MartinLiss. You are a GENIUS. Made a great impact in my life. Got this project done. Only problem I am still having is the Vbyes msg box which pops multiple of time cz of the call statements I used in the functions. Got any clue how to exit it from occurring at every call. Thank you very much.

  35. #35
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: [RESOLVED] Runtime error '91': Object variable or with block not set

    The easiest way would put a pair of radio buttons on the form, one of which would say 'Yes' and the other 'No' under or next to a label which says "Is anode cross sectional non-cylindrical?". I've attached a small project that shows how its done since I'm assuming you've never used radio buttons.

    BTW if I were designing your app I would always use radio buttons instead of comboboxes when there were only 2 or 3 values for the user to choose from like with the choice between "Bracelet Anode" and "Slender Sled Anode".
    Attached Files Attached Files

  36. #36

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: [RESOLVED] Runtime error '91': Object variable or with block not set

    Thanks. I will consider the second part after I get this thing handed in. I am concerned about the first one. Ok, if I use the radio button how do I assign the instruction to the line i want executed when it has to do with a "Yes" "NO" situation?

  37. #37
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: [RESOLVED] Runtime error '91': Object variable or with block not set

    Instead of

    If MsgBox("Is anode cross sectional non-cylindrical?", vbInformation + vbQuestion, "Confirm") = vbYes Then

    you would just do

    If optYes.Value = True Then

  38. #38

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: [RESOLVED] Runtime error '91': Object variable or with block not set

    Thank You.

  39. #39

    Thread Starter
    Addicted Member
    Join Date
    Jul 2011
    Posts
    139

    Re: [RESOLVED] Runtime error '91': Object variable or with block not set

    Hi MartiLiss, how do I use the If statement for the below code? If I use the IF statement it returns a value of zero. And how do I use the For loop to code the comment:For each 1 degree rise in temparature above 50 degree in this part of the code:
    vb Code:
    1. Case (CDbl(txtoperattemp.Text) > 50)
    Thanks

    Code:
    'Calculation of current requirement
    Function currequirement() As Double
        Call finalbrkdownfact
        Call unitsurface
        Const Pi = 3.142
        If MsgBox("Is Pipeline coated?", vbInformation + vbYesNo, "Confirm") = vbYes Then
            MsgBox ("initial current requirement should be neglected in the design calculations")
        Else
            MsgBox "Values for the initial, mean, and final current densities shall be accounted for during the design of the CP systems", vbQuestion + vbYesNo, "Confirm"
        End If
        Select Case cbopipeline.Text
            Case "Uncoated"
                currequirement = CDbl(txtinitialcurrentdensity.Text) * finalbrkdownfact * unitsurface
                'For each 1 degree rise in temparature above 50 degree
            Case (CDbl(txtoperattemp.Text) > 50)
                currequirement = (CDbl(txtinitialcurrentdensity.Text) + 1) * finalbrkdownfact * unitsurface
    
            Case "Coated"
                currequirement = CDbl(txtmeancurrentdensity.Text) * finalbrkdownfact * unitsurface
                'For each 1 degree rise in temparature above 50 degree
            Case (CDbl(txtoperattemp.Text) > 50)
                currequirement = (CDbl(txtmeancurrentdensity.Text) + 1) * finalbrkdownfact * unitsurface
            Case Else
                MsgBox ("unexpected value")
        End Select
        If (CDbl(txtoperattemp.Text) > 80) Then
            MsgBox "A special assessment of current densities should be carried out for external pipe surface to ascertain anode and coating condition", vbInformation + vbQuestion, "Confirm"
        End If
    End Function

  40. #40
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: [RESOLVED] Runtime error '91': Object variable or with block not set

    The way the Select Case statement works is that VB compares the value on the expression in the 'Select Case' (in other words in your case the value of cbopipeline.Text) and compares it with each 'Case' expression one at at time until one of them is the same. So in your case if the user had selected "Coated" VB would look at then first Case and determine that "Uncoated" was not the same as "Coated" and move to the next Case where it would find that whatever number was in the txtoperattemp textbox was not the same as "Coated" and move to the next Case where it would find a match between "Coated" and "Coated". It would then perform whatever code you had there and go to the 'End Select' skipping any following Case statements.

    So I think this is what you want but again I must warn you that I don't know the math.

    Code:
        Select Case cbopipeline.Text
            Case "Uncoated"
                If CDbl(txtoperattemp.Text) > 50 Then
                    ' This would add 25 if for example txtoperattemp.Text were 75
                    currequirement = (CDbl(txtinitialcurrentdensity.Text) + CDbl(txtoperattemp.Text) - 50) * finalbrkdownfact * unitsurface
                Else
                    currequirement = CDbl(txtinitialcurrentdensity.Text) * finalbrkdownfact * unitsurface
                End If
            Case "Coated"
                If CDbl(txtoperattemp.Text) > 50 Then
                    currequirement = (CDbl(txtmeancurrentdensity.Text) + CDbl(txtoperattemp.Text) - 50) * finalbrkdownfact * unitsurface
                Else
                    currequirement = CDbl(txtmeancurrentdensity.Text) * finalbrkdownfact * unitsurface
                End If
        End Select
    Note that while I obviously like Select Case, in you situation where there are I assume only two possible values for cbopipeline.Text you could also do this

    Code:
        If cbopipeline.Text = "Uncoated" Then
            If CDbl(txtoperattemp.Text) > 50 Then
                ' This would add 25 if for example txtoperattemp.Text were 75
                currequirement = (CDbl(txtinitialcurrentdensity.Text) + CDbl(txtoperattemp.Text) - 50) * finalbrkdownfact * unitsurface
            Else
                currequirement = CDbl(txtinitialcurrentdensity.Text) * finalbrkdownfact * unitsurface
            End If
        Else
            If CDbl(txtoperattemp.Text) > 50 Then
                currequirement = (CDbl(txtmeancurrentdensity.Text) + CDbl(txtoperattemp.Text) - 50) * finalbrkdownfact * unitsurface
            Else
                currequirement = CDbl(txtmeancurrentdensity.Text) * finalbrkdownfact * unitsurface
            End If
        End If
    There's a special case of Select Case that you should be aware of.

    Code:
    Select Case True
        Case SomeVar = 4
            'do something
        Case SomeOtherVar = 33
            ' do something else
        Case Text1.Text = "It's a nice day"
            MsgBox "Go for a run"
    End Select
    Also be aware that you can do things like this
    Code:
    Select Case MyVar
        Case 1, 3, 7
            ' do something when MyVar is 1, 3 or 7
        Case 14 - 22, 66
            ' do somthing when it between 14 and 22 inclusive or it's 66
        Case Is > 99
            ' it is over 99
    End Select
    Holler if you have any questions

Page 1 of 4 1234 LastLast

Posting Permissions

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



Click Here to Expand Forum to Full Width