# Solved: VBScript function return value



## madd74

I am attempting to write a small VBScript file (myfile.vbs) that has a function that does a data check and returns that value, however it is not returning the value. I am using this on a computer network, so I have no ability to install other 3rd party software, hense the reason I use a text file with the .vbs extension on it. Code looks like this:



Code:


function vTest(ftest)
  test = InputBox("RNA - Ring No Answer"&vbCrLf&"GBI   - Busy")
    if test="rna" or test="gbi" or test="vm" or test="gco" or test="rt" or test="tran" or test="cfn" then
      ftest=test
    else
      vTest(ftest)
    end if
  MsgBox test                                     'this will have a message box with one of the required values, rna, gbi, vm, etc
end function

MsgBox vTest(ftest)                               'this is blank

The MsgBox test inside the function will return the test varible (rna, gbi, etc). However the function call vTest(ftest) makes a blank pop up indicating the varible did not pass. I have attempted it without passing a varible "function vTest()", and still get the same results. No errors, but no varible leaves the function.


----------



## Ent

You don't seem to be using ftest in that function, so I'm inclined to suggest it's unnecessary (unless of course you plan to improve the function to use it later).
Remember, anything inside the parentheses is an argument to the function, used to modify how the function behaves.

To specify the result of a function, you don't change one of its arguments. In most languages you have some sort of RETURN statement; in VBS you actually just specify a variable having the same name as the function. For example


Code:


vTest=test


----------



## madd74

Well I tried a few other things, all with the same results. I actually attempted to not use a variable pass and I still had a problem for some reason. The following seems to go with what you say, and I still get the same results:



Code:


function vTest()
  test = InputBox("RNA - Ring No Answer"&vbCrLf&"GBI   - Busy")
    if test="rna" or test="gbi" or test="vm" or test="gco" or test="rt" or test="tran" or test="cfn" then
      test = test
    else
      vTest()
    end if
  MsgBox test         'This MsgBox returns whatever value you are able to pass woth the test = InputBox
end function

test=vTest()

msgbox test           'This MsgBox still returns null


----------



## Ent

Not 
test = test
That's about as useful as saying Dave is Dave.
vTest = test
That is, the function returns the value of test.


----------



## Rollin_Again

Here is a proper example of calling a function.

*Create the function*



Code:


Function vTest(vString As String)
  
Select Case LCase(vString)

Case "rna", "gbi", "vm", "gco", "rt", "tran", "cfn"

vTest = "YES"

Case Else

vTest = "NO"

End Select
                              
End Function

*Call the function from your subroutine
*


Code:


Sub TestSub()

vAnswer = vTest(InputBox("RNA - Ring No Answer" & vbCrLf & "GBI   - Busy"))

MsgBox(vAnswer)

End Sub

Rollin


----------



## madd74

Ent, yes, I know, I had test=test just because I wanted the function to make sure it was valid (the else statement). In actuality, it should be, "If not =... then vtest()" without an else, however dealing with about 9+ programming langages, I was not exactly sure how to do that with VBScript (i can in C+, and a few others)  I could have very well put a random comment in there, or, sucked it up and did research on how to "if not equal then" for VBScript.

Rollin, when I attempt your code I get an error message that there is an expected ')'. It was stating this on line 1 (the Function vTest(vString As String)) so I moved it to just Function vTest(vString). Once I did that, your code no longer gave an error, but unfortunately it did not do all I wanted.

The MsgBox would reply back with a "yes" or a "no". That tells me that it validated. Issue is, that I have to take whatever is put in (rna, gbi, vm, etc...) and use it later. Also, with yours, if someone puts in the wrong answer, it should call back to the same function to reask the question. So, if I put in "bob" it should pop up with the same message box, until I put in one of the "valid" answers.

So, for anyone I totally confused, this is what this function is suppose to do:

It asks you to input in a trouble type of rna, gbi, vm, gco, rt, tran, or cfn. These are the only valid options the user can put in. After this information is put in, later in the larger script, it will take the value entered and process depending on what was put in. My pseudo code would look like this:



Code:


declarations

functions

validate function asking for rna, gbi, vm, gco, rt, tran, or cfn

user input

more input

input calling the function asking for rna, gbi, vm, gco, rt, tran, or cfn

random crap

get some input and check some records, if the information is on a list, warn the user and stop the script

if variable from function was rna do this
if variable from function was gbi do this
if variable from function was vm do this
if variable from function was gco do this
if variable from function was rt do this
if variable from function was tran do this
if variable from function was cfn do this

At this point, everything works except what I originally had trouble with. The closest thing to work has been Rollin's suggestion of using Select Case, except his version changes the value put in to either yes or no, and I have to retain the value put in, as well as ask it again if it is false (one of the 7 on the list).


----------



## madd74

Ent said:


> Not
> test = test
> That's about as useful as saying Dave is Dave.


I would like to point out, this was the reason I failed geometry... because I thought it was pointless


----------



## madd74

Well, it is a little off track of what I wanted to do, however, running the full script got me what I wanted, which was selecting on of the specific options. I didn't even require a function for it:


Code:


Do
    TestCall = InputBox("Please enter the number that corresponds to your selection:" & Chr(10) & bullet &
"1)RNA - Ring No Answer" & bullet & "2)GBI - Gets busy in" & bullet & "3)VM - Goes to VM" & bullet &
"4)GCO - Gets cut off" & bullet & "5)RT - Ring trips" & bullet & "6)Trans - Transmission" & bullet &
"7)CFN - changing CFN" & Chr(10), "Enter 1 - 7")
    If TestCall = "" Then WScript.Quit  'Detect Cancel
    If IsNumeric(TestCall) Then If TestCall < 7 and TestCall > 0 Then Exit Do 'Detect value response.
    MsgBox "You must enter a numeric value between 1-7.", 48, "Invalid Entry"
Loop
MsgBox "The user chose :" & TestCall, 64, "Yay!"

The Do Loop checking input if it is numeric or not works, because then it is infinitely stuck in this loop until a number 1 - 7 is entered. I want to give thanks to those who took the time to look into this for me. Of course, if I want to enter a value in a function and bring it back universally (for VBS), I am going to have to look into that more


----------

