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!

Is there a way to click a button on one from from another form?

Status
Not open for further replies.

irethedo

Technical User
Feb 8, 2005
429
0
0
US
I have a form in my database named 'OrderMenu_frm' that allows a user to match items from a sales order.

The data from the sales order is populated onto the OrderMenu_frm if the user clicks a "Get New Order" button
on this form.

The user then matches sales order items on this form that are to be configured into a record and clicks a "Match Selected"
button on this form which places the matched items as a record in a table called Total_Items_tbl.

When a match is performed the items that were matched are removed from the form to avoid duplicate items being matched.

Once the user is finished matching all items from the sales order they click a Verify Matched Items button on this form
which opens a continuous form called "SalesOrder_frm" which displays the contents of the Total_Items_tbl table.

From this "SalesOrder_frm" continuous form, the user can edit any of the records displayed. Once finished the
user can click one of two buttons (Add to Database or Discard).

Sorry for the long description and here is the question:

For the Discard button, I want to close this "SalesOrder_frm" continuous form but with the 'OrderMenu_frm' still
open is there a way to automatically run the "Get New Order" button code on the 'OrderMenu_frm' so it populates the
order data back onto this form?

I was reading about setting the Control Source property of a control as =[SalesOrder_frm].Form![Discard_btn] but the Control Source property
does not appear to be available for a button control where I would attempt to apply this to the "Get New Order" button

I appreciate suggestions on how to do this...

Thanks

 
I would create a simple form with a command button that pops up a msgbox(). Then create another form with a command button that you want to call the command button code from first. You can then add code to the new form command button with something like:
Code:
Forms!frmFirstForm.cmd..._Click

You might have more time and interest in doing this than me at the moment.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Thanks Duane-

That method would add a couple additional messages boxes and I am wondering if might it be cleaner for the user if I was to add the same code under my Discard button as I
have under my Get New Order button on the first form and then do a requery of the first form before closing the form with the Discard button.

Can a requery command be sent to another form?

 
Why not move the code outside the forms with the required arguments if any from the forms? This makes the code stand on its own and not depend on any form.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
You can requery another open form by;
Forms!YourFormName.requery
 

I tried the following but it doesn't seem to have any affect on form OrderMenu_frm:

Code:
Forms!OrderMenu_frm.Requery

DoCmd.Close
End Sub
 
Can you post the complete code so we can see the event from where it is called?
To verify this works I made 2 forms "frm1" and "frm2". Bound to the same data. I open both forms and then make changes in form 1. In frm1 I have this code.

Private Sub Form_Close()
Forms!frm2.Requery
End Sub

When I close frm1 it requeries frm2 and you see then see the changes.
 
Thank you MajP and Duane for your suggestions.

Turns out I messed up as the parts on the form that I wanted to requery were subforms...

so once I remembered that I have to handle them separately I simply put the following into the
close event of the form and all is well:

Code:
 Forms!OrderMenu_frm.PCList_frm.Requery 
Forms!OrderMenu_frm.LicenseList_frm.Requery 
Forms!OrderMenu_frm.SPNoteList_frm.Requery]
 
To more directly answer the question...

No you can not run a button on another form. Events on forms are private which means they can't be called.

However, you can call a private event in a module, including a form's class module with a public procedure in the module.

Then it is a matter of calling the procedure... I think a fully qualified form reference and the procedure name is the right syntax since it is a class module and therefore a method of the instance.

As to whether you put the code in the public procedure called by the event or the code in the event called by the public procedure is a style choice to me (mine would be to call the private procedure in the public one so as not to confuse changes by less accomplished programmers in the future and break my code). That said there are compelling arguments for putting some code outside the module altogether for reuse on multiple forms. The opposing argument would be that by putting all the code on the form, you maintain object portability and if you copy it to another system it is more likely to work.

 
Sorry to be picky, but there is some wrong terminology here and not exactly true. There is a big difference between an Event and an Event Handler (Event Procedure). The event procedure is the thing that listens for and handles the event. You cannot make a button physically click (raise the click event) from an external module, but you can call the code in its event procedure. Event Procedures are only private by default when using the VBE, but there is no reason you cannot make them public and call them from outside the form.
The below event procedure can be called from outside the form module using the syntax
Forms!YourFormName.Command5_Click
Code:
Public Sub Command5_Click()
     MsgBox "Hello World"
End Sub
Events are not called they are Raised. Event Handlers (Event Procedures) handle a raised event. You can make your own event and Raise it. Here is an example. In Form2 I create an event called a HelloWorld event. I raise that event after a message box pops up.
Code:
Public Event HelloWorld()
Public Sub Command5_Click()
  MsgBox "Hello World"
  RaiseEvent HelloWorld
End Sub
Now in Form1 I can write the event procedure to handle the HelloWorld event
Code:
Public WithEvents frm2 As Form_Form2
Private Sub Form_Load()
  Set frm2 = Forms!Form2
End Sub
Private Sub frm2_HelloWorld()
  MsgBox "Form 2 Said Hello World"
End Sub
Now in form2 when the Command5_Click procedure executes it raises an event. Form1 handles that event and will pop up a message box. However, there is no way to Raise this event external to Form2. Probably more useful than making custom events is handling other Form events. So Form1 can do something when you do something of Form2.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top