Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Can't pass back values in a subroutine?

    Quote Originally Posted by davegri View Post
    Either way the return is discarded, because a function called that way is treated like a sub, which has no return value.
    If you want to use a function's return value you do like this:

    Rslt = fcnGetIt
    or
    If fcnGetIt = "OK" then...
    So, now I'm really confused, often not knowing if one is talking about a sub or function. In any case, is this post all wet?


    https://www.accessforums.net/showthr...236#post498236

  2. #17
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    My solution for now

    This topic, for me anyways, quickly got out of hand. I just don’t have enough experience with errors and VBA yet to know the best way to handle errors. And there is quite a bit of disagreement between the experts. For other newbies that may be in the same boat, here is my temporary solution.

    While developing, I’ll add a generic error handler to all subs and functions. Later it can be easily removed if deemed not necessary in EVERY procedure as the bubble-up advocates suggest. First, I need to understand what kinds of errors there are and how best to handle each one. To do that, I can comment out the On Error GoTo line in the offending procedure to focus in on the exact offending line of code and fix it.

    One universal unanticipated error handler (for now, all errors are unanticipated) will msgbox display all. Once I understand better, the Select Case statements can be uncommented and coded.

    Code:
    Public Function ErrUnhandled(lngErr As Long, strError As String, strSubFunc As String) As Boolean
        MsgBox "Unanticipated error in procedure: " & strSubFunc & vbCrLf & lngErr & ": " & strError
        ErrUnhandled = True
    End Function
    
    Public Function FcnTest2()
        Dim Test As Single, MyError As Boolean
        On Error GoTo ErrCd
    
        Test = 3 / 0
        
    ExitCd:
        Exit Function
    
    ErrCd:
    '    Select Case Err
    '    Case 998, 999: command
    '    Case Else
           MyError = ErrUnhandled(Err, error, "FcnTest2")
    '    End Select
        Stop
        Resume ExitCd
    End Function

  3. #18
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    You can find lists of error codes on line, but not aware of any that group them by 'topic'
    for example
    https://fmsinc.com/MicrosoftAccess/E...scription.html

    think your ErrUnhandled needs to do a bit more. As a developer you may come across unhandled errors and fix them so as a developer tool, OK. But when your app goes to production, the expectation is that the function will never be called. However if it is called, you don't want to drop the user into the VBE (you can't anyway if the app is .accde). And you need to decide whether the error can be ignored so the user can carry on, or is fatal to the app and the only option is to close the app.

    You may also want the function to log the error in a log table and/or email the developer with the details

  4. #19
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    So, now I'm really confused, often not knowing if one is talking about a sub or function. In any case, is this post all wet?
    https://www.accessforums.net/showthr...236#post498236
    I was hoping for an explanation of how a sub can return a value if that's what you mean, because M$ documentation does not support that contention. If the meaning was to set a module level variable according to the code in a sub, then to me that is setting a variable value that is exposed to the calling code and is not the same as returning a value to a line statement in that code.

    Can you point out where the disagreement is if you were referring to something else? I'd like to review.
    Thanks.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #20
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I was hoping for an explanation of how a sub can return a value if that's what you mean, because M$ documentation does not support that contention
    the post in the link is trying to explain that you can assign a value to a byRef parameter. Applies to both subs and functions. But only functions can return a value that is assigned outside of the function. There are plenty of API's that do this - ones I use regularly would be using the RECT and POINTAPI types as parameters


    sub mysub(x as integer)

    function myfunction(x as integer)

    with code

    x=10

    will both assign the value of x to the variable passed as a parameter.

    Personally I rarely use subs since functions can do the same thing and more - such as being referenced in queries, called by ribbons and assigned to events.

  6. #21
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    So is it fair to say that a sub isn't really returning a value to the statement in the calling procedure - it's altering the value of a variable that was declared ByRef (which by definition, is a pointer to a space in memory)? All I can say is that the M$ documentation states that a sub cannot return a value to a calling procedure and that is what I've long believed. I think that is what twgonder is questioning, but not sure.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error using Excel Export routine
    By greatwhite in forum Programming
    Replies: 4
    Last Post: 05-10-2019, 12:30 PM
  2. Replies: 5
    Last Post: 09-06-2015, 12:06 PM
  3. Query naming error help?
    By josh21 in forum Queries
    Replies: 4
    Last Post: 05-14-2015, 03:10 AM
  4. Replies: 7
    Last Post: 01-08-2014, 10:36 AM
  5. Replies: 3
    Last Post: 02-09-2011, 07:43 AM

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