Cannot setFocus

  • Thread starter Thread starter Howard Jacobs
  • Start date Start date
H

Howard Jacobs

Code below attempts to make [txtSelected] (a text box on current form)
visible & then move focus to it.

The final line (<--) gets run time error - "Cannot move focus to ..."

Debug shows that [txtSelected].visible is indeed false even tho first
line (<-.-.) sets it true. Repaint was a failed attempt to remedy
this.

if insert breakpoint at <..... & then continue visible is true & code
works.

Please help.

Private Sub cboAuthors_AfterUpdate()

[txtSelected].Visible = True ' <-.-.
[txtSelectedFirst] = [cboAuthors].Column(0)
[txtSelectedLast] = [cboAuthors].Column(1) ' <.....
[txtSelected] = [txtSelectedFirst] & " " & [txtSelectedLast]
[Label77].Visible = True
Forms!frmBooks.Repaint
Me.Repaint
[txtSelected].SetFocus ' <--

End Sub
 
What are the Enabled and Locked properties of the textbox set at?

Enabled is Yes, Locked is No

The line "txtSelected] = [txtSelectedFirst] & " " & [txtSelectedLast]"
correctly sets the value.

It seems that the line " [txtSelected].Visible = True" is not
executing in time.

I thought that "Repaint" dealt with this. Am I using it correctly?

Howard Jacobs said:
Code below attempts to make [txtSelected] (a text box on current form)
visible & then move focus to it.

The final line (<--) gets run time error - "Cannot move focus to ..."

Debug shows that [txtSelected].visible is indeed false even tho first
line (<-.-.) sets it true. Repaint was a failed attempt to remedy
this.

if insert breakpoint at <..... & then continue visible is true & code
works.

Please help.

Private Sub cboAuthors_AfterUpdate()

[txtSelected].Visible = True ' <-.-.
[txtSelectedFirst] = [cboAuthors].Column(0)
[txtSelectedLast] = [cboAuthors].Column(1) ' <.....
[txtSelected] = [txtSelectedFirst] & " " & [txtSelectedLast]
[Label77].Visible = True
Forms!frmBooks.Repaint
Me.Repaint
[txtSelected].SetFocus ' <--

End Sub
 
The next thing to check is duplicate names. Are the name of the control and
the name of the field both txtSelected? Try Me.txtSelected.Visible = True.

You can set a value to a disabled control using code, but you can't set the
focus to it. A Repaint should not be needed. I belive repainting is used to
redraw the form after it has been covered and you are doing drawing directly
to the form, such as creating circles in code.

--
Wayne Morgan
MS Access MVP


Howard Jacobs said:
What are the Enabled and Locked properties of the textbox set at?

Enabled is Yes, Locked is No

The line "txtSelected] = [txtSelectedFirst] & " " & [txtSelectedLast]"
correctly sets the value.

It seems that the line " [txtSelected].Visible = True" is not
executing in time.

I thought that "Repaint" dealt with this. Am I using it correctly?
 
txtSelected is the name of the unbound control.

I have reduced the sub to:

Private Sub cboAuthors_AfterUpdate()

Me.[txtSelected].Visible = True
Me.[txtSelected].SetFocus

End Sub

This fails as before at SetFocus & debug shows [txtselected].visible
is false at that point !!
But if I remove the setFocus line:
a) [txtselected].visible is true &
b) I can just tab to txtSelected & everything works.
 
Further info.

I replaced the

[txtselected].setFocus

with

[a different control].setFocus

Same failure at that point.
 
Try it without the bracket [ ] and see what happens.

--
Wayne Morgan
Microsoft Access MVP


Howard Jacobs said:
Further info.

I replaced the

[txtselected].setFocus

with

[a different control].setFocus

Same failure at that point.

The next thing to check is duplicate names. Are the name of the control and
the name of the field both txtSelected? Try Me.txtSelected.Visible = True.

You can set a value to a disabled control using code, but you can't set the
focus to it. A Repaint should not be needed. I belive repainting is used to
redraw the form after it has been covered and you are doing drawing directly
to the form, such as creating circles in code.
 
Same result.


Try it without the bracket [ ] and see what happens.
Further info.

I replaced the

[txtselected].setFocus

with

[a different control].setFocus

Same failure at that point.

The next thing to check is duplicate names. Are the name of the control and
the name of the field both txtSelected? Try Me.txtSelected.Visible = True.

You can set a value to a disabled control using code, but you can't set the
focus to it. A Repaint should not be needed. I belive repainting is used to
redraw the form after it has been covered and you are doing drawing directly
to the form, such as creating circles in code.
 
Follow up:

Made all controls visible & removed all code that changes visibility.

Everything works - no errors.

???????
 
Are the controls on the same form as the code? Are they on a tab control?

