Button to open form where ID=ID, and setfocus on a certain control (1 Viewer)

regnewby2465

Member
Local time
Yesterday, 23:19
Joined
Sep 22, 2015
Messages
58

MNewby


I have a form named CAD_CallDispSplitF with a primarey key as ID in the underlying record source. The form has a command button, btn_addVehicle.

I have another form named frm_CAD_CallerAndVehicle that has two subforms: frm_CADcaller and frm_CAD_Vehicles. The subforms are NOT on individual pages. Each subform is linked to the parent form on ID.

Either through VBA or Macro, I need an On-Click event on the btn_addVehicle button to open form frm_CAD_CallerAndVehicle, and setfocus on the VehTop control on the frm_CAD_Vehicles subform.

When I create an on-click event in macro or VBA, it opens the form where ID=ID but does not set the focus on a control.

Thanks in advance.
Mark
 

LarryE

Active member
Local time
Yesterday, 20:19
Joined
Aug 18, 2021
Messages
603

MNewby


I have a form named CAD_CallDispSplitF with a primarey key as ID in the underlying record source. The form has a command button, btn_addVehicle.

I have another form named frm_CAD_CallerAndVehicle that has two subforms: frm_CADcaller and frm_CAD_Vehicles. The subforms are NOT on individual pages. Each subform is linked to the parent form on ID.

Either through VBA or Macro, I need an On-Click event on the btn_addVehicle button to open form frm_CAD_CallerAndVehicle, and setfocus on the VehTop control on the frm_CAD_Vehicles subform.

When I create an on-click event in macro or VBA, it opens the form where ID=ID but does not set the focus on a control.

Thanks in advance.
Mark
 

LarryE

Active member
Local time
Yesterday, 20:19
Joined
Aug 18, 2021
Messages
603
Buttons OnClick event:
DoCmd.OpenForm "frm_CAD_CallerAndVehicle"
Me.frm_CAD_Vehicles.Controls("VehTop").SetFocus
 

regnewby2465

Member
Local time
Yesterday, 23:19
Joined
Sep 22, 2015
Messages
58
Buttons OnClick event:
DoCmd.OpenForm "frm_CAD_CallerAndVehicle"
Me.frm_CAD_Vehicles.Controls("VehTop").SetFocus
Thanks Larry, however, I get a an error Compile Error: Nethod or data member not found.
 

regnewby2465

Member
Local time
Yesterday, 23:19
Joined
Sep 22, 2015
Messages
58
Here are the forms I am using

Snap 2022-09-21 at 12.55.11.png


Snap 2022-09-21 at 12.56.10.png


This is the code on the Add person and Add Vehicle buttons. I need the Add Person button to open the form and setfocus on the FName control on the persons subform; and the Add Vehicle button to open the form and setfocus on the VehTop control.

Code:
Private Sub btn_addVehicle_Click()

    DoCmd.OpenForm "frm_CAD_CallerAndVehicle", , , "ID = " & Me!ID
    
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 04:19
Joined
Sep 21, 2011
Messages
14,399
Unless you only want to set the focus on this particular call, I'd put the setfocus in the load event of the opening form?
You still have to get the syntax correct even then though.


Me. would not work from the calling form, it has to be inside the form that the code is running in and refer to that form.
You could use Forms! syntax, specifying the whole form path.

Pay attention to the fact that you need to set focus to the subform before the control on it.
 

LarryE

Active member
Local time
Yesterday, 20:19
Joined
Aug 18, 2021
Messages
603
With the frm_CAD_Vehicles On Open Event enter:
Me.VehTop.SetFocus
That will set the focus to VehTop when the frm_CAD_CallerAndVehicle opens assuming frm_CAD_Vehicles is a subform of frm_CAD_CallerAndVehicle.
 

regnewby2465

Member
Local time
Yesterday, 23:19
Joined
Sep 22, 2015
Messages
58
Larry, I put the Me.VehTop.SetFocus on the OnOpen event of the frm_CAD_Vehicles form. When I clicked on the Add Vehicle button on the form main form, it does open the frm_CAD_CallerAndVehicle form to the matching record, but it does not put the focus on the VehTop control. It puts the focus on the FName control of the other subform.
 

LarryE

Active member
Local time
Yesterday, 20:19
Joined
Aug 18, 2021
Messages
603
OK try with the parent form frm_CAD_CallerAndVehicle OnOpen Event:
Me.frm_CAD_Vehicles.Controls("VehTop").SetFocus
So we set the focus to VehTop when frm_CAD_CallerAndVehicle opens
Can't think of anything else. The sequence should be:
  1. Open frm_CAD_CallerAndVehicle
  2. Set the focus to the VehTop control on the frm_CAD_Vehicles subform
Isn't that right?

