Changing Form Control Captions (1 Viewer)

pallem

Registered User.
Local time
Today, 11:00
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.
 

isladogs

MVP / VIP
Local time
Today, 19:00
Joined
Jan 14, 2017
Messages
18,219
Your syntax is a bit off...

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

Or, if on the form, use

Code:
Me.ControlName.Caption= "newcaption"
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 14:00
Joined
May 21, 2018
Messages
8,527
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.
 

June7

AWF VIP
Local time
Today, 10:00
Joined
Mar 9, 2014
Messages
5,470
Post the procedure code.

What is the condition for changing caption? Could use a textbox with conditional expression as a 'label'.
 

pallem

Registered User.
Local time
Today, 11:00
Joined
May 10, 2017
Messages
19
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!
 

moke123

AWF VIP
Local time
Today, 14:00
Joined
Jan 11, 2013
Messages
3,920
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".
 

Dreamweaver

Well-known member
Local time
Today, 19:00
Joined
Nov 28, 2005
Messages
2,466
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:

MajP

You've got your good things, and you've got mine.
Local time
Today, 14:00
Joined
May 21, 2018
Messages
8,527
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.
 

isladogs

MVP / VIP
Local time
Today, 19:00
Joined
Jan 14, 2017
Messages
18,219
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"
 

pallem

Registered User.
Local time
Today, 11:00
Joined
May 10, 2017
Messages
19
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

Top Bottom