How to Change Value of Active Control

S

Sondreli

I'm writing a routine to change the value of any Active Control to null. I
can get the name of the control using ActiveControl.Name but I can't figure
out the syntax to change the value of this control. I've tried assigning it
to variables but variables aren't working in a:

Forms![variable]![variable] = ""

Help
 
D

Dirk Goldgar

Sondreli said:
I'm writing a routine to change the value of any Active Control to null.
I
can get the name of the control using ActiveControl.Name but I can't
figure
out the syntax to change the value of this control. I've tried assigning
it
to variables but variables aren't working in a:

Forms![variable]![variable] = ""


ActiveControl -- whether Screen.ActiveControl or the .ActiveControl property
of a form -- returns a reference to the control itself, so you can just
assign a value to that reference. For example,

Screen.ActiveControl = Null

or (for the active control on the current form):

Me.ActiveControl = Null
 
J

Jeff Boyce

Do you mean "null" the same way Access means "null"? (if so, see Dirk's
reply)

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.
 
S

Sondreli

Dirk,

That doesn't work. here is my code.

Public Function ClearCBOList()
Dim frmCurrentForm As Form
Dim frmCurrentControl As Control
Set frmCurrentForm = Screen.ActiveForm
Set frmCurrentControl = Screen.ActiveControl
Screen.ActiveControl = Null
DoCmd.Requery frmCurrentControl.Name

End Function

It is a public function to clear any Combo box after a selection is made.
The error I get with this code is that "You can't assign a value with this
object" If I use ME it says it is an Invalid use of the Me object.

If I check the value of the variables frmCurrentForm and frmCurrentControl
it is the name that I would expect. I just want to clear the VALUE of the
current control, not the name of the control.

any help would be appreciated.

Sondreli

Dirk Goldgar said:
Sondreli said:
I'm writing a routine to change the value of any Active Control to null.
I
can get the name of the control using ActiveControl.Name but I can't
figure
out the syntax to change the value of this control. I've tried assigning
it
to variables but variables aren't working in a:

Forms![variable]![variable] = ""


ActiveControl -- whether Screen.ActiveControl or the .ActiveControl property
of a form -- returns a reference to the control itself, so you can just
assign a value to that reference. For example,

Screen.ActiveControl = Null

or (for the active control on the current form):

Me.ActiveControl = Null


--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)
 
D

Dirk Goldgar

Sondreli said:
Dirk,

That doesn't work. here is my code.

Public Function ClearCBOList()
Dim frmCurrentForm As Form
Dim frmCurrentControl As Control
Set frmCurrentForm = Screen.ActiveForm
Set frmCurrentControl = Screen.ActiveControl
Screen.ActiveControl = Null
DoCmd.Requery frmCurrentControl.Name

End Function

It is a public function to clear any Combo box after a selection is made.
The error I get with this code is that "You can't assign a value with this
object" If I use ME it says it is an Invalid use of the Me object.

If I check the value of the variables frmCurrentForm and frmCurrentControl
it is the name that I would expect. I just want to clear the VALUE of the
current control, not the name of the control.


It works fine for me, even using your code as posted above, which is more
elaborate than necessary. What version of Access are you using? I tested
with Access 2003.

It may be that you are calling the function under conditions that prevent
updating the current control. How exactly, and in what event, are you
calling the function?
 
S

Sondreli

Dirk,

I'm using 2007 and I call this procedure that is located in a global module
from a private procedure in a form. I had extra code in there just to check
values. When looking at a Help screen it indicated "Read Only"?

I can code it in each form but was looking for common routine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top