I'm not surprised that everything worked when you manually set everything to
Visible, the problem appears to be that the control isn't becoming visible
as was indicated by your earlier tests. Have you verified that the code is
being run? Place a message box or a break point in the code to make sure the
routine is being run.
 
<make sure the routine is being run.> - yes it is

<Are the controls on the same form as the code? Are they on a tab
control?> - they are all on the same page of a tabbed form.

I set up a clean simplified test of what is happening:

3 text boxes Test4, Test5 & Test6 on a page of a tabbed form

This works:

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus
End Sub

Private Sub Test5_AfterUpdate()
[Test6].Visible = True
[Test6].SetFocus
End Sub

This does not work & it fails at <------- with message:
"Cannot set focus on control test5":

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus <--------------
End Sub

Private Sub Test5_GotFocus()
[Test6].Visible = True
[Test6].SetFocus
End Sub

Does it work this way on your system?
 
Couldn't help note that in your fifth(?) post your code uses the expression
Me. [your control] - (Me dot).
Using Me. represents the current form object.
Using Me![your control] (with the exclamation mark) represents the control
on the current active form.
So I wonder whether you are qualifying your control correctly.
Also in your code below you do not seem to set [Test5] to visible? Is this a
typo?

There is often confusion with setting focus on form controls, especially
when using OnExit and AfterUpdate calls.
I sometimes cheat by creating a textbox (which I call [txtDummyFocus]),
minimise its size (0,0), making it utterly transparent, setting its Tab Stop
property to No, and tucking it away on the form out of harms way.

When I have SetFocus issues with a control on a form I send the focus off to
[txtDummyFocus] first and then back to the target control. Clunky (some
might say daft!), but it works.

I presume you are working on a multi-tabbed form? As Wayne asks, are you
SURE the controls are all on the same TAB (Page). They can have this
appearance in design view, but! I suggest you start with a brand new
single-page form and test your code there.

FWIW
WSF




Howard Jacobs said:
<make sure the routine is being run.> - yes it is

<Are the controls on the same form as the code? Are they on a tab
control?> - they are all on the same page of a tabbed form.

I set up a clean simplified test of what is happening:

3 text boxes Test4, Test5 & Test6 on a page of a tabbed form

This works:

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus
End Sub

Private Sub Test5_AfterUpdate()
[Test6].Visible = True
[Test6].SetFocus
End Sub

This does not work & it fails at <------- with message:
"Cannot set focus on control test5":

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus <--------------
End Sub

Private Sub Test5_GotFocus()
[Test6].Visible = True
[Test6].SetFocus
End Sub

Does it work this way on your system?

Are the controls on the same form as the code? Are they on a tab control?

I'm not surprised that everything worked when you manually set everything to
Visible, the problem appears to be that the control isn't becoming visible
as was indicated by your earlier tests. Have you verified that the code is
being run? Place a message box or a break point in the code to make sure the
routine is being run.
 
This works:
Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus
End Sub
This does not work & it fails at <------- with message:
"Cannot set focus on control test5":

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus <--------------
End Sub

They look the same to me. It fails one time and works the next? I did some
playing around and even if the control wasn't on the current tab, setting
the focus to it brought the tab that the control was on to the front and set
the focus to the control.

--
Wayne Morgan
Microsoft Access MVP


Howard Jacobs said:
<make sure the routine is being run.> - yes it is

<Are the controls on the same form as the code? Are they on a tab
control?> - they are all on the same page of a tabbed form.

I set up a clean simplified test of what is happening:

3 text boxes Test4, Test5 & Test6 on a page of a tabbed form

This works:

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus
End Sub

Private Sub Test5_AfterUpdate()
[Test6].Visible = True
[Test6].SetFocus
End Sub

This does not work & it fails at <------- with message:
"Cannot set focus on control test5":

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus <--------------
End Sub

Private Sub Test5_GotFocus()
[Test6].Visible = True
[Test6].SetFocus
End Sub

Does it work this way on your system?

Are the controls on the same form as the code? Are they on a tab control?

I'm not surprised that everything worked when you manually set everything to
Visible, the problem appears to be that the control isn't becoming visible
as was indicated by your earlier tests. Have you verified that the code is
being run? Place a message box or a break point in the code to make sure the
routine is being run.
 
Yes they are the same - thedifference is in the event used in Test5.

If I use the GotFocus event in Test5 then the SetFocus in Test4
fails.


This works:

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus
End Sub
This does not work & it fails at <------- with message:
"Cannot set focus on control test5":

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus <--------------
End Sub
They look the same to me.
It fails one time and works the next? I did some
playing around and even if the control wasn't on the current tab, setting
the focus to it brought the tab that the control was on to the front and set
the focus to the control.
 
I have identified my problem. It is not to do with "vidsible" but to
my use of the GotFocus event. Can you explain why?

Three unbound text boxes on simple form.

