Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

run a code from another form

Posted on 2001-08-16
Last Modified: 2012-06-22

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

Question by:asamir00

Expert Comment

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

Expert Comment

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)


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


Expert Comment

ID: 6393937
The simplest way to do this is to go into form2 and change the

Private Sub Command1_Click()


Public Sub Command1_Click()

and then from form1 just do


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.

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

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.


Accepted Solution

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.

Expert Comment

ID: 6394763

Expert Comment

ID: 6395011

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'

Community Support Moderator @ Experts-Exchange

Expert Comment

ID: 6395055
It is very easy just put the othe form name like form2 as :

Expert Comment

ID: 6395081

Please reread the comment I just made to DevParty.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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