Mirial
Mirial

Reputation: 163

Function Returning Boolean?

I have simple function in VBA and I would need to check whether or not it has been successfully performed. I do not know VBA much, so I have no idea whether or not its possible. I want to do something like this: bool X=MyFunction().

I am using VBA in the QTP descriptive programming. This does not work:

Function A as Boolean
   A=true
End Function

It says: Expected statement

But I cannot see any return type in my method etc.

Upvotes: 16

Views: 118779

Answers (5)

codeape
codeape

Reputation: 100904

In VBA, you set a function's return value by assigning to a variable with the same name as the function:

Function MyFunc() as Boolean
    MyFunc = True
End Function

Upvotes: 12

user688334
user688334

Reputation:

I suspect you may be using VBScript instead of VBA? If that's the case then VBScript doesn't state Type

this will work in VBScript

dim test,b
test = 1
b=false
msgbox ("Calling proc before function test=" & test)
msgbox("Calling proc before function b=" & b)
b = A(test)
msgbox ("Calling proc after function test=" & test)
msgbox("Calling proc after function b=" & b)

Function A(test)
test = test +1  
A=true
End Function

or in your example

Function A()    
A=true
End Function

Upvotes: 4

Serodis
Serodis

Reputation: 2112

There is no real way to check if a function worked in VBA. You must decide for yourself whether your function was successful. For example:

Function AFunction() as Boolean
    on error goto 1
    MyFunc = True
    AFunction = True
    Exit Function
    1
    AFunction = False
End Function

The above would let you know if the function failed. If it fails, it goes to the label '1' and then returns false, otherwise, it returns true.

If it isn't an 'error' you are looking for, then you must decide if the data returned or supplied is proper. One way of doing this is to return a specific value [error-code] which represents a failure.

Upvotes: 1

chamel
chamel

Reputation: 367

Well if you have access to your function declaration, you can set a bool return type for it and return true or false depending on the execution. Once your function returns a bool, your code will work.

Upvotes: 0

Rene de la garza
Rene de la garza

Reputation: 1269

function MyFunction() as Boolean
    .....
    .....
    MyFunction = True 'worked
end function

dim a as boolean = MyFunction()

Upvotes: 31

Related Questions