How to check for existence of a control label (without error handler)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jbrumbau
    New Member
    • Sep 2007
    • 52

    #1

    How to check for existence of a control label (without error handler)?

    Hello,

    Right now I'm having some performance issues (forms taking 2 or so seconds to check) when trying to check for all control labels in a loop. Currently the code I have is this:
    Code:
    Function hasLabel(ByRef ctl As Control) As Boolean
        Dim tempstr As String
        
    On Error GoTo err_handler
        tempstr = ctl.Controls(0).Name
        hasLabel = True
        Exit Function
    err_handler:
        hasLabel = False
        Exit Function
    End Function
    Though this works, I would prefer a more elegant solution that doesn't resort to an error handler if a control doesn't have a label. I think having this in a loop through all controls in a form is the reason why it doesn't update in the snap of a finger. Could somebody please advise me on a quicker/more elegant way to check for the existence of a label?

    Thanks.
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    This works for what? I thought all controls had a .Name? What is this telling you about a control?

    Jim

    Comment

    • jbrumbau
      New Member
      • Sep 2007
      • 52

      #3
      Yes, previously I was using the name of the control, but this is not the same as the label caption, so I would get stuff like txtSomeControl, cboSomeControl, and so on. This is not preferred since the user who doesn't know control box names won't always know what control this refers to.

      If you or somebody else can tell me how to check if a control has a label or not without an error handler it would be much appreciated.

      Thanks.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        Are you trying to ask how to determine if a specified control is a label?

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          If this is what you're after then look up ControlType Property in the help (Type ControlType then press F1 for context-sensitive help).

          This will tell you that a value of acLabel (100) indicates a label control.

          Comment

          • jbrumbau
            New Member
            • Sep 2007
            • 52

            #6
            No, just if the specified control has a label.

            I can't do it the other way around because not all controls have a label, I basically use the label name if it is available, and if not, use the control box name. But it just chews up CPU when a user tries to update 30 or so records when this type of change tracking is going on.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              Your function can then become :
              Code:
              Function hasLabel(ByRef ctl As Control) As Boolean
                  hasLabel = (ctl.ControlType = acLabel)
              End Function
              Though I would rename it to isLabel() myself.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32669

                #8
                Originally posted by jbrumbau
                jbrumbau: No, just if the specified control has a label.
                How can a control have a label? A Label is a control.

                Do you mean a .Caption property? It's hard to guess what you mean.

                Comment

                • jimatqsi
                  Moderator Top Contributor
                  • Oct 2006
                  • 1293

                  #9
                  I think the poster is considering text box objects (or other objects) paired with a label. Like when you create a new text box and Access glues a label to it; the poster is saying that text box "has" a label.

                  In fact, the label glued to that text box and the text box are separate objects, and I don't think you can find an association between them in the properties. I don't think it will help your speed issue any, but you could use the .tag property in the label or the text box (or both) to help you figure out if a label and another control are related to each other
                  Code:
                   if textboxname.tag = labelname.tag then
                   do same tag stuff
                  endif
                  They both have the same tag so they must go together.

                  However, I don't think that goes to your speed problem. And I don't think 2 seconds is a problem unless you are doing the process repetitively.

                  Would it help to build an array of all the labels in the OnOpen event of the form? In the array mark whether it is a control with a label or not. Then you can query the array instead of the objects and it won't take a noticeable amount of time to do that no matter how many times you do it.

                  Jim

                  Comment

                  • jbrumbau
                    New Member
                    • Sep 2007
                    • 52

                    #10
                    Jim, yes that is correct and that is one idea on how to check control labels without an error handler.

                    As for speed you are right that probably won't save much. I think a last resort is to just not use label names but rename my controls to where I just use their name and not the label name to speed things up.

                    Personally I didn't think 2 seconds was a problem either but users of the database started to complain that updating was slow after I put in the advanced change tracking using label names.

                    Comment

                    • jimatqsi
                      Moderator Top Contributor
                      • Oct 2006
                      • 1293

                      #11
                      Is it really necessary to repeat this process once for every record? Why? Are the labels changing? You should be able to know that by checking one time only, no matter how many records you have to update.

                      For 2 seconds a record times 30 records, for sure they are going to complain about that, and they should.

                      Jim
                      Last edited by jimatqsi; Jun 28 '10, 05:22 PM. Reason: correct typo

                      Comment

                      • jbrumbau
                        New Member
                        • Sep 2007
                        • 52

                        #12
                        Ah I see what you are thinking. That's a good idea, so I make that comparison when the form loads using the Form_Open or a Form_Load event, then I have the array of all controls with labels and all controls without labels, then I simply go through the two arrays in the change tracker as opposed to checking each control in the form.

                        Thanks Jim, I think that should work. Also thanks NeoPa for your thoughts as well.

                        Comment

                        • OldBirdman
                          Contributor
                          • Mar 2007
                          • 675

                          #13
                          Why not assign unique numbers to the controls you track, using the .Tag property. You could have a table of these numbers, with nice descriptive names for reporting. Future changes to the reports become easy.
                          If .Tag = null, blank, or zero-length, then not tracked.

                          Comment

                          • jbrumbau
                            New Member
                            • Sep 2007
                            • 52

                            #14
                            Ah that's a great idea. I've never used the Tag property of a control before, I wasn't previously aware of it. So I guess I could enter 2 for controls I track with labels and 1 for controls I track without labels. Or simply type the name I want to use in .Tag and then I don't have to do anything fancy with labels to begin with.

                            Thanks OldBirdman.

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32669

                              #15
                              Ah. It all starts to make sense.

                              If a control has an associated label then this will be held in a Controls collection within it. There may be other examples of associating controls with other controls (EG. Frames), so it may be worth checking more than just the existence of a Controls collection, but that should be easy enough, depending on your exact context.

                              Comment

                              Working...