Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On click navigate to url using webbrowser control

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
GB
I’m using MS Access 2003 with the WebBrowser control on my Form.

When I click command button ‘cmdGo’ the code is supposed to navigate to the ‘URL’ replacing the variables with the values from the appropriate controls on the form. When the page has loaded it should return the ‘result_box’ inner text to the control ‘txtTo’ on the form.

The problem is I can only get it to work by changing Debug.Print to MsgBox in the xWebCtl_DocumentComplete event, but of course the message box is displayed which I don’t want.

I have tried creating Boolean variables and replacing the debug.print line with them but it doesn’t seem to work either. I know the debug.print line isn’t doing anything apart from printing the line but I don’t know what to put there, or even if I need the DocumentComplete event, or if the problem resides in the command button ‘cmdGo’ click code.

If any one knows the correct way to code this or where I’m going wrong I would be grateful.

This is my current code:

Code:
Private Sub xWebCtl_DocumentComplete(ByVal pDisp As Object, URL As Variant)
   If (pDisp Is xWebCtl.Object) Then
      Debug.Print "Web document is finished downloading"
   End If
End Sub

Private Sub cmdGo_Click()
Dim URL As String
URL = "[URL unfurl="true"]https://translate.google.com/#"[/URL] & Me.cboFrom.Column(1) & "/" & Me.cboTo.Column(1) & "/" & txtFrom

With xWebCtl
     .Silent = True
    .Navigate "about:blank"
Do While .Busy Or .ReadyState <> 4
    DoEvents
Loop
    .Navigate URL, 4 'The 4 turns off caching.
Do While .Busy Or .ReadyState <> 4
    DoEvents
Loop

txtTo = .Document.getElementById("result_box").innertext
'Debug.Print IE.Document.getElementById("gt-sl").Value
Dim str As String
str = .Document.getElementById("gt-lang-left").innertext
txtLangDetected = Replace(str, "EnglishSpanishFrench", "")

End With

End Sub
 
OP said:
The problem is I can only get it to work by changing Debug.Print to MsgBox in the xWebCtl_DocumentComplete event, but of course the message box is displayed which I don’t want.

Two things to try given that line:
1. Comment out the Debug.Print and MsgBox as well
2. If #1 doesn't work by itself, try adding DoCmd.Wait where those would run.

I suggest #2 because it sounds to me like for some reason, Access is somehow tripping over itself. I don't see why, but if the Msgbox makes it work, then simply adding a slight pause between the two MIGHT fix it.

Another thing you could try if #2 doesn't work is to add in a line to say txtLangDetected.Select or whatever the correct command is. I don't think this should be required, but again, it's worth a try. If you do go this route, you may also want to capture the name of the currently selected control before this command, and set focus back to the original control.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
You might want to refactor your code a bit.

Consider, given the fact that the tight [tt]Do While .Busy Or .ReadyState <>4[/tt] loop is simply waiting for the document to complete and that the web browser control already has an [tt]event[/tt] for this (that you are already using), that you might want to eliminate the loops and put most of your code in the event
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top