Results 1 to 5 of 5
  1. #1
    Cecil is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2011
    Location
    Dallas, Texas
    Posts
    11

    RETURN Command?

    Does Access VBA use a RETURN command anywhere, in order to return a value back to the procedure which called a user-defined function for example?



    I am looking at code which has no RETURN mVar commands anywhere. I see what appears to be GLOBAL declarations of variables with the Dim command and then those variables are given values in various procedures or functions and although the value isn't RETURNed, it seems that by being GLOBAL, the value is defined successfully and use throughout.

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    if you are returning the value you set the value of the function before you exit it

    i.e.

    Code:
    Sub Main()Dim ReturnValue
    ReturnValue = ProductCalc(4, 5)
    Debug.Print ReturnValue
    End Sub
    
    
    Function ProductCalc(x As Integer, y As Integer)
    ProductCalc = x * y
    End Function

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    It does, but only when used with a GoSub statement in the same procedure.

    A function gets the value to be returned through an assignment statement:

    Function MyFunction(...parameter list...) as FunctionType
    .
    .
    . Function code
    '
    MyFunction = Value to be Retuned

    End function

    If the function needs to compute values in addition to the "main" return values, you can do that through the parameters, or by using Global variables as you have seen.

  4. #4
    Cecil is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2011
    Location
    Dallas, Texas
    Posts
    11
    It seems unnatural to me to make everything Global variables just to pass a value from one function to another. Everything I have been taught tells me not to do that.

  5. #5
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    You don't have to make it a global function at all

    Code:
    Private Sub Command0_Click()
    Dim smsg As String
    smsg = "THE RESULT IS " & OMGMULT(4, 5)
    Debug.Print smsg
    End Sub
    
    
    Private Function OMGMULT%(x As Integer, y As Integer)
    Dim dProd As Double
    dProd = x * y
    OMGMULT = dProd
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 03-29-2015, 07:42 PM
  2. Replies: 1
    Last Post: 09-12-2014, 06:09 AM
  3. Replies: 9
    Last Post: 08-19-2014, 12:41 PM
  4. Pause code until return from call command
    By trevor40 in forum Programming
    Replies: 1
    Last Post: 03-08-2014, 05:08 AM
  5. Replies: 1
    Last Post: 07-27-2010, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums