Convert a Sub to a Function

M

mate

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

RobFMS

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

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
C

Chris Nebinger

Change the Sub to Function:

Public Sub Routine(arguments)

Public Function Routine(arguments)


Chris Nebinger
 
J

John Vinson

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top