Set Focus after Navigate

  • Thread starter Zadok @ Port of Seattle
  • Start date
Z

Zadok @ Port of Seattle

I have a form (VIEWER) with a Web Browser window that shows me a pdf file
when I either click a button or hit <enter> to go to the next pdf record.
Once a pdf loads in the browser, I cannot for the life of me get the focus
back onto my form. I've done this code which flashes the control focus where
I want it (Sheet_No), but then the pdf loads and takes focus with it.

Private Sub Form_Current()
Forms!VIEWER.Browser.Navigate Me.Hyperlink
Sheet_No.SetFocus
End Sub

Thanks!
Zadok
 
D

Dirk Goldgar

"Zadok @ Port of Seattle"
I have a form (VIEWER) with a Web Browser window that shows me a pdf
file when I either click a button or hit <enter> to go to the next
pdf record. Once a pdf loads in the browser, I cannot for the life of
me get the focus back onto my form. I've done this code which flashes
the control focus where I want it (Sheet_No), but then the pdf loads
and takes focus with it.

Private Sub Form_Current()
Forms!VIEWER.Browser.Navigate Me.Hyperlink
Sheet_No.SetFocus
End Sub

Thanks!
Zadok

Try waiting in a DoEvents loop while the browser control is busy:

With Forms!VIEWER.Browser
.Navigate Me.Hyperlink
Do While .Busy
DoEvents
Loop
End With

Me.Sheet_No.SetFocus
 
Z

Zadok @ Port of Seattle

Dirk,
I put this in my 'click' event on my form to navigate the subform (VIEWER),
and it acts the same. Am I missing something.

Thanks for the help.
Zadok
 
D

Dirk Goldgar

"Zadok @ Port of Seattle"
Dirk,
I put this in my 'click' event on my form to navigate the subform
(VIEWER), and it acts the same. Am I missing something.

What is your exact code now? And what exactly is the
form/subform/control structure? You didn't say anything about a subform
before.
 
Z

Zadok @ Port of Seattle

Dirk,
I have a form PROJECTS, with DRAWINGS Subform containing 'Hyperlink' field
which contains the link to the PDF - opening in form VIEWER.

Many many thanks!

Private Sub View_Click()
If Not Nz(Me.Hyperlink, "") = "" Then
If Len(Dir([Hyperlink])) > 0 Then
If CurrentProject.AllForms("VIEWER").IsLoaded Then
Forms!VIEWER.Browser.Navigate Me.Hyperlink
Forms!VIEWER.Caption = Hyperlink
Else
DoCmd.OpenForm "VIEWER"
Forms!VIEWER.Browser.Navigate Me.Hyperlink
Forms!VIEWER.Caption = Hyperlink
End If

Else
'File Does Not Exist
End If
End If

End Sub
 
D

Dirk Goldgar

"Zadok @ Port of Seattle"
Dirk,
I have a form PROJECTS, with DRAWINGS Subform containing 'Hyperlink'
field which contains the link to the PDF - opening in form VIEWER.

Many many thanks!

Private Sub View_Click()
If Not Nz(Me.Hyperlink, "") = "" Then
If Len(Dir([Hyperlink])) > 0 Then
If CurrentProject.AllForms("VIEWER").IsLoaded Then
Forms!VIEWER.Browser.Navigate Me.Hyperlink
Forms!VIEWER.Caption = Hyperlink
Else
DoCmd.OpenForm "VIEWER"
Forms!VIEWER.Browser.Navigate Me.Hyperlink
Forms!VIEWER.Caption = Hyperlink
End If

Else
'File Does Not Exist
End If
End If

End Sub

It seems you've changed your code from the subform's Current event to
the Click event of a button. I take it this button, "View", is on the
subform, not on the main form? Do you still want to set the focus to
the Sheet_No control after the navigation is complete? If so, did you
try this?

'----- start of suggested code -----
Private Sub View_Click()

Dim strLink As String

strLink = Me!Hyperlink & vbNullString

If Len(strLink) = 0 Then
Exit Sub
End If

If Len(Dir(strLink)) = 0 Then
Exit Sub
End If

