Setting code on Command Button

  • Thread starter Thread starter DS
  • Start date Start date
D

DS

I have a command button where I want to leave the OnClick Event Blank.
I want to set it from the previous form. If I open it from form1 the
the OnClick event is This.......
If I open it from form2 then the OnClck event is that. How would I do this?
Thanks
DS
 
DS said:
I have a command button where I want to leave the OnClick Event Blank.
I want to set it from the previous form. If I open it from form1 the
the OnClick event is This.......
If I open it from form2 then the OnClck event is that. How would I
do this? Thanks
DS

No need to leave the OnClick blank. Just put your various code routines in
their own functions or sub-routines and then have your Click event decide
which one of those to call. Assuming you use the OpenArgs property to
determine the calling form...

Select Case Me.OpenArgs
Case "Form1"
Call SomeSub
Case "Form2"
Call SomeOtherSub
End Select
 
Rick said:
No need to leave the OnClick blank. Just put your various code routines in
their own functions or sub-routines and then have your Click event decide
which one of those to call. Assuming you use the OpenArgs property to
determine the calling form...

Select Case Me.OpenArgs
Case "Form1"
Call SomeSub
Case "Form2"
Call SomeOtherSub
End Select
Looks good Rick, but how do you use the OpenArgs tocall the form?
Thanks
DS
 
DS said:
Looks good Rick, but how do you use the OpenArgs tocall the form?

OpenArgs has a dual personality. It is an optional argument that can be
used in the OpenForm method.

DoCmd.OpenForm "FormName",,,,,,"Some String Value"

It is also a property of a form that can be referenced by any code in that
form. It's just that the OpenArgs property will be Null (or a zero length
string perhaps) if the form is not opened with OpenForm having the OpenArgs
argument supplied.
 
Rick said:
OpenArgs has a dual personality. It is an optional argument that can be
used in the OpenForm method.

DoCmd.OpenForm "FormName",,,,,,"Some String Value"

It is also a property of a form that can be referenced by any code in that
form. It's just that the OpenArgs property will be Null (or a zero length
string perhaps) if the form is not opened with OpenForm having the OpenArgs
argument supplied.
Thanks Rick, On target as always!!
DS
 
You have two choices:

You could pickup the name of the "previous" form in the 2nd forms on-load
even.

strPrevoius = screen.ActiveForm.Name

note that you can pick the name of the previous form as above as long as you
have NOT yet exited the on-open, and even the on-load event of the form. The
instant the on-load routine is finished then above would course refer to the
current form.

"strPrevous" would course have to be a module level variable defined at the
start of the forms module code.

Then, in your button click code, you can go

if strPrevous = "form1" then
bla bal bal
end if

if strPrevous = "form2" then
bla bal alb
end if


Another approach is to simply "leave" the on-click event of the control
blank, and set what function you want the on-click event to turn. You would
do this in the "calling" code..

eg:

docmd.OpenForm "form B"
forms("formB").Contorls("myButtion").OnClick = "=MyCode1()"


This means you can at runtime "modify" what public function the button will
run.....

"mycode" can either be a public function in the forms module, or in a
standard code module...

Last, but not least, since I written a zillion applications, and never had
to do the above, my spider sense tells me there is another way to do this by
changing your design........

However, the above should work for you.....
 
Albert said:
You have two choices:

You could pickup the name of the "previous" form in the 2nd forms on-load
even.

strPrevoius = screen.ActiveForm.Name

note that you can pick the name of the previous form as above as long as you
have NOT yet exited the on-open, and even the on-load event of the form. The
instant the on-load routine is finished then above would course refer to the
current form.

"strPrevous" would course have to be a module level variable defined at the
start of the forms module code.

Then, in your button click code, you can go

if strPrevous = "form1" then
bla bal bal
end if

if strPrevous = "form2" then
bla bal alb
end if


Another approach is to simply "leave" the on-click event of the control
blank, and set what function you want the on-click event to turn. You would
do this in the "calling" code..

eg:

docmd.OpenForm "form B"
forms("formB").Contorls("myButtion").OnClick = "=MyCode1()"


This means you can at runtime "modify" what public function the button will
run.....

"mycode" can either be a public function in the forms module, or in a
standard code module...

Last, but not least, since I written a zillion applications, and never had
to do the above, my spider sense tells me there is another way to do this by
changing your design........

However, the above should work for you.....
Great thanks Albert the second one works even better than the first one
for me.
Thanks
DS
 
Back
Top