ActiveControl is null?!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Further to my attempts to expand "t" into "000", I've been experimenting with
the OnChange property to call a macro.

As I suspected, it makes typing slow. Maybe too slow, I haven't decided yet.
Unexpectedly, it also seems to not fire in a wide variety of circumstances.

In order to make the code as generic as possible, I made a macro that calls
a public function in a module -- I've used this successfully in the past for
similar problems. In order to figure out which control is being _Changed, I
used...

Set currentControl = Screen.ActiveControl

After some playing about, I have found that this is returning null! I don't
see how this is possible, considering it's that control that fired the macro.

Any ideas?

Maury
 
Maury Markowitz said:
Further to my attempts to expand "t" into "000", I've been
experimenting with the OnChange property to call a macro.

As I suspected, it makes typing slow. Maybe too slow, I haven't
decided yet. Unexpectedly, it also seems to not fire in a wide
variety of circumstances.

In order to make the code as generic as possible, I made a macro that
calls a public function in a module -- I've used this successfully in
the past for similar problems. In order to figure out which control
is being _Changed, I used...

Set currentControl = Screen.ActiveControl

After some playing about, I have found that this is returning null! I
don't see how this is possible, considering it's that control that
fired the macro.

Any ideas?

Have you tried inserting

Debug.Print currentControl.Name

after that line? My guess is that currentControl is being set to the
control object correctly, but the Value of that control object is Null.
Bear in mind that, if the user is typing in a text box, the Change event
fires repeatedly, as the displayed text is changed, but the Value
property of the control isn't updated until the focus leaves the control
or some other action is taken to make Access commit the user's changes.

In other words, if the user types "xxx" (without the quotes) in a text
box that was formerly Null, the Change event will fire three times, but
the value of that text box will remain Null. If the user then presses
Enter or Tab, the control's BeforeUpdate event will fire, followed (if
the event isn't cancelled) by its AfterUpdate event. In *those* events,
the control's Value property will already have been updated to "xxx".
 
:

That was definitely the problem, but I'm not sure what to do now. I can
ASSUME that the event will always be fired with a "proper" ActiveControl, but
in the past that has always ended poorly. Is there some test that I can use
to make sure the control is not null, one that actually tests the control
itself and not .Value? I suppose I could test .Name, but won't that return an
error if the object itself is null? Or is there some way I could call the set
and refer to the control itself, without VB ASSUMING I mean the value inside?

Maury
 
Maury Markowitz said:
:

That was definitely the problem, but I'm not sure what to do now. I
can ASSUME that the event will always be fired with a "proper"
ActiveControl, but in the past that has always ended poorly. Is there
some test that I can use to make sure the control is not null, one
that actually tests the control itself and not .Value? I suppose I
could test .Name, but won't that return an error if the object itself
is null? Or is there some way I could call the set and refer to the
control itself, without VB ASSUMING I mean the value inside?

If you refer to Screen.ActiveControl at a time when there is no active
control, an error will always be raised. So temporarily switch to
inline error-handling and test for that:

Dim lngError As Long

On Error Resume Next
Set currentControl = Screen.ActiveControl
lngError = Err.Number
On Error GoTo YourErrorHandler ' restore normal handling

' Was the an active control?
If lngError <> 0 Then
' do something about the problem
Else

' Go ahead and work with currentControl

End If
 
Is there some test that I can use to make sure the control is not null,

When talking about an object Null doesn't apply. Null applies to values.

"If currentControl Is Nothing Then"
is how to test for the existence of an object.

BTW: I'm not sure where you are seeing "Null" returned, but its probably
from the object's default property. If so, then Null is indirectly
confirming that the object exists. If the object didn't exist you'd probably
be seeing "object not set" or something similar.
 
Back
Top