Solved Open Form B from Form A button click and after Form B opens call Sub on From B (1 Viewer)

Sh8dyDan

New member
Local time
Today, 18:39
Joined
Dec 15, 2022
Messages
26
<SOLVED - See Post #20>

I have a form called Contact_frm with a button named NewContact_cmd. I'd like to click the button and open ContactDetail_frm calling a Private Sub NewContact_cmd_Click in ContactDetail_frm.

Form: Contact_frmForm: ContactDetail_frm
Button: NewContact_cmdPrivate Sub: NewContact_cmd_Click
Action: Click button open ContactDetail_frm and call Private Sub NewContact_cmd_Click

Thanks in advance,
Dan
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:39
Joined
Feb 19, 2002
Messages
43,293
In general this sounds like a bad idea. Can you tell us what business function you are attempting to implement?
 

plog

Banishment Pending
Local time
Today, 17:39
Joined
May 11, 2011
Messages
11,646
Put code on Form_B's OnOpen event. If you only need it to run only in specific instances, you can use OpenArg's to control that:

 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:39
Joined
May 21, 2018
Messages
8,529
To call a procedure on another form
Code:
dim frm as Form_ContactDetail_Frm
docmd.openForm "ContactDetail_Frm"
set frm = forms("contactDetail_Frm")
frm.NewContact_Cmd_Click()
But I agree there is likely a much better way to do this.
 

Sh8dyDan

New member
Local time
Today, 18:39
Joined
Dec 15, 2022
Messages
26
In general this sounds like a bad idea. Can you tell us what business function you are attempting to implement?
Contact_frm is a split form with a button "Add Contact" DoCmd.OpenForm "ContactDetail_frm", acNormal, "", "1=0", , acDialog when clicked it opens ContactDetail_frm. In ContactDetail_frm is a Private Sub NewContact_cmd_Click DoCmd.GoToRecord , "", acNewRecDoCmd.GoToControl "FirstName_fld".

Really, I want to set focus to FirstName_fld when ContactDetail_frm opens from button click on Contact_frm.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:39
Joined
May 21, 2018
Messages
8,529
Really, I want to set focus to FirstName_fld when ContactDetail_frm opens from button click on Contact_frm.
Code:
docmd.openForm "ContactDetail_Frm"
forms("contactDetail_Frm").FirstName_Fld.Setfocus
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:39
Joined
May 21, 2018
Messages
8,529
Again, I do not recommend that design, But this shows it is possible to call another forms event procedure.
 

Sh8dyDan

New member
Local time
Today, 18:39
Joined
Dec 15, 2022
Messages
26
Code:
docmd.openForm "ContactDetail_Frm"
forms("contactDetail_Frm").FirstName_Fld.Setfocus

Code:
docmd.openForm "ContactDetail_Frm"
forms("contactDetail_Frm").FirstName_Fld.Setfocus
I get "Application-defined or object-defined error"
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:39
Joined
May 21, 2018
Messages
8,529
does the form open. Are all those names spelled correctly? is the first name text box really called FirstName_Fld?
 

Sh8dyDan

New member
Local time
Today, 18:39
Joined
Dec 15, 2022
Messages
26
does the form open. Are all those names spelled correctly? is the first name text box really called FirstName_Fld?
Good call, I had a typo. Error gone. Form opened just not on new. Getting closer. Looking into it now.
 

Sh8dyDan

New member
Local time
Today, 18:39
Joined
Dec 15, 2022
Messages
26
Code:
DoCmd.OpenForm "ContactDetail_frm", acNormal, "", "1=0", , acDialog
Forms("ContactDetail_frm").T1_FirstName_fld.SetFocus

Opens to new form, focus not set to FirstName_fld. When I close form I get error "... Cannot find the referenced form 'ContactDetail_frm'."
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:39
Joined
May 21, 2018
Messages
8,529
Form opened just not on new.
If you want the form to open to a New record then use the DataMode argument
ACFORMADD

If you open a form ACDIALOG the code execution in the calling form stops until the Called form closes. So the next line of code will not run until after the form closes, and since it is closed it fails.

Do you need this to open ACDIALOG. You can make the form modal, dialog which makes it a pop up form. This will not effect code execution. There are times to do want the code to stop, but not they way you designed it.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:39
Joined
May 21, 2018
Messages
8,529
Also if you always want the form to go to the FirstName_fld then simply set that first in the form's Tab order. In design view, right click, select "tab order".
 

Sh8dyDan

New member
Local time
Today, 18:39
Joined
Dec 15, 2022
Messages
26

Sh8dyDan

New member
Local time
Today, 18:39
Joined
Dec 15, 2022
Messages
26
Also if you always want the form to go to the FirstName_fld then simply set that first in the form's Tab order. In design view, right click, select "tab order".
Ok, I'll try that.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:39
Joined
Feb 19, 2002
Messages
43,293
Good call, I had a typo. Error gone. Form opened just not on new.
If you would just tell us what you want to do rather than asking us to fix your code, we might be able to solve your problem.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:39
Joined
May 21, 2018
Messages
8,529
Tab order not going to work in my case
Out of curiousity, why not? If you always want the control to have focus and for some reason the tab order will not work then you can also set focus in the forms on load event.
me.SomeControl.Setfocus
 

Sh8dyDan

New member
Local time
Today, 18:39
Joined
Dec 15, 2022
Messages
26
Out of curiousity, why not? If you always want the control to have focus and for some reason the tab order will not work then you can also set focus in the forms on load event.
me.SomeControl.Setfocus
I have a Tab Group on ContactDetail_frm which always opens to tab 0 with focus set to contact search combo box...I can live with this fact, just would of liked it to open from this form when button click with Tab 0 FirstName_fld with focus.
 

Sh8dyDan

New member
Local time
Today, 18:39
Joined
Dec 15, 2022
Messages
26
This code works. Opens to ContactDetail_frm new record with T1_FirstName_fld with focus.
Code:
    DoCmd.OpenForm "ContactDetail_frm", acNormal, "", "1=0"
    On Error Resume Next
    DoCmd.SearchForRecord , "", acFirst, "[ContactID]=" & Nz(DMax("[ContactID]", Form.RecordSource), 0)
    Forms![ContactDetail_frm]!T1_FirstName_fld.SetFocus
 

Users who are viewing this thread

Top Bottom