If CurrentProject.AllForms("VIEWER").IsLoaded = False Then
DoCmd.OpenForm "VIEWER"
End If

With Forms!VIEWER
.Caption = strLink
With !Browser
.Navigate strLink
Do While .Busy
DoEvents
Loop
End With
End With

Me.Sheet_No.SetFocus

End Sub
'----- end of suggested code -----

It occurs to me that you may have to set the focus back to form
PROJECTS, too, but I'm not sure of that. Anyway, please post back and
tell me if this approach works, or gets you closer.
 
Z

Zadok @ Port of Seattle

Looks like you cleaned up my sad code quite nicely. Unfortunately, it seems
to behave the same, focus is still on the VIEWER form. Thanks for all the
help Dirk, I'll understand if you are out of solutions.

How I tweaked the last section to bring me back to my Form as you suggested.

---
With Forms!VIEWER
.Caption = strLink
With !Browser
.Navigate strLink
Do While .Busy
DoEvents
Loop
End With
End With

Forms!PROJECTS.SetFocus
Me.Sheet_No.SetFocus

End Sub
 
D

Dirk Goldgar

"Zadok @ Port of Seattle"
Looks like you cleaned up my sad code quite nicely. Unfortunately, it
seems to behave the same, focus is still on the VIEWER form. Thanks
for all the help Dirk, I'll understand if you are out of solutions.

How I tweaked the last section to bring me back to my Form as you
suggested.

---
With Forms!VIEWER
.Caption = strLink
With !Browser
.Navigate strLink
Do While .Busy
DoEvents
Loop
End With
End With

Forms!PROJECTS.SetFocus
Me.Sheet_No.SetFocus

End Sub

That's odd. I just tested code almost identical to that, and it worked.
Here's the code I tested:

'----- start of code -----
Private Sub Command1_Click()

Dim strLink As String

strLink = "C:\Temp\ACCInvoice110103.pdf"

If Len(strLink) = 0 Then
Exit Sub
End If

If CurrentProject.AllForms("frmWebBrowser").IsLoaded = False Then
DoCmd.OpenForm "frmWebBrowser"
End If

With Forms!frmWebBrowser
.Caption = strLink
With !ActiveXCtl0
.Navigate strLink
Do While .Busy
DoEvents
Loop
End With
End With

Me.SetFocus
Me.Sheet_No.SetFocus

End Sub
'----- end of code -----

Is there other code on either of these forms that might be interfering?
You proviously had this code in the form's Current event -- did you
remove the code that was there?
 
Z

Zadok @ Port of Seattle

I created 2 new forms and pasted your code in with the same results.
Just to clarify: I have a form with a button to open a new form containing a
Microsoft WebBrowser element which then displays the PDF and then puts the
focus back on the original form and to the control Sheet_No. In your new
code, why does Me.SetFocus not set the focus on the VIEWER form?

Also Dirk, is 'ActiveXCtl0' the name of your WebBrowser element?
 
D

Dirk Goldgar

"Zadok @ Port of Seattle"
I created 2 new forms and pasted your code in with the same results.

