Set focus to control in another subform on the same form

D

Dale Fye

I'm playing around with a contact management database.

I've been playing around with a process to duplicate the functionality seen
in the Outlook contacts list phone number section. I've got a popup menu
that displays when the user clicks on a label that indicates the type of
phone number. When the user selects from the popup, it changes the caption
of the label and the query of the form so that it returns only the records
for that type of phone. This is all in a subform, and works smoothly.

The problem is that since each phone number is displayed in its own subform,
when the user tabs out of the phone #, the cursor goes back to the Area Code
field for that phone type. I've got the KeyPreview set to Yes, and in the
KeyDown event of txt_Phone_Number I'm checking for KeyCode = vbKeyTab. If
it is, then I call a public function on the parent form (see below), but
nothing I seem to do actually sets the focus to the Area Code control on the
next phone number.

Private Sub txt_Phone_Number_KeyDown(KeyCode As Integer, Shift As Integer)

if KeyCode <> vbKeyTab then exit sub

'I have some error checking code in here, deleted for clarity
'.....

'Valid phone number, set focus the the Area Code field in the next
sub_Phone subform
Call Me.Parent.NextPhone

End Sub

Public Sub NextPhone()

Dim intPhone As Integer
Dim strControl As String

intPhone = Right(Me.ActiveControl.Name, 1)

'Move the focus to the next phone subform
strControl = "sub_Phone" & (intPhone + 1)
On Error Resume Next
Me.Controls(strControl).Form.txt_Area_Code.SetFocus
Debug.Print Err.Number, Err.Description
if Err.Num <> 0 then
'set focus to the control with the next lowest tab index.
endif

End Sub
 
T

tina

if you're running code *in a subform*, and want to set focus to a control in
another subform on the same mainform, the syntax would be

Me.Parent!OtherSubformControlName.SetFocus
Me.Parent!OtherSubformControlName.Form!ControlName.SetFocus

hth
 
D

Dale Fye

Disregard, I figured it out.

Before setting the focus to the control on the other subform, I had to set
the focus to the other subform. It now looks like:

Public Sub NextPhone()

Dim intPhone As Integer
Dim strControl As String

intPhone = Right(Me.ActiveControl.Name, 1)

'Move the focus to the next phone subform
strControl = "sub_Phone" & (intPhone + 1)
On Error Resume Next
me.controls(strControl).setfocus
if Err.Num = 0 then
Me.Controls(strControl).Form.txt_Area_Code.SetFocus
Else
'set focus to the control with the next lowest tab index.
end if

End Sub

Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top