Problem checking textboxes for null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sharkiness
    New Member
    • Sep 2008
    • 19

    Problem checking textboxes for null

    Moderator
    This thread has been moved. New questions should go into new threads.

    Hi Guys,

    I am completely lost, trying to get the code found in:
    how-check-if-all-textboxes-form-null to work

    I am trying the code but I keep getting the error;

    'The object doesn't support this property or method'

    Runtime error '438'.

    Code:
            Dim ctl As control
            Dim check As Boolean
            
                check = True
                  
            For Each ctl In Me.Controls
                  If ctl.contoltype = acTextBox Then
                        If ctl.Value = "" Then
                         check = False
                End If
            
            End If
            
        Next ctl
        
                If check = False Then
                
                MsgBox "There is no information provided", vbOKOnly
                Me.lstResults.RowSource = ""
                Else
                ListSearchresults
                
                End If
    The error flags
    Code:
    If ctl.contoltype = acTextBox Then
    and when I hover over actextbox is states actextbox=109

    What i want is when the search button is clicked, to make sure at least one text box has a value. if none have a value then to throw the error message and exit sub, other run the Listsearchresul ts sub.

    Any help and guidance would be much appreciated.
    Last edited by TheSmileyCoder; Mar 2 '12, 11:12 AM. Reason: Moved question to its own thread. New question=New thread.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    It would appear you have a typo. Its called ControlType, not contoltype.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32662

      #3
      The clue is in the fact that the property is spelled with all lower-case letters. IntelliSense will fix that for you if it's recognised. Clearly, in this case it doesn't recognise what you've typed ==> Mistyped.

      It's why creating items with all lower-case characters (such as [check]) is recommended against. That and Require Variable Declaration can help you to avoid such simple errors in future. Think of these ideas as tools to help you avoid them.

      Comment

      • patjones
        Recognized Expert Contributor
        • Jun 2007
        • 931

        #4
        I forgot all about that thread, and yet it illustrates many good points.

        One thing that I will say about IntelliSense is that it usually, but does not always, behave as you might expect it to. In the present case, where we defined "ctl" as a Control typed variable and then refer to "Value" as a method of it...IntelliSen se will not capitalize Value for you - presumably because the control type of "ctl" is arbitrary (it may describe a control that cannot have a value). Yet, the code will compile and execute just fine.

        I also think that another crucial point, taken from the original thread, is that the If statements must be nested for this to work out properly. The first time that I tried to use code like this, it took me time to realize that the system evaluates both components of an AND even if the first component evaluates to False.

        Pat

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32662

          #5
          Originally posted by PatJones
          PatJones:
          In the present case, where we defined "ctl" as a Control typed variable and then refer to "Value" as a method of it...IntelliSen se will not capitalize Value for you - presumably because the control type of "ctl" is arbitrary (it may describe a control that cannot have a value).
          That's not accurate Pat. Intellisense will capitalise it for you unless you have somewhere redefined "Value". This can be done by dimming a variable for instance :
          Code:
          Dim value As Variant
          After this the system will treat all .Value references as .value. One of the reasons that reusing known names for your items is also recommended against.

          In fact, it will quite happily match case for any known names, whether or not they are valid properties of the object. Try using the following for instance (where txtName is a TextBox control) :
          Code:
          Me.txtName.querydefs = 3
          Even though TextBoxes have no .QueryDefs property, it will be made to match .QueryDefs for you.
          Last edited by NeoPa; Mar 2 '12, 09:02 PM.

          Comment

          • patjones
            Recognized Expert Contributor
            • Jun 2007
            • 931

            #6
            Correct you are. I got mixed up. When I tried it just now it does indeed capitalize "Value" for you. What it does not do is bring "Value" up in the list of methods for "ctl", an observation that fits in more naturally with what I was trying to say.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32662

              #7
              Originally posted by PatJones
              PatJones:
              What it does not do is bring "Value" up in the list of methods for "ctl", an observation that fits in more naturally with what I was trying to say.
              Indeed Pat. That also makes sense of course, as a Control object is almost as non-specific as a Variant (Which is what you seemed to be saying the first time round, to be fair). A reason for using such types only where the code needs to handle various different types of entities.

              To say that another way, when dealing with a specific type of control, for instance TextBox, it makes more sense to use a variable of type TextBox. Only when dealing with different types of control does it make sense to use one of type Control. For this case it's valid to use the Control type for the variable, of course.

              Comment

              Working...