tags:

views:

930

answers:

4

Strange error specific to a particular machine...

I have a app in which a combo box's text value is set to the path of a document (i.e...

cmbAIDFile.Text = clsTonyToolkit.GetSetting("ExportAIDFile",gtypmetadata.gcnnCentral) & ""

Forget about all the GetSetting procedure etc, just that it returns a line of text and works fine.

This works properly on all machines except one. This particular Vista machine throws the error Run-time error 0 and throws the user out of the program regardless of the ErrorHandling on the procedure. Other Vista machines work fine.

Any answers to this confusing issue would be gratefully received.

+1  A: 

What is the difference between the vista machines?

  • version
  • settings
  • user permissions

Al can have impact on how the application works.

Gamecat
A: 

Thanks for your help.

The machine is running Vista Business 64 bit.

User has the ability to get admin rights through User Account Control which is turned on.

Following your prompts I have tried running the app as admin and getting all sorts of strange responses (missing files, dependencies not registered etc). Have uninstalled the app and will try reinstalling. I think shadow copies of the folder might be confusing things too.

Tony
Have reinstalled and app is showing the same behavious
Tony
+4  A: 

You might be looking in the wrong place for the error. When you change the Text property of a ComboBox, the ComboBox's Change event will fire, if the new text is different from the previous text. The LostFocus and/or Validate events might also fire, depending on what your code is doing and how your form is set up.

If an error occurs in the Change event handler (or in the LostFocus or Validate event handlers, if you are doing anything with them), the error-handling code in the code that changes the combobox text will never get called (this has to do with how error-handling and events work in VB6).

As a general rule, you should always put an error handler in every single routine (Sub, Function, or Property) in your VB6 code, because you can never be sure you are going to catch every error otherwise.

Here is a quick example that demonstrates that error-handling might not always work as you might think it should in VB6.

Create a new Standard EXE project and add a CommandButton (Command1) and a ComboBox (Combo1) to the default form, and add the following code to the form:

Private Sub Combo1_Change()

   Dim a As Long
   a = 1/0 '<-- this will cause a divide-by-zero runtime error'

End Sub

Private Sub Command1_Click()

    On Error GoTo MyErrorHandler

    'Change the combobox text.'
    'This will cause the Change event to fire' 

    Combo1.Text = "test"

    Exit Sub

MyErrorHandler:
    'This code will not be executed if an error occurs in Combo1_Change...'
    MsgBox "My error-handler called."

End Sub

If you compile the project and run the resulting EXE, you will get a run-time error whenever you click Command1 and the program will terminate. This is because the error handling code that was added (MyErrorHandler in the example) is not getting called. However, if you add error-handling code to the Combo1_Change event handler, you can catch the error and try to deal with it (at the very least, you can keep the program from crashing).

Therefore, I would make sure that every event procedure has error-handling code in it, and add it if it's missing. That should make it easier to pinpoint where the error is actually occurring. For example, if you have code in the cmbAIDFile_Change event procedure, make sure it has error-handling. I would add line numbers to your code as well (if you don't have them already): that way you can use Erl in your error-handling code to get the actual line number where the error occurred. See the following code for an example of how to log line numbers in your error messages.

Using Erl to report line numbers in error messages

Private Sub cmbAIDFile_Change()

1000   On Error Goto ErrorHandler

1010   DoSomething
1020   DoSomethingElse

ErrorHandler:

1030   Dim sErrMsg As String
       sErrMsg = "A fatal error occurred." & vbCrLf & vbCrLf  & _
                 "Method: cmbAIDFile_Change" & vbCrLf & _
                 "Line: " & Erl & vbCrLf & _ 
                 "Err.Number: " & Err.Number & vbCrLf & _
                 "Err.Description: " & Err.Description

1040   MsgBox sErrMsg, vbCritical+vbOKOnly, "Fatal error"

End Sub

Once you have more fine-grained error-handling in place, you can begin to investigate why you are seeing a problem on only one machine, because you should have much more reliable information about the error that is occurring, and be in a much better position to figure what the root cause is.

Mike Spross
Wouldn't this behavior be the same on all machines, since it's a known issue in VB6? (I tested to confirm.) This doesn't explain why Ton'y's only seeing the problem on the one machine.
JeffK
@Jeff: You're right, it doesn't, but it might help him to find what line of code is actually failing on Vista. He is looking at the line of code that changes the Text property; however, I'm betting the code is erroring out in an event handler somewhere...
Mike Spross
What I'm suggesting in my answer is that he add error-handling to any event handlers he might have, that way he may be able to catch the error closer to where it's actually happening. The code we can't see could be doing any number of things that might fail on one machine and not another...
Mike Spross
@Mike: I've used VB since version 3, and in all that time never came across this, which is why I had to run your code example to believe it. I guess I've just always had event handlers for all of my events. This is a huge miss by MSFT. It's amazing that this behavior is still present.
JeffK
There is indeed some code in the Change event for the combo box and that code does all sorts of work for configuring the controls on the form. The code I've presented occurs in the form_load event and I suspect not all controls are initialised.
Tony
The machine with the error occurring is quite high powered and may produce the error because of some weirdness in the order it is initialising the controls on the form. I cannot get to that machine for maybe a couple of days. As soon as I can I'll check if this is the solution. Thanks for all so far
Tony
+1  A: 

Mike Spears suggestion is right on the money you need to put event handlers in all your events as assigning a string to a combo box's text causes a number of events to fire.

My contribution is to try the following.

Dim TempS as String
TempS = clsTonyToolkit.GetSetting("ExportAIDFile",gtypmetadata.gcnnCentral) & ""
cmbAIDFile.Text = TempS

There may be something funky going on with Unicode. This is indicated by Error 0 "Invalid procedure call or argument" on otherwise normal assignment. Again if you have event procedures for cmbAIDfile then you need to trap them so you can see if the error is occuring there.

RS Conley