By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

how to call subs on another form in the same tab control?

P: 5
I have a form 'frmHome'. It has a tab control 'tabCtrl'. The tab control has two forms: 'frmContacts' and 'frmProjects'.

On the contacts form I can see the projects a person is involved with. On the Projects form I can see the people associated with a project.

I want to dbl-click on a project from the contacts form, and have it take me to that project on the projects form. I want to similarly click a person on the projects form, and have it take me to that person on the contacts form.

So far, I can dbl-click on a project in the contacts form, and the code will change the tab control to the appropriate page with the projects form. Also, there is a public sub on the projects page which will set the current record to the value of a global variable. I tested that sub with a command button and it works great.

BUT - I can't figure out how to call that public sub from the other form. I can't figure out the proper syntax to reference it.

Help? Thanks.
Jan 30 '12 #1

✓ answered by Stewart Ross

From VBA code you can refer to a public sub or function in another open form (frmProjects in this case)as follows:

Expand|Select|Wrap|Line Numbers
  1. Form_frmProjects.YourSubName

More generally, to assign the value of a function on another form to a variable, and to call a sub:

Expand|Select|Wrap|Line Numbers
  1. SomeVar = Form_YourFormName.YourFunctionName([Arg1], [Arg2], ..., [ArgN])
  2. Form_YourFormName.YourSubName [Arg1], [Arg2], ..., [ArgN]
This will only work for public subs or functions; those declared as private are not accessible outside the scope of their own form. After you type the dot separator Intellisense will show you the properties and methods available to you. Your public sub or function should show on the Intellisense list if all is well.

-Stewart

Share this Question
Share on Google+
12 Replies


100+
P: 759
You lost me .
Jan 30 '12 #2

NeoPa
Expert Mod 15k+
P: 31,494
You should find enough reference material in Referring to Items on a Sub-Form to help with this. If not then try to explain what you need further in clear terms.
Jan 30 '12 #3

Expert Mod 2.5K+
P: 2,545
From VBA code you can refer to a public sub or function in another open form (frmProjects in this case)as follows:

Expand|Select|Wrap|Line Numbers
  1. Form_frmProjects.YourSubName

More generally, to assign the value of a function on another form to a variable, and to call a sub:

Expand|Select|Wrap|Line Numbers
  1. SomeVar = Form_YourFormName.YourFunctionName([Arg1], [Arg2], ..., [ArgN])
  2. Form_YourFormName.YourSubName [Arg1], [Arg2], ..., [ArgN]
This will only work for public subs or functions; those declared as private are not accessible outside the scope of their own form. After you type the dot separator Intellisense will show you the properties and methods available to you. Your public sub or function should show on the Intellisense list if all is well.

-Stewart
Jan 30 '12 #4

NeoPa
Expert Mod 15k+
P: 31,494
While all that Stewart says is correct, if you want to refer to items within a closed list such as this, and I'm assuming you want to refer to frmProjects from withing frmContacts, you have another option, which is to refer to it relationally.

I don't actually know the names of your subform controls on your main form, but if they were called sfmContacts and sfmProjects then referring to a procedure MyProcedure found in frmProjects from code in frmContacts could be handled by saying :
Expand|Select|Wrap|Line Numbers
  1. Me.Parent.sfmProjects.Form.MyProcedure
or even :
Expand|Select|Wrap|Line Numbers
  1. Me.Parent.sfmProjects!MyProcedure
Jan 30 '12 #5

P: 5
Thanks, everyone. Stewart's answer did the trick: Form_frmProjects.PublicSubName

NeoPa, I couldn't get your syntax to work - that's similar to the other things I tried before coming here (which also didn't work). I've done that sort of thing many times before, but never when the forms are on a tab control. I think the tab control confuses the syntax somehow. In the past I've emulated a tab control just by having buttons on the forms - and that seemed easier in many ways than the tab control is turning out to be.

At any rate, thanks for the help.
Jan 31 '12 #6

NeoPa
Expert Mod 15k+
P: 31,494
Tab controls don't actually effect the referencing at all. They confuse mostly by people thinking they need to include it as some part of the reference.

Anyway, I'm sure you're happy with Stewart's solution - and it's something that's very useful to understand anyway (The full name of the object modules themselves).
Jan 31 '12 #7

P: 5
So, from a syntax standpoint, I should just consider the two tabbed forms as if they were subforms on the main form?

Expand|Select|Wrap|Line Numbers
  1. Instead of this:
  2. Main form
  3.   |-- tab ctrl
  4.         |--- form1
  5.         |--- form2
  6.  
  7.  
  8. I should think this?
  9. Main form
  10.   |--- subform1
  11.   |--- subform2
Jan 31 '12 #8

Expert Mod 2.5K+
P: 2,545
Forms on a tab control are subforms of the main form itself - the tab control does not affect their referencing at all, as NeoPa said. This is as you have noted in the second part of your post above.

-Stewart
Jan 31 '12 #9

P: 5
Thank you both. That clarifies a lot.

One more question: When the main form is open, are the subforms (the forms on the tab control) also considered 'open'?

One of the options I tried early on gave me an error saying that I couldn't reference controls on the projects form unless it was open.
Jan 31 '12 #10

Expert Mod 2.5K+
P: 2,545
The subforms are indeed open - but they are not open in the same way they would be if they were opened on their own outside of the main form.

When you open a main form, the subforms are forms embedded as controls within the main form itself. If you were to check that, say, your form called frmProjects is loaded you will find that it is not. If you attempt to access its controls as if it is a run-time error will occur.

To access the controls within one of your subforms when the main form is open you have to refer to the subform control's form property, as detailed in the article hyperlinked by NeoPa in post #3. There are many (equivalent) ways to do so, so this example is only one of the possibilities:

Expand|Select|Wrap|Line Numbers
  1. Forms("frmHome")!frmProjects![Your control name]
-Stewart
Jan 31 '12 #11

P: 5
Now I get it! NeoPa's linked article confused me before because I wasn't thinking of the forms on the tab control as subforms. Now that I re-read the article it makes a lot more sense.

Thanks again.
Jan 31 '12 #12

NeoPa
Expert Mod 15k+
P: 31,494
Everything Stewart said :-)

I'm pleased to hear things make more sense now. I never felt it was necessary before, as logically they have so little effect, but I may revisit the article and explain explicitly that tab controls don't effect the referencing of items 'contained' within them at all.

PS. I've updated it now. I must admit though, I found the explanation of how sub-forms are handled (within Subform controls) already clearly stated in the introduction. No updates were required for that part at least.
Jan 31 '12 #13

Post your reply

Sign in to post your reply or Sign up for a free account.