Share via

SetFocus Does Not trigger a GotFocus Event

Anonymous
2015-06-13T01:07:20+00:00

The code below works just fine if the user clicks on the TopCatCodee control.

 Private Sub TopCatCodee_GotFocus()

 If Me.IntTopSzeCode = 3 Then

 Me.txtFullness = 60

 ElseIf Me.IntTopSzeCode = 4 Then

 Me.txtFullness = 80

 ElseIf Me.IntTopSzeCode = 5 Then

 Me.txtFullness = 100

 ElseIf Me.IntTopSzeCode = 6 Then

 Me.txtFullness = 120

 End If

 End Sub

However that is not the normal way the user gets to the control.  Instead  the following code runs when the user closes the SelectTopStyle form.  The last line sets the focus to the control but the above GotFocus code won’t run.  Is  there a solution?  I have tried the lost focus event too but it didn’t make a difference.

    Dim ctrl As Control

        Set ctrl = Me.lstTopDesc

        If Not IsNull(ctrl) Then

        On Error Resume Next

        Forms.orderentry.OrderDraperyLineItems.Form.Controls("TopDescCode") = ctrl

        Forms.orderentry.OrderDraperyLineItems.Form.topcatcodee.SetFocus

         End If

    End Sub

Any help is greatly appreciated.

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

Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
2015-06-13T02:36:09+00:00

Hi Lily,

That is by design. When doing things with VBA no events ever fire. 99% of the time this is a good thing; the other times you simply call the event procedure yourself:

        Forms.orderentry.OrderDraperyLineItems.Form.Controls("TopDescCode") = ctrl

        Forms.orderentry.OrderDraperyLineItems.Form.topcatcodee.SetFocus

         Forms.orderentry.OrderDraperyLineItems.Form.topcatcodee_GotFocus

You may have to make it a Public Sub:

Public Sub TopCatCodee_GotFocus()

  Me.txtFullness = 20 * Me.IntTopSzeCode

End Sub

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2015-06-15T11:23:14+00:00

    It worked.  I added the _GotFocus line and also the remaining code to the procedure and it does just what I needed.  Thanks again for the help.

    Was this answer helpful?

    0 comments No comments