4 Replies - 1357 Views - Last Post: 13 December 2011 - 08:36 AM Rate Topic: -----

#1 ashub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-December 11

how to call a sub procedure?

Posted 05 December 2011 - 11:40 PM

i am using multiple forms.

i have defined a sub procedure in form1.
now how do i call that sub procedure from form2??

i have defined the sub procedure in public..
Is This A Good Question/Topic? 0
  • +

Replies To: how to call a sub procedure?

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: how to call a sub procedure?

Posted 05 December 2011 - 11:43 PM

Can't you copy and paste the sub code to form two and call it like that?
Was This Post Helpful? 0
  • +
  • -

#3 ashub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-December 11

Re: how to call a sub procedure?

Posted 05 December 2011 - 11:56 PM

View PostjimmyBo, on 05 December 2011 - 11:43 PM, said:

Can't you copy and paste the sub code to form two and call it like that?

No i cannot..
The sub procedure is related to an object.
Now that object is placed in form 1 and i cannot place it in form2.
So there should be some way 2 call that sub procedure from from2.
Was This Post Helpful? 0
  • +
  • -

#4 chuckjessup  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 380
  • Joined: 26-October 09

Re: how to call a sub procedure?

Posted 06 December 2011 - 01:59 PM

View Postashub, on 05 December 2011 - 10:56 PM, said:

View PostjimmyBo, on 05 December 2011 - 11:43 PM, said:

Can't you copy and paste the sub code to form two and call it like that?

No i cannot..
The sub procedure is related to an object.
Now that object is placed in form 1 and i cannot place it in form2.
So there should be some way 2 call that sub procedure from from2.



No!!!!!
If you copy and paste the code from one form to an other you will BLOAT you program!
Set the procedure attributes to either Public or Friend!

Then call the sub/function by the following code:
'From form2...'
MySub [arglistitem1 , arglistitem2...ect...] 'or'
somevariable=MyFunction([Argvalue1, ArgValue2...ect...]



Sample Code:
'Form1'
Public sub ImClicked()
 Msgbox "Im Clicked"
End sub

Friend Function Get5(numbersmorethan5 as long) as long
dim retval as long
randomize
retval=int(numbersmorethan5 * rnd)-1
get5=retval
end function



'Form2'
Form_Load()
Dim Me as long
form1.imclicked 'calls sub...'
me=form1.get5(17)
end sub



Public=open to all projects while app is running...
friend= open to items in the project or project group only (Forms and classes only)
Private= form only.

Hope this helps!

Jesse Fender
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: how to call a sub procedure?

Posted 13 December 2011 - 08:36 AM

View PostjimmyBo, on 06 December 2011 - 06:43 AM, said:

Can't you copy and paste the sub code to form two and call it like that?

Jesse is rightly against this idea. "Copy and paste programming" is a well-known "antipattern", leading to all sorts of bad things. For more information, see here.

Ashub, if you want to call a routine from another form, just refer to the form first. If you want to call a sub called MySub that's in Form1 from Form2, just say Form1.MySub instead of MySub or Me.MySub.

Jesse, I wouldn't use Friend except if you're working with Class or Control files. While you can use Friend in Forms, nothing outside the project can see them anyway. Well, unless they are in a class or control, but I wouldn't recommend that either. Also, I'm not sure your line "me=Form1.get5(17)" actually runs.

In any case, with respect, you have buried the salient concept of your example in a plethora of unrelated minutiae, causing said example to be of rather less pedagogical value than it otherwise might. (Are you reading this Thava? :) ) If I might suggest it, if you focus on conciseness and relevance your examples will be easier to follow.

This post has been edited by BobRodes: 13 December 2011 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1