Programmatically change the caption of a label

helenmayhew88

New member
Local time
Today, 02:00
Joined
Oct 21, 2008
Messages
5
Hi everyone,
I am trying to create a form that has a label that needs to change through a cycle of different captions when a button is pressed.
The reason why is a bit complicated, so unless you need an explanation I wouldn't ask for one. Basically, I need the same form, with identical functions, 14 times, except with a different caption.

Can I do this without having 14 different forms?

Many thanks

Helen Mayhew
 
You use the caption property.

In a macro it is done with SetValue action. Here is one of my macros converted to code

Forms!PrintAndClose!Label306.Caption = Eval("""Not in Use""")

For the macro SetValue

Item [Forms]![PrintandClose]![Label306].[Caption]
Expression "Not in Use"

You can then have a list of them to produce different captions and the code/macro will run the line based on your conditions. If you want to remove the caption then Expression is " "

I only use stand alone labels but that should work OK on labels that are produced when you drag a field from the field list or create a form from the form wizard.
 
Last edited:
Another technique would be to place a function on the click of the button
which would reset the captions of string variables which are inturn used to reset the caption of a label on a form.

First declare the label caption string variables at modular level


Code:
Public StrLabelCaption1 As String
Public StrLabelCaption2 As String
etc

Then on the OnClick event of a command button

Call CaptionChanger("CmdABC")


Public Function CaptionChanger(ButtonName)

Select Case ButtonName
     Case "Default"
         StrLabelCaption1 = "Add"
         StrLabelCaption2 = "Edit"
         etc.

     Case "CmdABC"
         StrLabelCaption1 = "Add ABC"
         StrLabelCaption2 = "Edit ABC"
         etc.
     Case "CmdXYZ"
         StrLabelCaption1 = "Add XYZ"
         StrLabelCaption2 = "Edit XYZ"
         etc.
    etc
End Select
    Call ResetLabelCaptions()
End Function
Next call another function that will reset the captions for each label


Code:
Public Function ResetLabelCaptions()

    Forms("FormName")("LabelName").Caption = StrLabelCaption1
    Forms("FormName")("LabelName").Caption = StrLabelCaption2
    Forms("FormName")("LabelName").Caption = etc

End Function
Finally
On the On Open event of the form call the CaptionChanger("Default") function when the form opens. You will notice that I have used the word default instead of a button name. This will use the case statement to reset the label captions back to the standard default captions.

CodeMaster::cool:
 
If you ever come back and read this post it would be nice to know which option you went with?
 

Users who are viewing this thread

Back
Top Bottom