Solved

MS Access Forms WebBrowser Control Focus/Unfocus Problem

Posted on 2013-06-22
7
1,265 Views
Last Modified: 2014-03-26
The situation is this: I have a simple MS Access (2010/13) Form (frm_AttachInstrType) that has as one of its controls a WebBrowser (WebBrowser4).  The Webrowser control's source is a string containing variables that determine which PDF file in a specific directory is displayed when the form moves through the records, i.e. moving to the next record on the form will cause a different PDF file to be displayed in the WebBrowser Control. (This part all works just fine, BTW.)

My problem is this: When all elements of the parent form (frm_AttachInstrType) have finished loading (including the WebBrowser control) I want to set focus on a Text Box (txt_SelectInstrType) on the Parent Form to receive data input from the user....BUT....focus always ultimately lands on the WebBrowser control.  

I have tried placing a Me.txt_SelectInstrType.SetFocus statement in about 10 different places/events (on both Parent Form and WebBrowser Control) and I still can't get the focus to the txt_SelectInstrType after the form loads.  This doesn't sound like much, but given my particular application it will save literally thousands and thousands of keystrokes.

Thanks in advance for any help.
0
Comment
Question by:dgheck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 58
ID: 39267962
First thought; is the tab order correct?

Probably not that simple of a problem though<g>

Little more detail or a sample form would be good.

With the tab order correct, when you move to a new record, what happens?  ie. does the focus move to the first control in the tab sequence and then jump to the web control?   If you click on a control while the web control loads, does it jump back?

 One thing you can do to help to see what is happening when is place a:

 Debug.Print "just arrvied in the xxx control"

 in the GotFocus event of each of the controls.

 Also place in the OnCurrent, BeforeUpdate, AfterUpdate, etc events.

 Try the form out and then look at the debug window to see where you've been.

  Cumbersome I know, but it often surprise you about what fires when and often leads to a solution.

Jim.
0
 

Author Comment

by:dgheck
ID: 39268144
So.....

Set cbo_SelectInstrType as the only control on the entire form that is even a Tab Stop.

Placed Debug.Print statements on the GotFocus Event for both controls and open the form and the Debug.Print returned:

cbo_SelectInstr has focus
WebBrowser Control has focus
cbo_SelectInstr has focus

So it looks like the cbo box has the focus. Is there something additional I might have to do to actually insert the cursor into the cbo box's entry space even though it might technically have the focus? I vaguely remember having to do that on a control of some sort a long while back in another app.
0
 
LVL 58
ID: 39268164
<<So it looks like the cbo box has the focus. Is there something additional I might have to do to actually insert the cursor into the cbo box's entry space even though it might technically have the focus?>>

  No.   If it has the focus, the cursor should be there.  Tag the lostfocus event as well.

  The focus may be moving to a control which you haven't done a GotFocus for.

Jim.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 58
ID: 39268169
<<  The focus may be moving to a control which you haven't done a GotFocus for.>>

 or simpler, when it all settles down, do a:

 ? screen.activecontrol in the debug window.

Jim.
0
 

Author Comment

by:dgheck
ID: 39268253
Right now I have these three bits of code to help me debug.

1) Signals when the Combo Box receives focus

Private Sub cbo_SelectInstrType_GotFocus()
Debug.Print "cbo_SelectInstr has focus"
End Sub

----------------------------------

2) Signals when WebBrowser's Document Complete Begins, THEN sets focus to the Combo Box, THEN Displays the Active Control, THEN signals the End of Document Complete

Private Sub WebBrowser22_DocumentComplete(ByVal pDisp As Object, URL As Variant)

Debug.Print "Begin WebBrowser DocumentComplete Event"
Debug.Print "Begin SetFocus Attempt"
Me.cbo_SelectInstrType.SetFocus
Debug.Print "After SetFocus Attempt"
Debug.Print "Screen.ActiveControl is now...."
Debug.Print Screen.ActiveControl
Debug.Print "End WebBrowser DocumentComplete Event"

End Sub

---------------------------------------

3) Signals when WebBrowser Control receives focus

Private Sub WebBrowser22_GotFocus()
Debug.Print "WebBrowser Control has focus"
End Sub


4) There are no other attempts to set the focus to the ComboBox in any other events on any other controls.

----------------------------

RESULTS FROM IMMEDIATE WINDOW UPON RUNNING FORM/MOVING TO NEXT RECORD A SINGLE TIME:
(I numbered the lines)


1.cbo_SelectInstrType has focus    
2.Begin WebBrowser DocumentComplete Event
3.Begin SetFocus Attempt
4.After SetFocus Attempt
5.Screen.ActiveControl is now....
6.Null
7.End WebBrowser DocumentComplete Event
8.WebBrowser Control has focus
9.cbo_SelectInstrType has focus
10.Begin WebBrowser DocumentComplete Event
11.Begin SetFocus Attempt
12.After SetFocus Attempt
13.Screen.ActiveControl is now....
14.Null
15.End WebBrowser DocumentComplete Event

COMMENTS:
Line 1: I assume this is because the cbobox is the only control that is a Tab Stop on the form upon opening or moving between records.
Line 6: Was hoping for cbo_SelectInstrType...but shows NULL
Line 7: Expected End of Debug signals
Line 8: The Mystery. Why does it repeat?

I am certain that the WebBrowser has the focus after the form completely loads because that control is (properly) showing the top half of the first page of the correct PDF file that I want displayed....and when I hit the SPACE BAR once after the form has loaded the PDF scrolls down to the bottom half of the page.
0
 
LVL 21
ID: 39268648
I find it best to select the form using before moving to a control:

If the code is in the sub form try useing:

DoCmd.SelectObject acForm, Me.Parent.Name

Open in new window



Also have you tried Docmd.GotoControl ?
0
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 39271485
Change WebBrowser22_DocumentComplete() To:

Private Sub WebBrowser22_DocumentComplete(ByVal pDisp As Object, URL As Variant)

On Error Goto ErrorHandler

Debug.Print "Begin WebBrowser DocumentComplete Event"
Debug.Print "Begin SetFocus Attempt"
Me.cbo_SelectInstrType.SetFocus
Debug.Print "After SetFocus Attempt"
Debug.Print "Screen.ActiveControl is now...."
Debug.Print Screen.ActiveControl
Debug.Print "End WebBrowser DocumentComplete Event"

LeaveSub:
  Exit Sub

ErrorHandler:
   Msgbox Err.Number & " " & Err.Description
   Resume LeaveSub

End Sub

As for why it execues twice, I believe it's because of the way the browser control processes pages.   A web page is often made up of many "documents"; it's not what your thinking of where you would get one complete and a fully rendered page.

I've only worked with the web browser control once and it was many years ago, but if I'm remembering correctly, the number of completes you see will depend on the page your going to.

Jim.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question