PRB: SetFocus During Form Load May Cause Illegal Function Call

Last reviewed: June 21, 1995
Article ID: Q88477
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

SYMPTOMS

   Using the SetFocus method to set the focus to a specific control on a
   form during the form load event procedure may result in an "Illegal
   Function Call" error.

CAUSE
   This error occurs because the form that the control is on is not yet
   visible.

RESOLUTION
   To prevent this error from occurring, execute Form.Show before executing
   the SetFocus method.

MORE INFORMATION

Steps to Reproduce Problem

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.

  2. Place a text box (Text1) on Form1.

  3. Add the following code to the Form_Load procedure for Form1:

       Sub Form_Load ()
          Text1.SetFocus
       End Sub
    
    

  4. Press F5 to run the application. The line of code in the load event will be highlighted with the error "Illegal Function Call."

  5. If you show the form before executing SetFocus, the program will run as expected.

       Sub Form_Load ()
          Form1.Show
          Text1.SetFocus
       End Sub
    


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.