By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,608 Members | 1,940 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,608 IT Pros & Developers. It's quick & easy.

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

P: 52
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:
Expand|Select|Wrap|Line Numbers
  1. Function hasLabel(ByRef ctl As Control) As Boolean
  2.     Dim tempstr As String
  3.  
  4. On Error GoTo err_handler
  5.     tempstr = ctl.Controls(0).Name
  6.     hasLabel = True
  7.     Exit Function
  8. err_handler:
  9.     hasLabel = False
  10.     Exit Function
  11. End Function
  12.  
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.
Jun 25 '10 #1

✓ answered by jimatqsi

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
Expand|Select|Wrap|Line Numbers
  1.  if textboxname.tag = labelname.tag then
  2.  do same tag stuff
  3. endif
  4.  
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

Share this Question
Share on Google+
15 Replies


Expert 100+
P: 1,240
This works for what? I thought all controls had a .Name? What is this telling you about a control?

Jim
Jun 26 '10 #2

P: 52
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.
Jun 28 '10 #3

NeoPa
Expert Mod 15k+
P: 31,769
Are you trying to ask how to determine if a specified control is a label?
Jun 28 '10 #4

NeoPa
Expert Mod 15k+
P: 31,769
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.
Jun 28 '10 #5

P: 52
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.
Jun 28 '10 #6

NeoPa
Expert Mod 15k+
P: 31,769
Your function can then become :
Expand|Select|Wrap|Line Numbers
  1. Function hasLabel(ByRef ctl As Control) As Boolean
  2.     hasLabel = (ctl.ControlType = acLabel)
  3. End Function
Though I would rename it to isLabel() myself.
Jun 28 '10 #7

NeoPa
Expert Mod 15k+
P: 31,769
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.
Jun 28 '10 #8

Expert 100+
P: 1,240
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
Expand|Select|Wrap|Line Numbers
  1.  if textboxname.tag = labelname.tag then
  2.  do same tag stuff
  3. endif
  4.  
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
Jun 28 '10 #9

P: 52
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.
Jun 28 '10 #10

Expert 100+
P: 1,240
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
Jun 28 '10 #11

P: 52
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.
Jun 28 '10 #12

100+
P: 675
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.
Jun 28 '10 #13

P: 52
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.
Jun 28 '10 #14

NeoPa
Expert Mod 15k+
P: 31,769
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.
Jun 29 '10 #15

NeoPa
Expert Mod 15k+
P: 31,769
Some controls have a Controls collection, but others don't. If you wish to check the existence or contents of such a collection it is advisable either to check beforehand that the control is of a type that has it available, or to handle the error reported when it doesn't exist. A TextBox is recognised by having a ControlType value of acTextBox.

Look up ControlType Property in context-sensitive help for a fuller list of controls and their ControlTypes.
Jun 29 '10 #16

Post your reply

Sign in to post your reply or Sign up for a free account.