Find The Caption of a Label Attached to a Textbox

Getting the Caption of a Textbox Label

Getting the Caption of a Textbox Label

Caption of a Label Attached to a Textbox

You might think getting the caption of Textbox Label would be just as simple as getting the caption of a Command Button. However it’s a little bit more involved. I run through the process in this short video… This is the VBA code to get the caption of the textbox….

“Me.ControlName.Controls(0).Caption”

Finding the caption on a textbox is not quite a straightforward as you might think. This is because the caption is actually contained in the label attached to the textbox. This also causes an issue if you don’t have a label for your text box because if you have deleted the label, then the code to extract the caption will not run it creates an error

Video 1 (4:23)

Video 1 Index

00:10 linked to earlier video – See Video 4 Here:- Lock, Unlock Controls…
00:13 two buttons to perform one function not good!
00:22 command button locks the form so that you cannot type in the text boxes
00:25 the other button unlocks so you can type text in the text field on the form
00:42 you can change two buttons into a single button
00:55 and this is a look at the VBA code for Locking/Unlocking
00:57 the code checks the caption property of the command button, looking to see if the caption contains "Lock" or "Unlock" and then processing the code depending
01:14 however a text-box is different from a command button in that it doesn't have a caption property
01:22 a textbox has a label and the label gives it the text box a caption
01:33 this is the VBA code for capturing a command buttons caption
01:50 however if you try and extract the caption from a textbox in the same way as you do from a command button you generate an error "Method or data member not found (Error 461)"
02:00 this is because a textbox does not have a caption, the label attached to the textbox contains the caption
02:16 demo of the code for getting the caption from a textbox label
02:36 the VBA code to extract the Caption from the textbox label is:- Me.txtGetMyCaption.Controls(0).Caption
02:48 you are effectively looking through the collection of controls related to the text-box and as far as I'm aware there's only one control!
03:30 you can also get the label name in the same manner just by altering the code slightly Me.txtGetMyCaption.Controls(0).Name
03:38 notice how when you type in "name" in lower case MS Access automatically makes the first letter uppercase. "Name" This is a good indication that "name" is a valid property and the will most likely work
03:50 it's always a good idea to test for conditions that might be prevalent in the future, like a missing label. When you delete the label the code fails reporting error:- Run-time error 2467 – The expression you entered refers to an object that is closed or doesn't exist

… …

More Useful Stuff HERE:-

Attach a Label

Attach a Label

Attach a missing label – Nifty Access – The problem you are facing is, you’ve got a Control and it’s missing its Label. To duplicate the situation, first create a Textbox on an MS Access Form. Created a text box with a label. When you move the text box, the Label moves with it.

Attach a Missing Label

You Deleted the Label - And Now Want it Back!

Video 1 (0:52)

However for some reason you decide you no longer need the label and you delete it. A few hours later you realise you made a mistake and think to yourself “Damn! I wish I hadn’t done that” You try and fix the problem by creating a new label, however The first problem is the label won’t stay there! It vanishes! You can solve that problem by entering the label and pressing a dot, or any text really. You’ve added a caption in the label and it will stay. But although the New Label is adjacent to your text box, it won’t move with the Textbox, it’s not fixed, not attached to it. What you do is you click on the label do:- “Cut” then click on the text box and do “Paste”. Now your label is reattached to your text box…..

Video 1 (0:52)

Nifty Access YouTube Thumb Nail

See the video Above:- Video 1 (0:52) for a demonstration of the problem and how to fix it.

More Label Posts

Vertical Text in a Label

Explanation of how to display text vertically in a label and textbox here:-

Vertical Text in Label and TextBox

Find Caption of Label

Getting the caption of a text box label is not straightforward, I explain it here

Find The Caption of a Label Attached to a Textbox

… …

More Useful Stuff HERE:-

Vertical Text in Label and TextBox

Vertical Text

If you’re stuck for real estate, (a crowded jumbled Form) a particularly busy Form then one way of getting a bit more room might be to have a label oriented vertically. An alternative would be to take stuff off your Form and put it on a pop-up Form. You can see my blog here on a Class Module which adds some interesting functionality when you pop open a Form, it’s a very good solution. Another solution is to put a Tab control on your Form so that you have different sets of controls on different Tabs.

Vertical Text

Video 1  (1:11)

Open the property sheet for the Text Box and/or the Label. You can do this, both in a Form and a Report. You can also multi-select Controls as shown in the demo. In the property sheet select the other tab and then click on the “Vertical” listing and this will change it from Yes to No, and vice versa.

If you want something a bit special you could try one of the activeX controls. I’m not sure if this one from Lebans  works with the latest versions of MS Access, but you could give it a try. As a general principle, is not considered good practice to use activeX Controls because they can fail when Microsoft updates MS Access…

Video 1  (1:11)

Nifty Access YouTube Thumb Nail

More Useful Stuff HERE:-

This website uses third-party software - WordPress Add-Ins to be exact. I don't know what any individual add-in does, but I'm sure that many of them collect information about you. So be aware, if you continue using this site, then you are likely to be sharing your information. I don't know how to disable this sharing for any, or all of the plugins for individual users. So I can't stop the sharing of information. If this worries you then please do not use this site... If you continue to use this site I will assume that you are happy with it.

Do you need a hand in the right direction?

You are in the right place.