PRB: Error Message "Invalid Procedure Call or Argument" While Modal Dialog or Form is Shown (242347)
The information in this article applies to:
- Microsoft Visual Basic Learning Edition for Windows 5.0
- Microsoft Visual Basic Learning Edition for Windows 6.0
- Microsoft Visual Basic Professional Edition for Windows 5.0
- Microsoft Visual Basic Professional Edition for Windows 6.0
- Microsoft Visual Basic Enterprise Edition for Windows 5.0
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
- Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
- Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
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.
Modification Type: | Major | Last Reviewed: | 6/24/2004 |
---|
Keywords: | kbprb KB242347 |
---|
|