Returning a string from a function

  • Thread starter Thread starter fergusor
  • Start date Start date
F

fergusor

Can anyone help. I'm trying to get a year returned from a function bu
it keeps coming back empty.

I'm calling it, sending a string... in this case '2004'

Call messageYearResponse(yearResponse)

Then the function is supposed to return xYear as 2004... instead it'
coming back empty.

Private Function messageYearResponse(ByVal yearResponse As String)
Dim xYear

If yearResponse = "" Then
Exit Function
Else
If IsNumeric(yearResponse) Then
xYear = CInt(yearResponse)
Else
msgbox ("Must be numeric"), vbOKOnly
Resume
End If
End If

End Functio
 
To return a value from a function, you need to set the
name of the function to the value. Add in this line:

messageYearResponse = xYear

just before the End Function line.

HTH
Helen
 
Thanks Helen. I've put that line in but how do I use the returned value
when I leave the function?
 
fergusor,
You either need to supply a return value for your function:
Private Function messageYearResponse(ByVal yearResponse As String) As
Integer
'Also, assign the return value:
messageYearResponse=MyReturnValue
End Function

or pass the input ByRef and use a sub
Private Sub messageYearResponse(ByVal yearResponse As String, ByRef myOutput
as Integer)

NickHK
 
fergusor,
Something like:
Dim MyReturnValue as integer

MyReturnValue =messageYearResponse(yearResponse)

If MyReturnValue=False Then 'As 0=False
MsgBox "Must be ....."
Else
'Process.....

End If

Private Function messageYearResponse(ByVal yearResponse As String) As
Integer
Dim RetVal as integer

If yearResponse is a year then
RetVal = convert to integer(yearResponse)
else
RetVal=0
end if

messageYearResponse=RetVal

End Function

You may want to read up on the limitations of the IsNumeric function to see
if it will handle all inputs. Some error handling would be a good idea also.

NickHK


fergusor > said:
Thanks Helen. I've put that line in but how do I use the returned value
when I leave the function?


Nick Cranham said:
fergusor,
You either need to supply a return value for your function:
Private Function messageYearResponse(ByVal yearResponse As String) As
Integer
'Also, assign the return value:
messageYearResponse=MyReturnValue
End Function

or pass the input ByRef and use a sub
Private Sub messageYearResponse(ByVal yearResponse As String, ByRef myOutput
as Integer)

NickHK
 
Back
Top