How to call/trigger a command button when opening an form (1 Viewer)

chrisjames25

Registered User.
Local time
Today, 07:54
Joined
Dec 1, 2014
Messages
401
Hi. I currentyl have an edit form in my database. I enter a batch no in a textbox and then click the edit command button and it loads all the data that im allowed to edit, makes comboxes eanbled etc.

This works great. I also have another form with a subform in it that is showing a summary of all data. WIthin the subform there is an edit button.

WHat i want this edit button to do is open my edit form, look in the subform and put the batch no in the batch no. text box of the edit form and simulate the clicking of the command button to populate all the data in the comboboxes etc.

I can manage to open the edit form from the subform and populate the batch no but I dont know how to call an event from another bit of code.

I considered just copying and pasting the command button codse into the on load event for the form but i dont want to trigger this event if I open the edit form the normal way rather than via the subform of another form. I could just create 2 edit forms and add the stuff to the on load event but this would make form maintenance messy if i want to add somethign to the edit form.

GUess im really looking for some help on how to call event from another form.

FOr clarity my actual forms are called:

Frm_BatchInput which has a subform in it called Frm_BatchINputSubForm

THe edit form is called Frm_BatchInputEdit and the command button i want to simulate clicking is called Cmd_EditPottingData_Click()

I found someting online that suggested the following:

Code:
Call YourButtonName_Click

BUt this fails when i run the code.

Any help on calling event would be massively appreciated.
 

moke123

AWF VIP
Local time
Today, 02:54
Joined
Jan 11, 2013
Messages
3,920
Did you replace "YourButtonName_Click()" with "Cmd_EditPottingData_Click()"?
 

chrisjames25

Registered User.
Local time
Today, 07:54
Joined
Dec 1, 2014
Messages
401
Did you replace "YourButtonName_Click()" with "Cmd_EditPottingData_Click()"?
Yep I did.

Wasnt sure whether i needed to add more reference to it as i am typing this in the subform code so do i need to reference the full frm name im opening with the cmd_editpottingdataclick?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:54
Joined
Oct 29, 2018
Messages
21,473
If you're talking about calling an event code from another form, try declaring the code stub as Public.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:54
Joined
Feb 28, 2001
Messages
27,186
By default, all event routines are PRIVATE, which means they are not visible outside of the form declaring them. Since Access implements sub-forms merely as forms with a special container (a sub-form control), that means you have two distinct forms open at once - and they can't see each other unless you allow it.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:54
Joined
May 21, 2018
Messages
8,529
1. If in your edit form you have
Code:
Private sub Cmd_EditPottingData_Click()
  'Code here
end sub
you have to make that public
Code:
Public Sub Cmd_EditPottingData_Click()
end sub
2. Normally people will instead pull the code out of the click event procedure and make a stand alone
Code:
Public Sub EditPottingData
  'code here
end Sub
Then the click event procedure stays as private but calls the public procedure
Code:
Private sub Cmd_EditPottingData_Click()
   call EditPottingData()
end sub

3. However code in a Form or report module is encapsulated so you can only call it from a reference to the class

Code:
dim Frm as access.form
docmd.openForm "Frm_BatchInputEdit", ....
set frm as forms("Frm_BatchInputEdit")
frm.EditPottingData

See discussion
 

chrisjames25

Registered User.
Local time
Today, 07:54
Joined
Dec 1, 2014
Messages
401
1. If in your edit form you have
Code:
Private sub Cmd_EditPottingData_Click()
  'Code here
end sub
you have to make that public
Code:
Public Sub Cmd_EditPottingData_Click()
end sub
2. Normally people will instead pull the code out of the click event procedure and make a stand alone
Code:
Public Sub EditPottingData
  'code here
end Sub
Then the click event procedure stays as private but calls the public procedure
Code:
Private sub Cmd_EditPottingData_Click()
   call EditPottingData()
end sub

3. However code in a Form or report module is encapsulated so you can only call it from a reference to the class
Code:
[CODE]dim Frm as access.form
docmd.openForm "Frm_BatchInputEdit", ....
set frm as forms("Frm_BatchInputEdit")
frm.EditPottingData
[/CODE]

See discussion
Hi, thanks for this. I think i understand it now. I will try and put this in and see if any issues occur. Makes a lot more sense than 1 hour ago :)

Stupid question but in your code what are the .... about?
Code:
[CODE]dim Frm as access.form

docmd.openForm "Frm_BatchInputEdit", ....

set frm as forms("Frm_BatchInputEdit")

frm.EditPottingData
[/CODE]
 

moke123

AWF VIP
Local time
Today, 02:54
Joined
Jan 11, 2013
Messages
3,920
the ... are nothing. they're there to represent any other arguments you may be using in the open form method.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:54
Joined
May 21, 2018
Messages
8,529
Ellipsis in writing show an omission of words, represents a pause, or suggests there’s something left unsaid. There is a lot of code I assume you have. I assume you are passing a wherecondition to edit form and then updating the edit form, but I do not know what that all is.

Here is a demo (see 5) that has a form calling another form's procedure "Helloworld". There are some examples of more difficult events handling. Trapping another forms events, raising custom events, using a single function to trap multiple events.
 

Attachments

  • Events Demo V1.accdb
    672 KB · Views: 86

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:54
Joined
Feb 19, 2002
Messages
43,275
Why are you not using master/child links to sync the subform to the mainform? This sync'ing should happen automagically.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:54
Joined
May 21, 2018
Messages
8,529
I do agree with @Pat Hartman here. Running code on another form is not unheard of, but it is pretty rare to need to do such a thing. This may be a result of a poor form or database design.
 

Users who are viewing this thread

Top Bottom