Form onLoad question

Hi guys,

why am I getting this error - "Invalid procedure call or argument?"

Private Sub Form_Load()
    If App.PrevInstance Then
        MsgBox "The application is already running!", vbCritical, "Error!"
        End
    End If
   
    Me.SSTab1.Tab = 0
    Me.StoreName0.SetFocus  'Error here!
   
    For i = 0 To 6
        Me.Controls("SetPoint" & i).AddItem "Occupied"
        Me.Controls("SetPoint" & i).AddItem "Unoccupied"
    Next i
   
End Sub

Thanks.
LVL 7
Yury MerezhkovDevelopment Team LeadAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jkaiosConnect With a Mentor IT DirectorCommented:
You can only move the focus to a visible form or control. Because a form and controls on a form aren't visible until the form's Load event has finished, you CANNOT use the SetFocus method to move the focus to a control on the form being loaded unless you first use the Show method to show the form before the Form_Load event procedure is finished.

To work around the problem, you either have to set the TabIndex property of the StoreName0 control to zero (0) at design time or move this line:
   "Me.StoreName0.SetFocus"
to the Form_Activate() event.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
This assumes there are combo boxes SetPoint0, SetPoint1, SetPoint2, ..., SetPoint6
If any of those are missing or a different type of control, that would cause an invalid procedure error.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>    Me.StoreName0.SetFocus  'Error here!
Whoops, didn't see the 'Error here! part.
Make sure StoreName0 is a control, and is typed correctly, and is not a control array such as StoreName(0)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Yury MerezhkovDevelopment Team LeadAuthor Commented:
StoreName0 is a simple textbox. It's not a control array. It's typed correctly. Very strange.
0
 
Yury MerezhkovDevelopment Team LeadAuthor Commented:
I can do this:

Me.StoreName0.Text = "TEST"

but setfocus just won't work.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>I can do this: Me.StoreName0.Text = "TEST"  but setfocus just won't work.
Some wild guesses...  Is Enabled = True?  Locked = False?  Visible = True?
0
 
jkaiosIT DirectorCommented:
But if you still want to set the focus on the control at the Form_Load event, you simple force Load event to finish by using the Show method.

Private Sub Form_Load()
    Me.Show                     '<-- CALL THIS METHOD FIRST B4 SETTING THE FOCUS TO ANY CONTROL
    Me.SSTab1.Tab = 0
    Me.StoreName0.SetFocus
End Sub
0
All Courses

From novice to tech pro — start learning today.