Backwards code works Forwards? Why?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyWaterloo
    New Member
    • Dec 2007
    • 135

    Backwards code works Forwards? Why?

    I quickly put this code together to test if it would disable some of my functions OnChange when my text box (purchase order number) is empty...and if it would enable those functions when I added data to my field. The funny thing is that if you read the code it actually runs in reverse! When my field is null it is suppose to disable...inste ad it enables...Why? So I put my code in to do the opposite of what I want and it works!?!?! I don't mind having my code in logical reverse as long as I get the results I'm looking for...but...I don't get it....???? ;-)


    Forwards: (works in reverse and enables features when Null)
    Code:
    Private Sub PurchaseOrderNumber_Change()
    If IsNull(Me.PurchaseOrderNumber) Then
    Me.Check107.Enabled = False
    Me.Check111.Enabled = False
    Me.Text77.Enabled = False
    Me.Label119.Visible = True
    Else
    Me.Check107.Enabled = True
    Me.Check111.Enabled = True
    Me.Text77.Enabled = True
    Me.Label119.Visible = False
    End If
    
    End Sub
    Backwards: (disables features when null)
    Code:
    Private Sub PurchaseOrderNumber_Change()
    If IsNull(Me.PurchaseOrderNumber) Then
    Me.Check107.Enabled = True
    Me.Check111.Enabled = True
    Me.Text77.Enabled = True
    Me.Label119.Visible = False
    Else
    Me.Check107.Enabled = False
    Me.Check111.Enabled = False
    Me.Text77.Enabled = False
    Me.Label119.Visible = True
    End If
    
    End Sub
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hello.

    I suggest you to perform a little experiment - add two controls on the form and in PurchaseOrderNu mber_Change event handler assign PurchaseOrderNu mber.Value property and PurchaseOrderNu mber.Text property values to them.

    Regards,
    Fish.

    Comment

    • MyWaterloo
      New Member
      • Dec 2007
      • 135

      #3
      Originally posted by FishVal
      Hello.

      I suggest you to perform a little experiment - add two controls on the form and in PurchaseOrderNu mber_Change event handler assign PurchaseOrderNu mber.Value property and PurchaseOrderNu mber.Text property values to them.

      Regards,
      Fish.
      Ooook that was interesting.... .So....what is that showing me. A little bizarre, but I have no idea what these results tell me... =-)




      "So, where did you learn Access?"

      "Learn Access? Who? Me?"

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Originally posted by MyWaterloo
        Ooook that was interesting.... .So....what is that showing me. A little bizarre, but I have no idea what these results tell me... =-)
        I guess it explains why your code behaves in a way you don't expect.

        Kind regards,
        Fish.

        Comment

        • MyWaterloo
          New Member
          • Dec 2007
          • 135

          #5
          Originally posted by FishVal
          I guess it explains why your code behaves in a way you don't expect.

          Kind regards,
          Fish.
          I don't understand how it explains what my code is doing. Why do the two controls behave the way they do? Why does the code work, but in a way that appears to be in reverse? I just don't see how the two controls reveal why the code behaves the way it does.






          "I can design a fantastic looking form for you. With buttons and menus and everything."
          "What do they do?"
          "What?"
          "The buttons and menus."
          "I have no idea, but they sure look good."

          Comment

          • MyWaterloo
            New Member
            • Dec 2007
            • 135

            #6
            OK. I see that (for whatever reason I don't know) this current process of mine will not work. I guess I'm at a dead end. I was hoping to disable certain features if one of my fields was not filled in i.e. null. =(

            Comment

            • FishVal
              Recognized Expert Specialist
              • Jun 2007
              • 2656

              #7
              Originally posted by MyWaterloo
              I don't understand how it explains what my code is doing. Why do the two controls behave the way they do? Why does the code work, but in a way that appears to be in reverse? I just don't see how the two controls reveal why the code behaves the way it does.
              I think it is quite straightforward .

              PurchaseOrderNu mber.Value property is a default property you implicitly invoke when checking condition for enabling/disabling.
              This little experiement shows you what this property returns when PurchaseOrderNu mber_Change event fires thus making clear why the code behaves the way it behaves. At the same time value returned by PurchaseOrderNu mber.Text property is , I guess, a more relevant criteria for making decision whether the controls should be enabled or disabled.

              Does it make more sense now?

              Comment

              • MyWaterloo
                New Member
                • Dec 2007
                • 135

                #8
                Yes, it kind of makes sense to me. I guess what it really means is that I need to come up with some other way to disable my print buttons if my P.O. number field is null. Any ideas? (Thanks for the lesson.)

                Comment

                • mshmyob
                  Recognized Expert Contributor
                  • Jan 2008
                  • 903

                  #9
                  subscribing just to watch the fun.

                  cheers,

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32661

                    #10
                    Did the values ever get posted?

                    Fish & the OP seem to be discussing something they can both see but I for one, don't see any values posted (Emperor's clothes! I was never one to try to hide my ignorance so I will always ask the questions).

                    Comment

                    • mshmyob
                      Recognized Expert Contributor
                      • Jan 2008
                      • 903

                      #11
                      I think what you were told was to use the .text property instead of the .value property.

                      You could also the the NZ function and then check the value of the variable to see if NULL and do the appropriate commands.

                      cheers,

                      Originally posted by MyWaterloo
                      Yes, it kind of makes sense to me. I guess what it really means is that I need to come up with some other way to disable my print buttons if my P.O. number field is null. Any ideas? (Thanks for the lesson.)

                      Comment

                      • mshmyob
                        Recognized Expert Contributor
                        • Jan 2008
                        • 903

                        #12
                        Just to reword Fishval's response....

                        There is a slight difference between the .Value and .Text property. It is subtle but an important difference.

                        The .Value property refers to the saved value of the textbox while the .text property refers to the current contents of the textbox. Therefore, until you lose focus you should use the .text property to determine the contents of the textbox.

                        Does this help explain why your code works in reverse?

                        cheers,

                        Comment

                        • MyWaterloo
                          New Member
                          • Dec 2007
                          • 135

                          #13
                          Originally posted by mshmyob
                          Just to reword Fishval's response....

                          There is a slight difference between the .Value and .Text property. It is subtle but an important difference.

                          The .Value property refers to the saved value of the textbox while the .text property refers to the current contents of the textbox. Therefore, until you lose focus you should use the .text property to determine the contents of the textbox.

                          Does this help explain why your code works in reverse?

                          cheers,
                          Yes, that helped bring light to the subject. So when I lose focus of the textbox I have to use the .value property?

                          Comment

                          • mshmyob
                            Recognized Expert Contributor
                            • Jan 2008
                            • 903

                            #14
                            I am glad it helps you a bit but you do not need to use the .value property you can use the .text property all the time.

                            cheers,

                            Originally posted by MyWaterloo
                            Yes, that helped bring light to the subject. So when I lose focus of the textbox I have to use the .value property?

                            Comment

                            • MyWaterloo
                              New Member
                              • Dec 2007
                              • 135

                              #15
                              Originally posted by mshmyob
                              I am glad it helps you a bit but you do not need to use the .value property you can use the .text property all the time.

                              cheers,
                              Ok. I will give it a go.

                              Comment

                              Working...