Access 2003: Identifying an active control on the form

Y

Yarik

Hi.

I have a control named (for example, text box named "txtDate") on a
form. Let's say that I want to do something conditionally - only when
this control has focus. I expected the following code to work:

Dim frm as Form
Dim ctl as Control
...
Set ctl = frm.txtDate
...
If (frm.ActiveControl Is ctl) Then
...
EndIf

but it does not: even when the given control is active, operator "Is"
returns false.

One workaround is obvious - instead of using operator "Is" to compare
control references, compare names of controls:

If (frm.ActiveControl.Name = ctl.Name) Then
...
EndIf

However, I am really curious why operator "Is" does not work in this
case? Is it a bug or is it by design?

I am also wondering if there are any other workarounds (in particular,
workarounds that would not rely on uniqueness of control names in a
form)?

Any feedback would be greatly appreciated.

Thank you,
Yarik.
 
D

Dirk Goldgar

Yarik said:
Hi.

I have a control named (for example, text box named "txtDate") on a
form. Let's say that I want to do something conditionally - only when
this control has focus. I expected the following code to work:

Dim frm as Form
Dim ctl as Control
...
Set ctl = frm.txtDate
...
If (frm.ActiveControl Is ctl) Then
...
EndIf

but it does not: even when the given control is active, operator "Is"
returns false.

I just tested that code on a form of my own, and the test evaluated as true,
not false. Is it possible that, when you tested it, the form's active
control wasn't what you thought?
 
Y

Yarik

I just tested that code on a form of my own, and the test evaluated as true,
not false.  Is it possible that, when you tested it, the form's active
control wasn't what you thought?

Hmm... I checked a few times, and I was pretty sure that it was the
right control.

Now that you confirm that "Is" is working in your case... I'll
definitely repeat my experiments (and post the results). I really,
really want "Is" to work! :))
 

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