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 k开发者_StackOverflow社区now 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.
function MyFunction() as Boolean
.....
.....
MyFunction = True 'worked
end function
dim a as boolean = MyFunction()
In VBA, you set a function's return value by assign to a variable with the same name as the function:
Function MyFunc() as Boolean
MyFunc = True
End Function
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
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.
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.
精彩评论