Run Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have what is probably a silly question. Is there a way to run code that
applies to form1 from form2? I have a delete button in form1 that has a lot
of code in it that is specific to form1 like me.controlname = "test" etc. I
have a button in form2 that opens form1 to the last record. Now I need a way
to run the same code that's in the delete button on form1 without needing to
click the delete button. I copied the code to a module and tried to call it
from form2 with this code:

DoCmd.Close
DoCmd.OpenForm "NewPartInputfrm", , , , acFormEdit
DoCmd.GoToRecord , , acLast
Forms![NewPartInputfrm]![Model#] = "test"
Call DeleteNewRecord

This code works until Call DeleteNewRecord. Maybe I have the function
arguments wrong. I have: Public Function DeleteNewRecord().

All I want to do is once the button in form2 is clicked for the code behind
my delete button on form1 to run. Any ideas? Thanks.
 
Alex said:
I have what is probably a silly question. Is there a way to run code that
applies to form1 from form2? I have a delete button in form1 that has a lot
of code in it that is specific to form1 like me.controlname = "test" etc. I
have a button in form2 that opens form1 to the last record. Now I need a way
to run the same code that's in the delete button on form1 without needing to
click the delete button. I copied the code to a module and tried to call it
from form2 with this code:

DoCmd.Close
DoCmd.OpenForm "NewPartInputfrm", , , , acFormEdit
DoCmd.GoToRecord , , acLast
Forms![NewPartInputfrm]![Model#] = "test"
Call DeleteNewRecord

This code works until Call DeleteNewRecord. Maybe I have the function
arguments wrong. I have: Public Function DeleteNewRecord().


When calling a method (public function or sub) in a class
(or form/report) module, you must do it through the class
object. The syntax would be:

Call Forms!form1.DeleteNewRecord
 
Barry said:
Form_Form1.DeleteNewRecord


Be careful of that syntax Barry. Form_formname.method
refers to the class, not necessarily to any specific
instance of the class (i.e. if there are multiple instances
of the form).

If the procedure (method) uses static or module level
variables, then, since the Form_ construct refers to the
"default" instance, you may get the wrong values.

It's just safer to use the Forms!formname.method syntax (or
frm.method in the case of multiple instances of the form) so
you which instance you are calling.
 
Back
Top