PRB: ActiveControl Set to "Nothing" on Disabled Form
Article ID: 173348
Article Last Modified on 5/13/2003
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
This article was previously published under Q173348
SYMPTOMS
In Microsoft Visual Basic version 4.0 for Windows, referencing the
ActiveControl property of a form that has been disabled returns the
control. In Visual Basic for Windows versions 5.0 and later, referencing
the ActiveControl property of a form that has been disabled causes run-time
error 91 "Object variable or With block variable not set" to be displayed.
CAUSE
In Visual Basic 5.0 and later, the value of the ActiveControl property of a
form is set to "Nothing" when the form is disabled.
RESOLUTION
The behavior is correct. This change in behavior may affect Visual Basic
4.0 applications that have been coded to the old behavior and then
converted to Visual Basic 5.0 or later. One workaround for this problem is
to store the value of the ActiveControl property in a variable before a
form is disabled. That variable can later be used to reference the control.
To prevent the run-time error from occurring, change the code to the
following:
Public Form2LastControl As Control 'General Declarations of Form1
Private Sub Command1_Click()
Set Form2LastControl = Form2.ActiveControl
Form2.Enabled = False
End Sub
Private Sub Command2_Click()
MsgBox Form2LastControl.Text
End Sub
Private Sub Form_Load() 'Form Load Event of Form1
Form2.Show vbModeless
End Sub
Private Sub Form_Load() 'Form Load Event of Form2
Set Form1.Form2LastControl = Me.Controls(0)
End Sub
STATUS
This behavior is by design.
Additional query words: kbVBp kbdsd kbDSupport kbVBp500 kbVBp600 kbControl
Keywords: kbprb KB173348