• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

run a code from another form

Hi,

how can i run a code from another form , ex:

on click form1.command1 let it run form2.command1
but not by typing the same code of form2.command1_click in form1.command1_click

thanks
asamir
0
asamir00
Asked:
asamir00
1 Solution
 
kreesCommented:
Well, I think for that you create a global sub in a module of your project.
0
 
SorklinCommented:
You have to make sure that you have loaded the form 2 as an object in form one.  

For example, If form 1 loads form 2, create a module level global form2 object and when you load form2, set this global equal to the form2 object.

Then later in the code (when form1.command1 has been activated), you can call globalvariable.command1_Event (like _click) and it will activate.

If the forms are created independently from each other (by another main form, lets say), then there should be a global scope object assigned to form2.  When you originally create form2, the global would be assigned to it. ie.

dim frm as Form2

set frm = new Form2 (this will create a new instance of the form2 object)

frm.show

etc...
Then any form can access Form2 and its children by using the global frm.



0
 
Z_BeeblebroxCommented:
The simplest way to do this is to go into form2 and change the

Private Sub Command1_Click()

to

Public Sub Command1_Click()

and then from form1 just do

Form2.Command1_Click

This will work as long as you only have one instance of Form2, ie you loaded it by doing Form2.show or load Form2. If Form2 has not been loaded in this manner, it will be loaded when you call the Command1_Click method. If you want to have multiple instances of Form2, so you are doing

Set frm = new Form2

then you need to take Sorklin's approach.

Zaphod.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
chaos_59Commented:
You can access the subs or functions of another form or module by making the sub type Public. Normally the subs of click events and such are created as private subs. If you change it to public you can access it from other forms and modules.

0
 
rkot2000Commented:
chaos is correct, but with commandbutton
you can use value property to "click on command

form2.command1.value = true - will fire command1_click event.
0
 
DevPartyCommented:
0
 
costelloCommented:
DevParty

you are relatively new to this site so I would ask you to please read the guidelines on comments vs answers at the bottom of this page and on http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp 

It is not considered good practice to lock questions in this way, as this places these questions in the locked section and reduces the visibility of the question to other experts. It also tends to put pressure on the questioner to accept the proposed answer. In future, please do not post "answers" except in trivial questions which have one and only one answer such as what is 1+1. I know that is a bit simplistic but I am sure you get the idea.

Having said that I hope that you continue to contribute to this site and that your experience proves useful to many others in future.
I am also rejecting your answer, for above reasons.

By the way, as you can see, something went wrong with your comment anyway, it just shows 'No Text'

costello
Community Support Moderator @ Experts-Exchange
0
 
ashraf_tCommented:
It is very easy just put the othe form name like form2 as :
form2.command1_Click
0
 
costelloCommented:
ashraf_t;

Please reread the comment I just made to DevParty.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now