Solved

run a code from another form

Posted on 2001-08-16
9
179 Views
Last Modified: 2012-06-22
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
Comment
Question by:asamir00
9 Comments
 
LVL 1

Expert Comment

by:krees
ID: 6393911
Well, I think for that you create a global sub in a module of your project.
0
 
LVL 1

Expert Comment

by:Sorklin
ID: 6393920
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
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6393937
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
 
LVL 1

Expert Comment

by:chaos_59
ID: 6393956
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Accepted Solution

by:
rkot2000 earned 15 total points
ID: 6394163
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
 

Expert Comment

by:DevParty
ID: 6394763
0
 

Expert Comment

by:costello
ID: 6395011
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
 

Expert Comment

by:ashraf_t
ID: 6395055
It is very easy just put the othe form name like form2 as :
form2.command1_Click
0
 

Expert Comment

by:costello
ID: 6395081
ashraf_t;

Please reread the comment I just made to DevParty.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now