Changing a control value with VB code

  • Thread starter Thread starter Fred
  • Start date Start date
F

Fred

I'm trying to change several controls to 0 if a certain condition is met
using VB code, but I keep getting an error stating that I can't change those
values. Is there an easy to do this?

my current code looks something like this
ie. If x=0 then
a=0
b=0
end if

TIA
 
Hi,

if your control is called say text1 then the proper way to set the values is
me.text1.value = 0 or text1.value = 0 also use the option explicit at the
top of all modules and code behind forms etc. this makes sure that your code
and references are declared properly.
 
I tried your suggestions and it isn't working.
The way I have it set up, there are three fields that if one of them is null
or zero, I want all of them to be set to 0 when you exit the form. So my
event occurs upon clickin the "close form" button.

The error I'm getting is a run-time error "you can't assign a value to this
object".

Thanks again.
 
Hi Fred,

couple of questions

are these fields or textboxes ?

if they are fields then we are talking at a database level, if they are
textboxes are they bound to a recordset on the form?

if they are textboxes then maybe setting the default value in the properties
= 0 might sort the problem.

I have just tested setting values in the form close event for a textbox and
it worked without fault, so I need a bit more information, maybe post the
code within the form close event so I can see what is going on.
 
so you need something like this ...

for bound forms/controls

me.text1.value = 0

docmd.runcommand accmdsaverecord

or

docmd.close acform, me.name, acsaveyes

for database fields that are not bound

DAO for dao, you need to run the edit method first

daoTable.Edit
daoTable!Field1.Value = 0
daoTable.update

for ado

adoTable.fields("Field1").value = 0
adoTable.update


also trying something like

if isnull(adoTable.fields("Field1").value) then
adoTable.fields("Field1").value = 0
end if

have you thought about setting default field values at a table level?

if none of the above helps post up some code and I will look at it for you.
 
Alex,

I'm gonna try to work around this one for now. I'm running out of time.
Really appreciate all your help and I'll give these last suggestions a try
when I get a chance.

Best Regards,
Fred
 
if you have the controls actually placed on the screen, then you can use:


me.MyContorl = 0

If the fields are part of the underlying reocrdset, and NOT placed on the
screen, (or you change the recorsouce via code), then you can use:


me!MyFieldName = 0

You do NOT have to create a reocrdset, or use the recordset clone, or use
the reocrdset.Edit method.
 
Back
Top