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….
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