Change All Label Captions within a Form (1 Viewer)

Richaradthomas

New member
Local time
Today, 16:31
Joined
Jun 13, 2022
Messages
1
Hi,

I like to update label Captions here within a form.

However I am only able to get the control Name and not the caption of that control.

The reason I like to update all Labels in a Form as I have always something like Cont_FirstName and so on..

I like to get rid of the "Cont_" so the caption is "Firstname"

Code:
Sub Test()
    Dim ctl As Access.Control
    Dim strOld As String
 
    strOld = "Cont_"
    Dim strNew As String
 
    strNew = ""
 
    For Each ctl In Forms!frm_Contacts  ' must be open in design view
        If ctl.ControlType = acLabel Then
            Debug.Print ctl.Name
         
            Dim strLabel As String
            strLabel = ctl.Name
            dislyke.com/instagram-captions/
            Debug.Print me.(ctl.Name).Caption)      'How To get this Property when not knowing the Control Name
         

        End If
    Next ctl
End Sub

Once I am able to receiving the Caption I am able to update that Label so I was hoping someone could help please.

Would be just nice as there are many Forms and Controls so it would be a nice thing to have.

Many thanks
After you change the caption of your label, use the Refresh-method of the
label to get it to repaint itself. Only then it shows its new value.
When you do not refresh the label, it is refresh when your code has
completed. As only then the events are handled by the system. So you could
use the ‘Do Events’ command, but it is much beter to just refrehs the label.

For more data search in MSDN for ‘Refresh’. It also gives you an example.
 

silentwolf

Active member
Local time
Today, 04:31
Joined
Jun 12, 2009
Messages
545
Hi oh ok well I will look into it thanks for your reply!

Cheers
 

CJ_London

Super Moderator
Staff member
Local time
Today, 11:31
Joined
Feb 19, 2013
Messages
16,553
After you change the caption of your label, use the Refresh-method of the
label to get it to repaint itself.
That is not correct. Refresh is to do with the underlying data, not captions. It also happens automatically - typically 60 seconds as the default set in Options>client settings and is really only relevant in a multi user environment as it reflects changes in the current recordset made by other users.

Refresh differs from requery in that it looks at changes to the current records whereas requery will reapply any criteria and may add or remove records accordingly.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:31
Joined
Feb 19, 2002
Messages
42,981
40 years ago when I was writing COBOL, we used to prefix all our variable names by where they came from. Today, in the era of intellisense, that makes no sense and especially with Access, it is just plain annoying. If you feel the need for this identification, use a suffix. It serves the same purpose but doesn't get in the way of anything. It is probably too late for you to change your naming convention but you might consider it. It is just a couple of days of pain to convert everything and test it if you have a find and replace tool.

Refresh differs from requery in that it looks at changes to the current records whereas requery will reapply any criteria and may add or remove records accordingly.

But as a side-effect, BOTH force the current record to save and so you really don't want to use them unless you are OK with saving. Generally this interferes with your validation code because it will be forced to run before data entry is complete and that will raise errors that will confuse the user. Unless of course, you don't bother with validation:(
 

silentwolf

Active member
Local time
Today, 04:31
Joined
Jun 12, 2009
Messages
545
Ok I am changing slowly lol

Getting alot of not that great feetback of using prefixes and so I did in new application get rid of it :)

Cheers
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:31
Joined
Feb 28, 2001
Messages
27,001
Hi Doc_Man,

thanks for your input too.

I am a little lost with what you mean exactly.

Also with CJ_London I do have a little difficulty to understand how to set it up correctly.
As mentioned it is a bit akward how I do it or done it.

Cheers

Sorry I'm a little late in replying.

What I meant was that if you did a "for each ctl in forms.controls" type of loop, you would know which control was associated with a label because the .Parent of a label is the control that owns it, whereas the .Parent of some other control is usually the form itself. I can't be certain, but I think most cases of controls are such that they have either a caption or a value but not both. The option group & option button get a bit complex about this, but most other controls do follow that general rule of having one or the other but not both. Controls also exist that have neither a .Value nor a .Caption - e.g. rectangles or lines. So that "for each" loop would do best to verify the control type of the control it was seeing at the moment anyway.
 

silentwolf

Active member
Local time
Today, 04:31
Joined
Jun 12, 2009
Messages
545
Hi Doc_Man

all good, I sometimes oversee it to and then I reply way to late so no problem at all!

But thanks for taking it on again and explaining it a little better, well explaining it so I understand it :)

Very much appreciated!

Cheers
 

Users who are viewing this thread

Top Bottom