Solved Open Form B from Form A button click and after Form B opens call Sub on From B

Sh8dyDan

New member
Local time
Today, 10:36
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:
In general this sounds like a bad idea. Can you tell us what business function you are attempting to implement?
 
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:

 
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.
 
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.
 
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
 
Again, I do not recommend that design, But this shows it is possible to call another forms event procedure.
 
does the form open. Are all those names spelled correctly? is the first name text box really called FirstName_Fld?
 
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.
 
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'."
 
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.
 
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".
 
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.
 
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.
 
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
 
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.
 
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

Back
Top Bottom