Edit:
When you open a form with subforms, the subforms actually open first, so you might need to use the frm_CAD_CallerAndVehicle OnLoad event instead of the OnOpen event. I hope one of them works at least.
 
Last edited:

regnewby2465

Member
Local time
Yesterday, 23:19
Joined
Sep 22, 2015
Messages
58
OK try with the parent form frm_CAD_CallerAndVehicle OnOpen Event:
Me.frm_CAD_Vehicles.Controls("VehTop").SetFocus
So we set the focus to VehTop when frm_CAD_CallerAndVehicle opens
Can't think of anything else. The sequence should be:
  1. Open frm_CAD_CallerAndVehicle
  2. Set the focus to the VehTop control on the frm_CAD_Vehicles subform
Isn't that right?

Edit:
When you open a form with subforms, the subforms actually open first, so you might need to use the frm_CAD_CallerAndVehicle OnLoad event instead of the OnOpen event. I hope one of them works at least.
I will try that but remember, there are two subforms on the frm_CAD_CallerAndVehicle form: Both subforms are on the same level. One is called frm_CAD_Vehicles, (which has the VehTop control), and the other subform is frm_CADcaller.
 

LarryE

Active member
Local time
Yesterday, 20:19
Joined
Aug 18, 2021
Messages
603
I experimented with one of my own forms that has 4 sub-forms on it. If I used the main form OnLoad event, I could set the focus on any sub-form control by setting the Tab Index on the sub-form to 0 and then with the main form ONLoad event use
Me.Sub-formName.SetFocus
So in your case:
  1. Set the VehTop controls Tab Index to 0 on the frm_CAD_Vehicles subform
  2. Use Me.frm_CAD_Vehicles.SetFocus in the frm_CAD_CallerAndVehicle OnLoad event
That will set the focus to the sub-form first, and then select the VehTop control automatically because its Tab Index is 0 (ACCESS considers 0 to be the first control on any form if its Tab Index is 0)

That should do it.
 

regnewby2465

Member
Local time
Yesterday, 23:19
Joined
Sep 22, 2015
Messages
58
Thank you for trying all that for me. It did work but it sets the focus to the VehTop control (index 0) whenever the main form is opened. As long as I click on the Add Vehicle button on the calling form, it will be fine because I am adding a vehicle. But if I click on the Add Perosn button on the calling form, it will also setfocus to the VehTop control, which needs to be FName control when adding a person.
 

MarkK

bit cruncher
Local time
Yesterday, 20:19
Joined
Mar 17, 2004
Messages
8,186
Try...
Code:
Private Sub btn_addVehicle_Click()
    Const FN = "frm_CAD_CallerAndVehicle"
    DoCmd.OpenForm FN, , , "ID = " & Me.ID
    Forms(FN).VehTop.SetFocus
End Sub
 

MarkK

bit cruncher
Local time
Yesterday, 20:19
Joined
Mar 17, 2004
Messages
8,186
Code:
Private Sub btn_addPerson_Click()
    AddNewAndSetfocus "FName"
End Sub

Private Sub btn_addVehicle_Click()
    AddNewAndSetfocus "VehTop"
End Sub

Private Sub AddNewAndSetfocus(ControlName As String)
    Const FN = "frm_CAD_CallerAndVehicle"
    DoCmd.OpenForm FN, , , "ID = " & Me.ID
    Forms(FN).Controls(ControlName).SetFocus
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:19
Joined
May 7, 2009
Messages
19,246
you can also try this.
add an OpenArgs parameter when you open "frm_CAD_CallerAndVehicle" form:
Code:
Private Sub btn_addVehicle_Click()

    DoCmd.OpenForm "frm_CAD_CallerAndVehicle", , , "ID = " & Me!ID, , , "Vehicle"
    
End Sub

Now, you add Code to the Load Event of "frm_CAD_CallerAndVehicle":
Code:
Private Sub Form_Load()
    Dim strSubName As String
    strSubName = Me.OpenArgs & ""
    If Len(strSubName) <> 0 Then
        Select Case strSubName
            Case "Person"
                Forms!frm_CAD_CallerAndVehicle.SetFocus
                Forms!frm_CAD_CallerAndVehicle![Person subform Name Here].SetFocus
                Forms!frm_CAD_CallerAndVehicler![Person subform Name Here].Form!FName.SetFocus
            Case "Vehicle"
                Forms!frm_CAD_CallerAndVehicle.SetFocus
                Forms!frm_CAD_CallerAndVehicle![Vehicle subform Name Here].SetFocus
                Forms!frm_CAD_CallerAndVehicle![Vehicle subform Name Here].Form![Veh Top].SetFocus
        End Select
    End If
End Sub
 

Users who are viewing this thread

Top Bottom