The following code fails at arrow

Private Sub Test4_AfterUpdate()
[Test5].SetFocus ' <--------------
End Sub

Private Sub Test5_GotFocus()
[Test6].SetFocus
End Sub


This works:

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus
End Sub
This does not work & it fails at <------- with message:
"Cannot set focus on control test5":

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus <--------------
End Sub

They look the same to me. It fails one time and works the next? I did some
playing around and even if the control wasn't on the current tab, setting
the focus to it brought the tab that the control was on to the front and set
the focus to the control.
 
Couldn't help note that in your fifth(?) post

I only started with Access last month. This is my first post on this
group. If I am not following proper protocol, pls inform me.
your code uses the expression Me. [your control] - (Me dot).
Using Me. represents the current form object.
Using Me![your control] (with the exclamation mark) represents the control
on the current active form.
So I wonder whether you are qualifying your control correctly.

I have found this confusing. A lot of the code I see uses the
following apparently indiscriminantly:
ctl
[ctl]
me.ctl
me.[ctl]
me!ctl
me![ctl]

I have tried to use just [ctl] unless I have a problem (which is what
happened here.
Also in your code below you do not seem to set [Test5] to visible? Is this a
typo?

All 3 fields start of as visible & Test5 is not changed.

There is often confusion with setting focus on form controls, especially
when using OnExit and AfterUpdate calls.
I sometimes cheat by creating a textbox (which I call [txtDummyFocus]),
minimise its size (0,0), making it utterly transparent, setting its Tab Stop
property to No, and tucking it away on the form out of harms way.

When I have SetFocus issues with a control on a form I send the focus off to
[txtDummyFocus] first and then back to the target control. Clunky (some
might say daft!), but it works.

Good thought - thank you
I presume you are working on a multi-tabbed form? As Wayne asks, are you
SURE the controls are all on the same TAB (Page). They can have this
appearance in design view, but! I suggest you start with a brand new
single-page form and test your code there.

Three unbound text boxes on simple form.

The following code fails at arrow

Private Sub Test4_AfterUpdate()
[Test5].SetFocus ' <--------------
End Sub

Private Sub Test5_GotFocus()
[Test6].SetFocus
End Sub

It seems to be caused by my use of the GotFocus event.

What is wrong here?
 
Judging by what you've given here, there appears to be a conflict between
the events. You may try the Enter event instead of the GotFocus event on the
control you are going to. The Enter event fires before the GotFocus event
and only fires when you go to the control. If you leave the focus at the
control, go to another form, and return to this form then the Enter event
will only fire once; however, the GotFocus event will fire twice, once when
you moved to the control and once when you went back to the form while the
control was still the active control on the form.

--
Wayne Morgan
Microsoft Access MVP


Howard Jacobs said:
I have identified my problem. It is not to do with "vidsible" but to
my use of the GotFocus event. Can you explain why?

Three unbound text boxes on simple form.

The following code fails at arrow

Private Sub Test4_AfterUpdate()
[Test5].SetFocus ' <--------------
End Sub

Private Sub Test5_GotFocus()
[Test6].SetFocus
End Sub


This works:

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus
End Sub
This does not work & it fails at <------- with message:
"Cannot set focus on control test5":

Private Sub Test4_AfterUpdate()
[Test6].Visible = False
[Test5].SetFocus <--------------
End Sub

They look the same to me. It fails one time and works the next? I did some
playing around and even if the control wasn't on the current tab, setting
the focus to it brought the tab that the control was on to the front and set
the focus to the control.
 
Using Enter instead of GotFocus leads to same error.

What I am trying to do:

Form with instructions to be followed 1 step at a time - works fine.

Trying to add following feature to form: Only first step visible.
When completed, second step visible & completed step not visible, etc.

Tried to do this by:

1. In completed step's AfterUpdate - make next step visible &
SetFocus to it.

2. In the step getting focus, use its GotFocus to make prior step not
visible.

What I am doing instead (which I have no problem with) is to start
with all steps visible & make each completed step disappear.

Thank you for spending your effort & time on this for me.

Judging by what you've given here, there appears to be a conflict between
the events. You may try the Enter event instead of the GotFocus event on the
control you are going to. The Enter event fires before the GotFocus event
and only fires when you go to the control. If you leave the focus at the
control, go to another form, and return to this form then the Enter event
will only fire once; however, the GotFocus event will fire twice, once when
you moved to the control and once when you went back to the form while the
control was still the active control on the form.
I have identified my problem. It is not to do with "vidsible" but to
my use of the GotFocus event. Can you explain why?

Three unbound text boxes on simple form.

The following code fails at arrow

Private Sub Test4_AfterUpdate()
[Test5].SetFocus ' <--------------
End Sub

Private Sub Test5_GotFocus()
[Test6].SetFocus
End Sub
 
Back
Top