control handle coding question

Y

Yair Sageev

Given an array of variants

Dim myArr()

and a textbox control containing a Date/Time value called "ctrl", passed as
an arg to the function.

The following code stores the *value* of the control, not the control (or
handle to the control)

myArr(0) = ctrl

this array is passed to OpenForm in the OpenArgs parameter.

if you look at ctrl in the locals window, its type is Control/Textbox

However, myArr(0) lists the type of the item as Variant/Date.

I would like to maintain and pass the *handle* to the control itself so that
I don't have to iterate through the forms to find the control based on its
name at a later time.

Is there any way to do this?
 
D

Dirk Goldgar

Yair Sageev said:
Given an array of variants

Dim myArr()

and a textbox control containing a Date/Time value called "ctrl",
passed as an arg to the function.

The following code stores the *value* of the control, not the control
(or handle to the control)

myArr(0) = ctrl

this array is passed to OpenForm in the OpenArgs parameter.

if you look at ctrl in the locals window, its type is Control/Textbox

However, myArr(0) lists the type of the item as Variant/Date.

I would like to maintain and pass the *handle* to the control itself
so that I don't have to iterate through the forms to find the control
based on its name at a later time.

Is there any way to do this?

To set myArr(0) to a control reference, instead of this:
myArr(0) = ctrl

write this:

Set myArr(0) = ctrl

However, I don't see how you're going to be able to pass this to your
form via OpenArgs, since the OpenArgs parameter is a String. I could
see passing a fully qualified reference ("Forms!FormName!ControlName")
in the form of a string, and then gettiing the value of the control by
applying the Eval function to that string:

vCtlValue = Eval(Me.OpenArgs)

and I could see passing the form name and control name in a delimited
string, then parsing them out on the other end:

*Opening code:

DoCmd.OpenForm "NewForm", OpenArgs:="OldForm!CtlName"

*Code in NewForm:

Dim strArgs() As String
Dim ctl As Control

strArgs = Split(Me.OpenArgs, "!")

Set ctl = Forms(strArgs(0)).Controls(strArgs(1))

Does that help?
 
D

Dirk Goldgar

Yair Sageev said:
(VB newbie question) what is the difference
between "!" and "."? Forms!formName vs. Forms.formName?

Here's my own standard answer to that question:

----- "Bang" vs. "Dot" Notation -----
It's not so much a question of one or the other being "proper syntax",
but that they mean different things that nevertheless almost always give
the same result. As I understand it, the bang (!) notation specifically
denotes that what follows is a member of a collection; in this case, a
member of the form object's default collection, the Controls collection.
The dot (.) notation denotes that what follows is a property or method
of the preceding object. That would logically make the bang notation
"proper" and the dot notation improper.

But wait. Wherever possible, Access makes the controls on a form and
the fields in its recordsource all available as properties of the form.
It also makes the fields of the recordsource available via the bang
notation. I'm not sure exactly how it does this; maybe if a name is
not found in the Controls collection it checks the Fields collection of
the form's recordset as a fallback position. So for most practical
purposes Me!ControlName and Me.ControlName evaluate to the same thing,
and the timing tests I've seen suggest that there is little to choose
between them as far as execution efficiency is concerned. I seem to
recall that there is a very slight difference, but I can't remember
which way the advantage lies, and it's not much. There's a coding-time
advantage, however, to using the dot notation, as it makes the
"intellisense" dropdown lists available. That's a strong argument for
using the dot notation, in my book.

But wait again! I said above that Access makes the controls available
as properties "wherever possible". There are cases where it can't do
that. Specifically, it can't do it when there is already a property of
the same name as the control in question. For example, if your form
"Form1" has a control or a field foolishly named "Name", currently
displaying the value "John Doe", then executing this statement in the
form's code module:

Debug.Print Me!Name, Me.Name

will print

John Doe Form1

in the Immediate Window. So you must be careful not to use any reserved
words or built-in properties as names for your controls, if you want to
use the dot notation to refer to them. But then, you should avoid doing
that anyway, as it tends in general to confuse poor Access.
 

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