Share via

SetFocus and DropDown

Anonymous
2010-08-19T16:28:58+00:00

Hi all

I have a subform with a combo box named Type_id with a default value of 100000

I am trying to ensure users select from the list and, if they don't, the Focus remains on Type_id and expands the list

I have the following code:

Private Sub Type_id_LostFocus()

 ' go back to type_id if it is 100000

    If Type_id = 100000 Then

        MsgBox "You must select a type code"

        Me!Type_id.SetFocus

        Me!Type_id.Dropdown

    End If

End Sub

The message box works OK but the SetFocus line doesn't (nor line 3)

What am I doing wrong? Or is there another way of achieving what I want?

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2010-08-19T17:25:26+00:00

You cannot set the focus back to the control that is losing the focus.

You can try setting the focus to another control and then setting the focus back to this control. 

Me.SomeControl.SetFocus

Me.Type_ID.SetFocus

Me.Type_ID.DropDown

If you wish to trap the user in the Type_ID control then use the above code.  They will not be able to get out until they select a value or leave the field blank.

A better idea might be to use the control's validation rule.

Set the control's Validation rule to

<> 100000 and Is Not Null

Or if you want to allow nulls

<> 100000

Set the Validation text for the message to You must select a type code


John Spencer Access MVP 2002-2005, 2007-2010 The Hilltop Institute University of Maryland Baltimore County

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2010-08-21T20:14:35+00:00

    Could you give a specific example of a control that requires the brackets?  And the usage thereof.


    John Spencer Access MVP 2002-2005, 2007-2010 The Hilltop Institute University of Maryland Baltimore County

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-08-21T18:37:33+00:00

    Thanks John

    Setting the focus to another control first worked fine.

    On a slightly related subject, why does VBA require some controls to be enclosed in square brackets, and others not?

    Was this answer helpful?

    0 comments No comments