Can't Use Arguments to Call Sub When Another Procedure Running

ID: Q108814

The information in this article applies to:

SYMPTOMS

If you run a Microsoft Visual Basic Programming System, Applications Edition, sub procedure that has arguments when another Visual Basic procedure is running, you may receive the following error message:

   Can't perform requested operation

CAUSE

You cannot call a Visual Basic sub procedure that has arguments if a Visual Basic procedure is running at the time. Take for example a custom dialog box with a control button assigned to a Visual Basic procedure with an argument. If you run the dialog box using the "Run Dialog" toolbar button on the Forms toolbar, you can choose the button and the procedure assigned to that button is run. However, if you show the dialog box from another Visual Basic procedure, and you click the button assigned to the procedure that has arguments, you receive the error message above, because the first Visual Basic procedure is still running. When you run the dialog box from the Visual Basic procedure, the calling procedure is suspended while the dialog box is displayed.

WORKAROUND

Although you cannot call a Visual Basic sub procedure that has arguments if a Visual Basic procedure is running, you can do either of the following:

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft Support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

1. Enter the following in a new module sheet:

Public DoneFlag As Integer, ArgFlag As Integer

Sub MainMacro()
   ArgFlag = 0                           'initialize ArgFlag
   DoneFlag = 0                          'initialize DoneFlag
   DialogSheets("Dialog1").Show          'Show it initially

   'While the DoneFlag does not equal 1 (which will only occur if the
   'DoneButton is clicked), continue to loop through the Subroutine.

   Do
      If ArgFlag = 1 Then                 'if the ArgFlag is set, then
         HasArg ("Here is the Argument")  'Arg Sheet1 and
         ArgFlag = 0                      'reset the ArgFlag
         DialogSheets("Dialog1").Show     'Reshow it only after
      End If                              'having called the procedure
   Loop Until DoneFlag = 1                'that hid it
End Sub

Sub DoneButton_Click()
   DoneFlag = 1                          'set the DoneFlag
   DialogSheets("Dialog1").Hide          'hide the dialog box
End Sub

Sub ArgButton_Click()
   DoneFlag = 0                          'ensure DoneFlag set to 0
   ArgFlag = 1                         'set the ArgFlag
   DialogSheets("Dialog1").Hide          'hide the dialog box
End Sub

Sub HasArg(Arg As String)   'do not assign this macro to any button
MsgBox Arg
End Sub

2. Insert a new dialog sheet and make sure it is named "Dialog1", without
   quotes.

3. Add two new buttons to the dialog sheet.

4. Change the text of one button to "PassArg", without quotes, and assign

   the ArgButton_Click macro to this button.

5. Change the text of the other button to "Done", without quotes, and
   assign the DoneButton_Click macro to this button.

6. Run the MainMacro subroutine.

7. The dialog box will appear - click the "PassArg" button.

The dialog box will hide itself and a message box will appear with "Here is the Argument" displayed.

8. Click OK in the message box.

The dialog box reappears. This will repeat itself as long as you click the "PassArg" button on the dialog box.

9. Click the "Done" button to dismiss the dialog box and stop the MainMacro

   subroutine.

REFERENCES

For more information about the Show Method, choose the Search button in Help and type:

   dialog boxes: displaying for user input

Additional query words: 5.00 7.00
Keywords          : kbprg 
Version           : 5.00 5.00c 7.00
Platform          : WINDOWS

Last Reviewed: May 19, 1999