Microsoft KB Archive/242347

= PRB: Error Message "Invalid Procedure Call or Argument" While Modal Dialog or Form is Shown =

Article ID: 242347

Article Last Modified on 6/24/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q242347



SYMPTOMS
If a modal dialog or form is programmatically shown in code, other executing code in the same process that calls SetFocus on a control on a dialog or form other than the one currently modal raises the following:

Run-time Error '5'

Invalid Procedure Call or Argument

This only occurs when running the application as a compiled EXE. This does not occur in the Visual Basic IDE.



CAUSE
In the case of the SetFocus call, when a modal dialog, such as a messagebox or a modal Visual Basic form is shown, any other forms and their controls are disabled. Calling SetFocus on a disabled control always generates an error of this type.



RESOLUTION
There are several workarounds that can be used:

The most elegant workaround is to first check to see if the target of the SetFocus is Enabled.

The problem can be avoided by changing the repro sample code in the Timer event from this: Private Sub Timer1_Timer Command2.SetFocus End Sub to this: Private Sub Timer1_Timer If Command2.Enabled = True Then Command2.SetFocus Else Beep End If End Sub This works because whenever a modal dialog or form is shown, the controls on non-modal forms in that process become disabled.

Other ways that can work around this problem include:
 * Trapping the Error 5 within the procedure that actually calls the SetFocus.
 * Setting a global flag that becomes True when a call to MsgBox is made and check it before you make a call to SetFocus.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Start a new Standard EXE project in Visual Basic. Form1 is created by default. Add two Command Buttons, Command1 and Command2, and a Timer control, Timer1, to Form1.  Add the following code to Form1: Private Sub Command1_Click MsgBox "Wait until the timer control fires" End Sub

Private Sub Timer1_Timer Command2.SetFocus End Sub  Set the Timer1.Interval property to 3000 for a three-second delay. Compile the project and run the newly-compiled EXE file.</li> Immediately after starting the application, click Command1 and wait for the Timer control to fire its Timer Event. When the event fires, you receive the error dialog described in the SYMPTOMS section above. After dismissing this dialog and the message box, your application terminates.</li></ol>

Additional query words: modally

Keywords: kbprb KB242347

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.