Setting the ControlSource property

arm1

Registered User.
Local time
, 22:39
Joined
Nov 21, 2006
Messages
40
I was hoping someone might be able to tell me how I can dynamically code in the modification to a fields control source.

I have a (numeric) field in my report. When the report opens I have (VB Code) an if statement that checks if something is true or false. If it is true, I want it to set the Control Source =0, and if not, set the Control Source =some other value.

It seems I am unable to figure out how the synax works for this, as it keeps giving me the message

"Object doesn't support this method or property"

but if i go into the objects properties... sure enough the second property is the control source.

Ant enlightenment would be much appreciated

(I already figured out how to do this in forms, for the RowSource property)
 
Okay, now would be a good time for an explanation of the controlsource.

The controlsource for a control is meant for you to either bind it to an existing field within a table or query, or to set a formula or expression to display when a record is current on the form.

If you are using a bound control (wanting it to write to the table by use of the form's recordset) then you can't assign a controlsource other than the field that you are binding it to. If you are not binding the control (unbound control) then you can set the controlsource to be a formula or expression at design time, or you can just set the value of the control at run time.

For what you are wanting to do, you just need to set the value of the control to 0 or your other value. You can use the Form's On Current event to do the true/false check and then assign the value.
 
The object is unbound.

I have tried to simply associate a value to it... but I get a msg

'You can't assign a value to this object'


One thing though.

If I were to manually put =0 as the value in the control source property (i realize this would bind the object / field), then the report field would display 0. Similarly, if I put =1 in, the report field would display 1. I have manually tried this.. and for this purpose it seems to work, I just can't figure out how to code this in VB... field1.controlsource = .... doesn't seem to work.

What is the best method of coding in an assigned value for the field?


lets say my field name is field1

what would the line of code be?:

line1.value = x

line1.controlsource = "=1"

...

Thank you for your continual help.

-arm1
 
Check out the Access VBA help file and search for Controlsource.

Here's an excerpt:
Example
The following example sets the ControlSource property for a text box named AddressPart to a field named City:

Forms!Customers!AddressPart.ControlSource = "City"

The next example sets the ControlSource property for a text box named Expected to the expression =Date() + 7.

Me!Expected.ControlSource = "=Date() + 7"
 
Thank you... much appreciated...

for some reason... going through me!fieldname.controlsource seems to have worked where directly (without the 'me!') seems to have failed?

much obliged! -arm1
 

Users who are viewing this thread

Back
Top Bottom