tabbing does not seem to work

G

Guest

I have an interactive form that gathers information from the user. The
questions change depending on the previous answers, so upcoming questions are
hidden until needed.

My problem is the following: on the 'lostfocus' event of each control I have
some code that determines which question should be next depending on the
value of previous controls, and makes it visible. The lost focus event is
typically something like the following:

Private Sub Question019TextBox_lostfocus()

If Me.Question019TextBox.value > 0 Then
Me.Question020Label.Visible = True
Me.Question020TextBox.Visible = True
Else
Me.Question021Label.Visible = True
Me.Question021ComboBox.Visible = True
End If
end sub

For some reason, however, Access does not set the focus to the next control
(controls for questions 20 or 21, in this case), but instead goes back to the
first visible control on the form. All the tab stop indices are correctly set
and the tab stop property is set to true, and still the control does not get
the focus. If I force it with a setfocus command after the '.visible = true'
statement everything works well, but the problem is that I am then unable to
go back to a previous control by using shift-tab (because when the current
control loses the focus due to the shift-tab, the lost focus code is called
and the setfocus command is executed). I guess I could make it work by
putting code in each control's keyup event to determine if tab or shift-tab
were pressed, and then using setfocus accordingly, but this seems "messy" and
complicated. So, is there a way to avoid having to use setfocus in teh code
above, and still have Access set the focus properyl? Why is Access not
setting the focus correctly in the code above?

I tried repainting the form after making the next control visible but it did
not work.

any thoughts or suggestions?

Thanks in advance,

William
 
D

Douglas J Steele

See whether explicitly setting focus works:

If Me.Question019TextBox.value > 0 Then
Me.Question020Label.Visible = True
Me.Question020TextBox.Visible = True
Me.Question020TextBox.SetFocus
Else
Me.Question021Label.Visible = True
Me.Question021ComboBox.Visible = True
Me.Question021ComboBox.SetFocus
End If
 
D

David C. Holley

Suggestion - you may want to investigate the use of page breaks placing
1 question per page and then jumping to that particular page. All of the
Access Wizards use page breaks on form to gather the particular
information that they're looking for.
 
G

Guest

If I explicitly use setfocus then yes, the focus goes to the appropriate
control. This, however, has a big drawback: I am then unable to use
<shift-tab> to move backwards to previous controls. If I hit <shift-tab> the
current control's lostfocus event is called, and the setfocus statement moves
the focus to the next control, rather than the previous one. So, using
setfocus solves one problem but creates another.

I am trying to avoid having to use code in each control's keydown event to
detect if either <enter>, <tab> or <shift-tab> were pressed, as that seems
messy. Since explicit use of setfocus does work as stated above, do you have
any thoughts as to why the focus does not move to the correct control without
the setfocus statement?
 
G

Guest

Thanks for the suggestion. In my case, however, the user may need to refer to
previous answers as he goes through the form, so I need to keep several
controls visible at the same time. The script has lots of questions, and I
don't want users to have to tab backwards thorugh 20 questions to find a
previous answer that I could have placed on the screen for them.

If I cannot find a way to have the focus move correctly I may have to just
use code in each control's keydown event to detect whether <enter>, <tab> or
<shift-tab> were pressed, and then set the focus accordingly. That would
certainly work, but it seems "brute force" and somewhat messy.

Any other thoughts?

TIA,

William
 
D

Douglas J Steele

I suspect that since you can't tab to an invisible control, if you're
starting out with your controls not being visible, they won't have a tab
order.

You could try playing with setting the TabOrder property.

For instance, try

If Me.Question019TextBox.value > 0 Then
Me.Question020Label.Visible = True
Me.Question020TextBox.Visible = True
Me.Question020TextBox.TabOrder = Screen.ActiveControl.TabOrder + 1
Else
Me.Question021Label.Visible = True
Me.Question021ComboBox.Visible = True
Me.Question021ComboBox.TabOrder = Screen.ActiveControl.TabOrder + 1
End If

You might have to write a routine that determines all of the visible
controls on the form, and sets the tab order properties accordingly, then
call that routine.
 
G

Guest

Douglas,

I tried your suggestion in different ways, and it does not seem to have any
effect. I think the problem may lie in the fact that when the lostfocus event
is triggered, the current control has, in essence, already lost its focus and
Access has already "decided" which control is going to be next. When this
happens the new control is not yet visible (because the lostfocus event code
has not yet executed), so Access loops back to the first control on the
form. I tried moving the code to the exit event, with no results.

Since this seems to be some type of quirk, trying to guess at this point
what the exact cause of the problem is might not be worth it. I don't want to
waste anyone's time! While I was trying to avoid it, at this point the
easiest thing to do seems to be to add code to each control's keydown event
to see if tab, shift-tab or enter have been pressed, and set the focus
accordingly. I will continue to play around with this, and if I figure out
what the problem was I will post it.

Thanks for your help!

William
 
J

John Spencer (MVP)

A kludge that may work for you.

Add another very small, visible control to your form after the control you want
to tab out of. This small but visible (1 by 1 pixel) text control is set to be
the next control in the tab order. (You can also use a button set to
transparent if you want something slightly larger) When the new control gets
the focus, you can decide to make your other control visible or not visible and
then move the focus to the desired control using set focus.

You are in Control A (if the answer is yes, then you want to go to control c,
other wise go to control d)
Press tab, focus goes to control B (our very small control)
In the got focus event of B, Check A's value. If it is Yes, then make control C
visible and set the focus to control c, else set the focus to control d.
 

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