Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which control has the focus?

Status
Not open for further replies.

annie52

Technical User
Mar 13, 2009
164
0
0
US
I hide and show two command buttons ([cmdEnterJD] and [cmdAllReqns]) on my main form. This was working fine until I added a hyperlink to one of the tabbed subforms. (The hyperlink opens a PDF document.) When I close the pdf document and then click on the [cmdAllReqns] button to release the filter, I get Error #2165 "You can't hide a control that has the focus". The two command buttons still work fine if I don't use the hyperlink. I'm having trouble finding the problem because I can't see which control has the focus. Is there a command I can run in the Immediate window that will identify the control the currently has the focus?


Private Sub cmdEnterJD_Click()
On Error GoTo Err_cmdEnterJD_Click

Me.FilterOn = False
Me.Filter = "[JD]='" & Me![txtEnterJD] & "' AND [Serial] = '" & Me![txtEnterSerial] & "'"
Me.FilterOn = True

If IsNull(Forms![frmReqnWatch]![JD]) Then
MsgBox "That requisition is not in the database." & vbCrLf, _
vbOKOnly, "RPP Tracking System"
Me.FilterOn = False
Me.txtEnterJD = ""
Me.txtEnterSerial = ""
Me.txtEnterJD.SetFocus
Else
Me.txtEnterJD.SetFocus
Me.cmdEnterJD.Visible = False
Me.cmdAllReqns.Visible = True
End If

Exit_cmdEnterJD_Click:
Exit Sub

Err_cmdEnterJD_Click:
Call RPPErrorHandler
Resume Exit_cmdEnterJD_Click

End Sub

Private Sub cmdAllReqns_Click()
On Error GoTo Err_cmdAllReqns_Click

Me.FilterOn = False
Me.Filter = ""
txtEnterJD = ""
txtEnterSerial = ""
Me.txtEnterJD.SetFocus
Me.cmdAllReqns.Visible = False
Me.cmdEnterJD.Visible = True

Exit_cmdAllReqns_Click:
Exit Sub

Err_cmdAllReqns_Click:
Call RPPErrorHandler
Resume Exit_cmdAllReqns_Click

End Sub

 
Have you tried setting a break point and stepping through the code?

Ian Mayor (UK)
Program Error
9 times out of 10 I know what I'm talking about. This must be my tenth reply.
 
Code:
Dim ctlCurrentControl As Control
Dim strControlName As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name

Debug.Print strControlName
displays the current control

Ian Mayor (UK)
Program Error
9 times out of 10 I know what I'm talking about. This must be my tenth reply.
 
How are ya annie52 . . .

Strange this ... as your code looks just fine! Try adding the line in [purple]purple[/purple] where you see it:
Code:
[blue]   Me.FilterOn = False
   Me.Filter = ""
   txtEnterJD = ""
   txtEnterSerial = ""
   Me.txtEnterJD.SetFocus
   [purple][b]DoEvents[/b][/purple]
   Me.cmdAllReqns.Visible = False
   Me.cmdEnterJD.Visible = True[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
annie52 . . .

Are [blue]txtEnterJD[/blue] and [blue]txtEnterSerial[/blue] on the mainform? ... if not, you need to reference the controls thru the subform!

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Hi Ian and TheAceMan1. Thanks for your replies.

Ian, thanks for the code snippet but I'm not sure how to use it properly. When I added it to the cmdAllReqns_Click routine, the focus never moves from the cmdAllReqns control even after stepping through the Me.txtEnterJD.SetFocus line.

TheAceMan1, I included the DoEvents line that you suggested but don't see anything happening as a result. Also, txtEnterJD and txtEnterSerial are on the main form.

It seems as though the focus is staying with the hyperlink on the subform that opens the PDF document even though my cursor is blinking in the txtEnterJD field which is on the main form. The hyperlink control is supposed to be hidden if there's no data but it remains in view. I've tried moving the focus to other fields on that subform and on the mainform but that doesn't help. Any ideas why I can't move the focus away from this hyperlink?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top