Convert a Sub to a Function

  • Thread starter Thread starter mate
  • Start date Start date
A function returns a value, a subprocedure does not.

Example of a subprocedure
---------------------------------------------
Public Sub Acme()

Dim x as integer
x = 3
Msgbox Prompt:="Value of X = " & x

End Sub

In your code, just call the subprocedure by name, such that:

Public Sub Test()

' This will run the above subproc and display a message box
Acme

End Sub



Example of a function
---------------------------------------------
Public Function Acme() as Integer

Dim x as integer
x = 3

Acme = x

End Function

In your code, just call the function, such that:

Public Sub Test()

Dim intResults as Integer

intResults = Acme()
Msgbox Prompt:="The result from the function is: " & intResults

End Sub


HTH

--
Rob

FMS Professional Solutions Group
http://www.fmsinc.com/consulting

Software Tools for .NET, SQL Server, Visual Basic & Access
http://www.fmsinc.com

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
Change the Sub to Function:

Public Sub Routine(arguments)

Public Function Routine(arguments)


Chris Nebinger
 
How do you convert a sub to a function? your help is much
appreciated. thankyou, mate.

Open it in the VBA editor; change the line

[Private|Public] Sub procedurename([arguments])

to

[Private|Public] Function procedurename([arguments]) [AS datatype]

where the bracketed text is optional.

Ordinarily a Function will return a value - if so, specify the
datatype of that value (e.g. AS INTEGER), and somewhere within the
function add a line

procedurename = <some value>
 
Back
Top