6 Replies - 42819 Views - Last Post: 08 September 2006 - 10:13 PM Rate Topic: -----

#1 conclusionsend  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-September 06

Calling a function from a different form

Posted 06 September 2006 - 12:43 PM

Hi.
Is it possible to call a function in one form from a different form?
At the moment I have two forms called Edit and Start. I want to run a function called FileOpen in the Edit form from a control in the Start form.
I tried the code bellow but it doesn't work.
Edit.FileOpen


Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Calling a function from a different form

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Calling a function from a different form

Posted 07 September 2006 - 06:07 AM

The Function call you provided will work, but ensure that you set your function as Public and not Private.
Was This Post Helpful? 0
  • +
  • -

#3 conclusionsend  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-September 06

Re: Calling a function from a different form

Posted 07 September 2006 - 06:42 AM

View Postborn2c0de, on 7 Sep, 2006 - 06:07 AM, said:

The Function call you provided will work, but ensure that you set your function as Public and not Private.


Thanks man... I should have known that really :blink:
Was This Post Helpful? 0
  • +
  • -

#4 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Calling a function from a different form

Posted 07 September 2006 - 08:37 PM

View Postborn2c0de, on 7 Sep, 2006 - 06:37 PM, said:

The Function call you provided will work, but ensure that you set your function as Public and not Private.

Yes but while calling a function in a form module from another form module u say:
<variable>= <formname>.function()


Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Calling a function from a different form

Posted 08 September 2006 - 12:01 PM

View PostLouisda16th, on 8 Sep, 2006 - 09:07 AM, said:

View Postborn2c0de, on 7 Sep, 2006 - 06:37 PM, said:

The Function call you provided will work, but ensure that you set your function as Public and not Private.

Yes but while calling a function in a form module from another form module u say:
<variable>= <formname>.function()


Only if you're calling a Function (which in VB has to return a value).

You can omit the assignment to the variable in case you are calling a Subroutine.
Was This Post Helpful? 0
  • +
  • -

#6 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Calling a function from a different form

Posted 08 September 2006 - 09:07 PM

conclusionsend did say its a function.

This post has been edited by Louisda16th: 08 September 2006 - 09:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Calling a function from a different form

Posted 08 September 2006 - 10:13 PM

That type of function call is optional at times where the return value is unimportant.

For example, the Simple Msgbox Function
Msgbox "ASDF"


Can be called without accepting a return value. Msgbox is a function, and hell yea, it does return a value.

Only when you give parameters about the number of buttons in Msgbox(), the return value gains importance(which would indicate if OK or Cancel etc was pressed) and hence it is required to accept the return value or else VB will not compile.


And besides, when the function call is like this:
Form_name.Func
--or--
Call Form_name.Func


You do not need to accept a return value depending on its importance.

But it is mandatory to do it if your function call is:
Form_Name.Func()



Hope you get things clearly now.

And check this example out....Functions can also be declared and used like this:
Private Sub Command1_Click()
	Dim x As Byte
	
	x = func1
	' Default Return Value Zero
	
	func1	  ' Allowed
	Call func1 ' Allowed
	func1()	' Not Allowed
	x = func1() ' Allowed
	
	

	

End Sub

' This Declaration is ok.
Public Function func1()
MsgBox "Howwzat?"
End Function


This was based on the term 'function' in C/C++ where every user-defined block of code outside main() or other functions is a Function irrespective of whether it returns a value or not.

It doesn't matter because if the return value is required, all the callee function does is read the value of EAX register.
And if it doesn't, EAX is not read or overwritten after the function is called.
Was This Post Helpful? 1

Page 1 of 1