Solved

MS Access Forms WebBrowser Control Focus/Unfocus Problem

Posted on 2013-06-22
7
1,145 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
  • 4
  • 2
7 Comments
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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
Comment Utility
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 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
<<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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
<<  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
Comment Utility
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
Comment Utility
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 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now