"Method Not Applicable For This Object" with SetFocus

ID Number: Q72676

1.00

WINDOWS

Summary:

Visual Basic will return "Method Not Applicable For This Object"

whenever you use the SetFocus method on a control that is on a form

that is not visible.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

A control needs to be visible before you can execute the SetFocus

method to that control. To make a control visible, the form must be

loaded and the Visible property for the control must also be True

{-1}.

As an example, create two forms, and on each form, create a command

button.

In the code segment that follows, Form2 is not loaded yet, so you must

show Form2 before you can set the focus to Form2.Command1. Once Form2

has been loaded, you can set the focus to the visible control

Form2.Command1. When you click the Form1.Command1 twice, you will

receive the error "Method Not Applicable For This Object" because

Form2.Command1's visible property was set to false during the previous

call to Form_Click.

'* This is the load event for Form1.

Sub Form_Load ()

Form2.Show

Form2.Command1.Visible = 0 'this is the default setting

End Sub

'* This is the click event for Form1.Command1

Sub Command1_Click ()

Form2.Command1.SetFocus

Form2.Command1.Visible = 0 'hide the control

End Sub