LITTLEBLACKDOG.COM Forum Index LITTLEBLACKDOG.COM

 
LWD LWD   FAQ FAQ   Memberlist Memberlist   Usergroups Usergroups   Active Topics Active Topics   Register Register  
  Profile Profile   Log in to check your private messages Log in to check your private messages   Log in Log in  
  Who is Online Who is Online   Image Gallery Image Gallery   Chat Chat   Search Search  
  LWDGear       LBDGear  

View next topic
View previous topic
Post new topic     Reply to topic   LITTLEBLACKDOG.COM Forum Index » Code Warriors
Author Message
Daftlad
Stray Dog


Joined: 14 Oct 2003
Posts: 7
Location: England

Post Posted: Thu Oct 23, 2003 5:25 am   Post subject: VB problem Reply with quote Back to top  

I know I should really know but ...... I dont.........

could some one give me a pretty straight forward guide on how to pass variables from a from into a module with an example.. in the meantime I will be looking for examples

so thank you very much !! Very Happy

_________________
"Its not the size of the wand, its the magic in the stick.."
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Lycander
Lead Dog
Lead Dog


Joined: 24 May 2002
Age: 25
Posts: 12198
Location: The Constitution State

Post Posted: Thu Oct 23, 2003 5:54 am   Post subject: Reply with quote Back to top  

First, difference between a function and a sub routine is that a function can take parameters whereas a sub routine can't. I guess you prolly already know this.

Text taken from VBScript doc:

Kinds of Procedures
In VBScript there are two kinds of procedures; the Sub procedure and the Function procedure.

Sub Procedures
A Sub procedure is a series of VBScript statements, enclosed by Sub and End Sub statements, that perform actions but don't return a value. A Sub procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure). If a Sub procedure has no arguments, its Sub statement must include an empty set of parentheses ().
The following Sub procedure uses two intrinsic, or built-in, VBScript functions, MsgBox and InputBox, to prompt a user for some information. It then displays the results of a calculation based on that information. The calculation is performed in a Function procedure created using VBScript. The Function procedure is shown after the following discussion.
Code: Select all

 Sub ConvertTemp()
    temp = InputBox("Please enter the temperature in degrees F.", 1)
    MsgBox "The temperature is " & Celsius(temp) & " degrees C."
 End Sub

Function Procedures
A Function procedure is a series of VBScript statements enclosed by the Function and End Function statements. A Function procedure is similar to a Sub procedure, but can also return a value. A Function procedure can take arguments (constants, variables, or expressions that are passed to it by a calling procedure). If a Function procedure has no arguments, its Function statement must include an empty set of parentheses. A Function returns a value by assigning a value to its name in one or more statements of the procedure. The return type of a Function is always a Variant.
In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. When the function is called from the ConvertTemp Sub procedure, a variable containing the argument value is passed to the function. The result of the calculation is returned to the calling procedure and displayed in a message box.
Code: Select all

 Sub ConvertTemp()
     temp = InputBox("Please enter the temperature in degrees F.", 1)
     MsgBox "The temperature is " & Celsius(temp) & " degrees C."
 End Sub

 Function Celsius(fDegrees)
     Celsius = (fDegrees - 32) * 5 / 9
 End Function

Getting Data into and out of Procedures
Each piece of data is passed into your procedures using an argument. Arguments serve as placeholders for the data you want to pass into your procedure. You can name your arguments anything that is valid as a variable name. When you create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure. Any arguments are placed inside these parentheses, separated by commas. For example, in the following example, fDegrees is a placeholder for the value being passed into the Celsius function for conversion:
Code: Select all

 Function Celsius(fDegrees)
    Celsius = (fDegrees - 32) * 5 / 9
 End Function

To get data out of a procedure, you must use a Function. Remember, a Function procedure can return a value; a Sub procedure can't.

Using Sub and Function Procedures in Code
A Function in your code must always be used on the right side of a variable assignment or in an expression. For example:
Code: Select all

 Temp = Celsius(fDegrees)
or
 MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees."

To call a Sub procedure from another procedure, you can just type the name of the procedure along with values for any required arguments, each separated by a comma. The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses.
The following example shows two calls to the MyProc procedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing.

Call MyProc(firstarg, secondarg)
MyProc firstarg, secondarg

Notice that the parentheses are omitted in the call when the Call statement isn't used.


That's it for now, I'll be back with more in a bit, gotta have a cig break Wink

_________________
To the top of hunger mountain
I found my solitary ways
Where I could live on nuts and honey
And take my shelter in a cave
View user's profile Send private message
Lycander
Lead Dog
Lead Dog


Joined: 24 May 2002
Age: 25
Posts: 12198
Location: The Constitution State

Post Posted: Thu Oct 23, 2003 6:27 am   Post subject: Reply with quote Back to top  

Ok now for a real example:

Create a form and in it create:

1. A text box, the default name is Text1 or something like that.

2. A push button.

Double click the push button and it will take you to the form's code view with premade code to handle the On Click event for that button. Hold that thought for a moment as we start a module.

Add a blank module to your project then insert this code into it:
Code: Select all

Function test1(arg1 As String)
    MsgBox arg1, vbOKOnly, "Title"
End Function

Only functions take in variables or parameters (whatever terminology you prefer) and return a value. Here, we're defining a function "test1" and having it take in a variable who's data type is a string of text. All we're doing with it is taking the input string of text and displaying it back in a normal Windows message box.

No go back to the code view of your form into the sub routine that handles the button press. Here's the code for the event handler:
Code: Select all

Private Sub Command1_Click()
    Call test1(Text1.Text)
End Sub

"Command1" was the default name of the button when I first created it, but can change. The same goes for the text box. Here it's just called "Text1", if you change you must update the code as well. What we're doing here is taking the text that was typed into that text box and passing it to the "text1" function. "Text" is a property of the text box that allows you to access the actual text in it.

So to sum it up, once you've setup your function, you would use the "Call" keyword to call it from within your form, then pass it variables from wherever you may get it from. Just make sure the data type matches. If you want to pass text use "String" or numbers with "Integer" whichever the case may be. The variable name between the parentasis ( ) next to the function name is how you'd refer to them from within the function.

_________________
To the top of hunger mountain
I found my solitary ways
Where I could live on nuts and honey
And take my shelter in a cave
View user's profile Send private message
Display posts from previous:   
Post new topic     Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
phpBB SEO
All times are GMT - 8 Hours

Help us keep advertisements off this site. Donate today!