Odd. I wonder if it has to do with the Access version (I'm using
A2002), the OS version (I'm using Windows 2000 Pro), or the version of
Acrobat or Acrobat Reader that is serving the web browser control (I'm
using a very old version of Acrobat Reader -- 4.0).
Just to clarify: I have a form with a button to open a new form
containing a Microsoft WebBrowser element which then displays the PDF
and then puts the focus back on the original form and to the control
Sheet_No. In your new code, why does Me.SetFocus not set the focus on
the VIEWER form?

Because that code is running on the form that contains the command
button, so "Me" refers to that form, not the viewer form.
Also Dirk, is 'ActiveXCtl0' the name of your WebBrowser element?

Yes.

It may be worth finding out if your forms and code work the same on my
PC as on yours. If you'd like to send me a cut-down copy of your
database, containing only the elements necessary to demonstrate the
problem, compacted and then zipped to less than 1MB in size (preferably
much smaller) -- I'll have a look at it, time permitting. You can send
it to the address derived by removing NO SPAM from the reply address of
this message. If that address isn't visible to you, you can get it from
my web site, which is listed in my sig. Do *not* post my real address
in the newsgroup -- I don't want to be buried in spam and viruses.
 
D

Dirk Goldgar

Dirk Goldgar said:
"Zadok @ Port of Seattle"


Odd. I wonder if it has to do with the Access version (I'm using
A2002), the OS version (I'm using Windows 2000 Pro), or the version of
Acrobat or Acrobat Reader that is serving the web browser control (I'm
using a very old version of Acrobat Reader -- 4.0).


Because that code is running on the form that contains the command
button, so "Me" refers to that form, not the viewer form.


Yes.

It may be worth finding out if your forms and code work the same on my
PC as on yours. If you'd like to send me a cut-down copy of your
database, containing only the elements necessary to demonstrate the
problem, compacted and then zipped to less than 1MB in size
(preferably much smaller) -- I'll have a look at it, time
permitting.

Time permitted. Now I've tested the database you sent me, and it works
for me. In other words, clicking "View" opens the VIEWER form, displays
the "Hyperlink" PDF in the Browser control (I had to change the
hyperlink data in the table), and then brings the PROJECTS form to the
foreground again with the focus on the subform in the Sheet_No text box.

So if that's not what's happening for you, the problem may be in the
different versions of the various components that make this work. Did
you say what your various software versions are?
 
Z

Zadok @ Port of Seattle

well that is good news for you and bad for me, eh. thanks again for the
trouble shooting Dirk. guess i'm out of luck. i'm using XP, Access 2003,
Acrobat Pro 6.0.
 
D

Dirk Goldgar

"Zadok @ Port of Seattle"
well that is good news for you and bad for me, eh. thanks again for
the trouble shooting Dirk. guess i'm out of luck. i'm using XP,
Access 2003, Acrobat Pro 6.0.

Not necessarily. I'll bet you I can figure out a way to make it work
for you. Give me a day or so to tinker with it.

Do you really need to open your drawings in the web browser control?
Would it work to just open them in Acrobat, in an external window?
 
Z

Zadok @ Port of Seattle

i had it opening in an external window, the problem was that i couldnt see
both my database and the pdf. i need to see the pdf to type in the
information on the blueprint for the database.
 
Z

Zadok @ Port of Seattle

Dirk, I've also noticed that if I ever switch to view the VIEWER form and
come back to the PROJECTS form, focus is lost, even outside of running the
code. It looks like the VIEWER/WEB BROWSER element just always grabs focus no
matter what.
 
D

Dirk Goldgar

"Zadok @ Port of Seattle"
Dirk, I've also noticed that if I ever switch to view the VIEWER form
and come back to the PROJECTS form, focus is lost, even outside of
running the code. It looks like the VIEWER/WEB BROWSER element just
always grabs focus no matter what.

I don't kinow what's going on there or why. But let's see if I can get
you closer to where you want to go. In the Declarations section of the
code module of the PROJECTS form, put this function declaration:

Private Declare Function Putfocus Lib "user32" _
Alias "SetFocus" (ByVal hwnd As Long) _
As Long

Then, in the code for the command button, after the lines

Me.SetFocus
Me.Sheet_No.SetFocus

add the following line:

Putfocus Me.hWnd

I can't make a meaningful test of this here, since the original code
works for me. But let me know if that makes a difference for you.
 
Z

Zadok @ Port of Seattle

dirk,
still trying to get the code to work. i put a beep function in your original
code after the Do While .Busy code and it doesn't fire off. should it?
zadok
 
D

Dirk Goldgar

"Zadok @ Port of Seattle"
dirk,
still trying to get the code to work. i put a beep function in your
original code after the Do While .Busy code and it doesn't fire off.
should it? zadok

How do you know it doesn't "fire off"? Just that you don't hear a beep?
What do you have set up in your Sounds control-panel applet for "Default
Beep"? And, of course, do you have sound turned on?

The most reliable way to check whether code is executing is simply to
set a breakpoint on the line of code, and see if execution halts at that
breakpoint. Note that, to enable debugging, you have to have the
Startup option "Use Access Special Keys" checked.
 
Top