Changing Form Control Captions

pallem

Registered User.
Local time
Today, 15:53
Joined
May 10, 2017
Messages
19
Hi, i'm trying to change Form captions using code line:
Forms("oForm.Name")("oCtrl.Name").Caption = "newcaption"
Everytime i try to run the code the program flow just steps into it and then steps out without doing anything?? I have checked oForm.Name and oCtrl.Name and they both contain the correct values. I'm sure i had this working at one point but must have changed something without realising.
Any suggestions much appreciated.
 
Your syntax is a bit off...

Code:
Forms!FormName.ControlName.Caption = "newcaption"

Or, if on the form, use

Code:
Me.ControlName.Caption= "newcaption"
 
Code:
("oForm.Name")("oCtrl.Name")
Looks like you are trying to use variables, but you are using literals

dim myVariableName as string
myVariableName = "ABC"
debug.print "myVariableName"
'that prints the word myVariableName
debug.print myVariableName
'that prints ABC

However there is likely more problems because I doubt you have a form named ofrm.name so it should throw a form not found error instead of doing nothing.
 
Post the procedure code.

What is the condition for changing caption? Could use a textbox with conditional expression as a 'label'.
 
Many thanks for all your help. I've made a schoolboy error and forgotten to take the quotes off so it should read: Forms(oForm.Name)(oCtrl.Name).Caption = "newcaption"
which works fine. Serves my right for coding late at night!
 
Code:
Forms(oForm.Name)(oCtrl.Name).Caption = "newcaption"
This is new syntax for me. Just looks like there should be a dot between the variables. I use the syntax Colin posted as well as Forms("YourFormName").YourControlName.Caption = "Whatever".
 
You can access a conrols properties from a subform as below


For a combobox
Forms("FormName").Controls("ContolName").RowSource = "YourQuery"


A form is different and requires you to access the forms properties
Forms("FormName").Form.Caption = "Caption"
 
Last edited:
This is new syntax for me. Just looks like there should be a dot between the variables.
The world of vba default properties make it really confusing since there can be so many permutations of writing the same thing. Default properties can be dropped from the syntax.
Fully referenced without default properties it would be
Code:
Forms.Item("FormName").Controls.Item("ControlName").Value
Item is default property of all collections so not required
Code:
Forms("FormName").Controls("ControlName").Value
Controls is the default property of a form so not needed
Code:
Forms("FormName")("ControlName").Value
And value is the default property of a control so not needed
Code:
Forms("FormName")("ControlName")

I tend to write more than less, because it is more readable to me.
 
To my mind, the simplest solution should normally be used (as long as it is readable).

So whilst this works:
Code:
Forms("FormName")("ControlName").Caption = "newcaption"
it takes more effort than the 2 versions I gave in post 2:
Code:
Forms!FormName.ControlName.Caption = "newcaption"
Or, if on the form itself:
Code:
Me.ControlName.Caption= "newcaption"
 
Many thanks for all your replies. It gets very confusing with all the different permutations. I have to swap between different development platforms so sometimes end up using the wrong syntax in the wrong code.
Now its working i'll leave it alone.
Many Thanks.
 

Users who are viewing this thread

Back
Top Bottom