Webbrowser Stopping process on Error Message

Posted on 2005-03-31
Medium Priority
Last Modified: 2013-12-26
This is a repeat question that i have asked already, however i closed it out too soon. So heres another try at it.

I have made a webbrowser that basicly goes out to the web every half hour to download data from a webpage.
Everything works great, however once and a while i get an error from the page saying "Lost SQL connection during query". When this happens my process halts and waits for a user to walk up and hit ok on the error. I have since added the webbrowser1.silent = true and i get less dialog errors. However this paticular error is not silenced using the previous.

I have also tried refrencing the internet explorer object itself using only code (no control) which the process keeps flowing when this dialog comes up (being that IE is independant of my VB, it just launches a new window on the next cycle) however i cannot get a document_complete message back when the page loads so that method isn't really useful either. (I may just be missing something altogether)
I would prefer to use the webbrowser control if i can find a way to silence or trap that error when it comes up so that my process keeps flowing. (As a temporary workaround i have vb move the mouse to the middle of the screen and send a click every half hour or so to click ok on the button) This is of course a complete hack job and i would like to handle it in a cleaner manner. I really wanted to post a screen shot of the error in the hopes that someone would relate to it beter. If anyone has any good ideas i would greatly appreciate it. (Meanwhile i fight with the site admin to get the sql errors stopped at the root, but this is a slow and painful process. Especially so close to year end) Thanks again.     -Shawn
Question by:ShawnCorbett
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
  • 3
  • 2
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13678438
Can u post the code u r using, and show the position where u get that error


Author Comment

ID: 13682640
I can post the code but it won't do much good. The error is not coming from within my program. If you brought up IE and went to the webpage the same error box would come up if your timing was right. Its basicly a slip between the sql server and the webpage. However when my process is running it launches the webpage and becuase the sql error is coming from the browser I created, the program halts there. here is the code i am using if it helps. Its very basic.

Private Sub Form_Load()
  WebBrowser1.Silent = True
End Sub

Private Sub Timer1_Timer() 'every half hour
    WebBrowser1.Offline = False
    WebBrowser1.Navigate "www.blah.com"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
     Select Case URL
       Case "www.blah.com/index.php"
       Case "www.blah.com/home.php"
     End Select
End Sub

Private Sub login()
 On Error GoTo alreadyin
   WebBrowser1.Document.Forms(0).Item("Uname").Value = "ME"
   WebBrowser1.Document.Forms(0).Item("Email").Value = "MYEMAIL"
   WebBrowser1.Document.Forms(0).Item("Password").Value = "MYPASSWORD"
 Exit Sub
  If Err.Number = 91 Then WebBrowser1.Navigate "www.blah.com/home.php" else Resume Next
End Sub
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13686365

 >>Private Sub Timer1_Timer() 'every half hour

What do u mean by every half hour, if u r using Timer control , its interval is in milliseconds. that is
For example, a value of 10,000 milliseconds equals 10 seconds. The maximum, 65,535 milliseconds, is equivalent to just over 1 minute.

This may be the reason of hanging
after few seconds u r aging executing the statement
>>WebBrowser1.Navigate "www.blah.com"

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13700562
Thats just the basic underlining routine. I bascialy use the timer1.tag to increment every minute (interval 60000) adding one until the timer1.tag = 30. This is the equivalant of half hour. And works fine. However as i have said the problem isn't coming from vb, it is coming from the webpage. The Webpage is having problems with accessing the SQL database. When this happens a Msgbox comes up from IE saying "Lost SQL connection during query". This is becuase the Domain and the SQL database are not currently in synch. I want to be able to close this window when it comes up.
SO i am looking for a way to detect that the popup error came up and once it does be able to close it. becuase it basicly puts my webbrowser1 into a stopped mode waiting for someone to walk up and hit ok on the msgbox. This error is NOT comming from within VB it is coming from the internet exploder object and webbrowser1.silent = true does NOT silence this paticular error box. Is there any other way to silence or detect IE (script errors/page errors)?

LVL 10

Expert Comment

ID: 13711596
Hi, ShawnCorbett.

Would it be possible for you to have your Timer regularly scan for the presence of the pop-up error window, and just close it using the APIs I've listed below?  To do this:

1)  Add the following code to your form's General Declarations section:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

     Private Const SW_SHOW = 5
     Const WM_CLOSE = &H10

2)  Next, initialize the following variables in your form's Form Load event:

    Dim winHwnd As Long
    Dim RetVal As Long
    Dim strPopupName As String

3)  Then, in your Timer routine, add:

     'Private Sub Timer1_Timer()
          strPopupName = "Lost SQL connection during query"
          winHwnd = FindWindow(vbNullString, a)
          'Msgbox winHwnd
          If winHwnd <> 0 Then
              PostMessage winHwnd, WM_CLOSE, 0&, 0&
          End If

     'End Sub

Note:  I've got this set up with strPopupName = "Lost SQL connection during query".  You may need to alter the text in quotes.  It needs to exactly match the title of your error message's pop-up box.  To see if the handle of the pop-up is being found, you can enable the "Msgbox WinHwnd" above--if it displays "0", then title needs to be adjusted for the "strPopupName = ..." statement.

Author Comment

ID: 13728504
edwardiii, Thanks for the post I am trying it now. however i have one question. strPopupName is set to "Lost SQL connection during query" however should the FindWindow statement be different? Where do i search for the string. I am guessing maybe it should be like below?

          strPopupName = "Lost SQL connection during query"
          winHwnd = FindWindow(vbNullString, strPopupName) instead of 'a'

Also the string strPopupName should be looking for the title not the text in the message right? Becuase the popup i get simply says "Internet Explorer" and the text in the box would be "Lost SQL connection during query". So i should be looking for "Internet Explorer" Right? Also Does this API look at all currently open windows? or just popup windows? Thanks again for your help!


LVL 10

Accepted Solution

edwardiii earned 1000 total points
ID: 13730485
Hi, Shawn.

Sorry-you're right about replacing "a" with "strPopupName in the winHwnd statement.  Yes, the routine is searching for window titles.  If the popup window appears on your taskbar, the name you see when you hover your mouse over it is the name you want.  If not, then the pop-up caption will do -- "Internet Explorer".  Try setting strPopupName to "Internet Explorer" and see if that does the job.  The question here is whether closing the window is sufficient, or if the press of a button in the popup is required.  If so, we can approach that problem too:)

Author Comment

ID: 13738922
edwardiii,  That solution seems to work awesome. The only downside i see is that any window that comes up with "Microsoft Internet Exploder" will be closed. But i use Firefox for a browser anyways So its not anything that i am worried about. Thanks again for your help!

LVL 10

Expert Comment

ID: 13739437
Glad I could help:)

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

801 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