Getting a reference to a control

A

Andrew

I have a function where I want to pass a reference to a control to the
function. The Function header is:
Public Function Search(ctlText As Control, strField As String) As Variant
I then call this function from the On Change event of a textbox from a form
with the following call:
varRetval = Search(Me.txtSearchName, “Nameâ€)
When I run this code the control Me.txtSearchName comes back as NULL and so
the the call to the function works but nothing is passed because
Me.txtSearchName is equal to NULL. I have checked to make sure I have the
right name for the textbox and cannot figure out why I cannot get a reference
to the text box. Any help would be great I am using Access 2007 and the
database is in the 2003 format.
 
A

Allen Browne

You need to refer to the Text property of the control, e.g.:
varRetval = Search(Me.txtSearchName.Text, "Name")

When you type in a control, its Text changes with each character, but its
Value is not updated until later. Value is the default property, so you are
not getting the Text.

This behavior is different from pure VB, where Text is the default property.
In a data-centric program like Access, this makes sense. For example, if you
are entering a date, the control's Text might be 4/5/ (i.e. you haven't
typed the year yet), which could never be the Value, but is the Text.

There's an example of the kind of thing you are trying to do here:
Find as you type - Filter forms with each keystroke
at:
http://allenbrowne.com/AppFindAsUType.html
The code is rather comprehensive: it examines all the controls on the form,
to figure out which ones are bound to fields that can be filtered on, and
what name the user knows those fields by, so it can offer a list of fields
to search in a combo. Therefore you just copy'n'paste onto any form, set one
property, and it just works.

BTW, hopefully "Name" is just an example. If you really have a field by that
name, you may run into problems where Access thinks you mean the Name
property (the name of the form) instead of the contents of the field named
Name. Here's a list of field names it's best to avoid:
http://allenbrowne.com/AppIssueBadWord.html
 
A

Andrew

Hi Allen,

Thanks for the help and yes “Name†is just an example in this case. I am
interested though how would I pass a reference to the control rather than
data that the control is holding?

Thanks
Andrew
 
A

Allen Browne

Your function does receive the control, i.e.:
Public Function Search(ctlText As Control, strField As String) As
Variant

Therefore you can refer to its Text property as:
ctlText.Text

Note that the Text property only applies if the control has focus.
 
G

Guest

What makes you think that nothing is passed?
Me.txtSearchName is equal to NULL.

That doesn't mean that nothing was passed. That means that the
value of the default property of the control is equal to NULL.

Me.txtSearchName.value is equal to NULL.

(The default property of a textbox is called 'value')

The value of the value property of textbox is Null or empty
string if there is nothing in the textbox, or if there used to be
nothing in the textbox and the new stuff in the textbox hasn't
been completed yet.

VB.net avoids this confusion by not having a default property
for controls. In VB.net Me.txtSearchName would ALWAYS
be the control, and would NEVER be Me.txtSearchName.value
C# users appreciate this because confusion of the value and the
pointer was a traditional C problem, and it looms large in their
consciousness. In effect, it means that the default property of a
control is always a reference, which is one step more obscure,
but C programmers never considered that obscurity was a drawback.


(david)
 
A

Andrew

Hi David and Allen,

Thank you for your posts I have gotten myself straight now on the passing of
controls. I was seeing the error in the wrong place. Thanks again for the
help.

Andrew
 

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