UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Exiting A Function From A Sub, Access 2016    
 
   
arpirnat
post Jan 2 2018, 11:55 AM
Post#1



Posts: 106
Joined: 25-June 16



So I am hoping this is an easy fix that I am just thinking too hard about...

I have a function that runs off of my database. This function calls a couple sub's during the process if certain criteria are met. On one of these sub's there is the potential that the entire process needs to be aborted. I can do an 'Exit Sub' to end that sub but the function continues to run. I am unable to exit the function from the sub.

The only thing I can think of is to assign a value somewhere and have the function check for this value to exit the function once the sub is finished running.

In the code below in the FindCorrectConsult sub it is terminating the sub, however the code after the 'call' continues to run.


CODE
  If InStr(Reflection.gettext(0, 0, 23, 80), "Select: Quit//") > 0 Then
                        Call FindCorrectConsult
                    End If
                        
    Select Case Forms!frmTestForm.OptionCase
    Case 1 'ADD COMMENT
        .transmit "CM"
        .transmit Chr(13)
        .transmit Forms!frmTestForm.Form!ConsultAddComment.Value
        .transmit Chr(5)
        .transmit "y"
        .transmit Chr(13)
        Pause (0.45)
        Call AddSigners
Go to the top of the page
 
theDBguy
post Jan 2 2018, 11:59 AM
Post#2


Access Wiki and Forums Moderator
Posts: 71,915
Joined: 19-June 07
From: SunnySandyEggo


Hi,

Yes, you have a couple of ways to tell the function to terminate from the Sub:

1. Convert the Sub into a Function, so you can return a value, or

2. Add an argument to the Sub using the ByRef keyword. You should then be able to check the value of the argument variable from the Function after the Sub completed running.

Hope it helps...

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Microsoft Access MVP | Access Website | Access Blog | Email
Go to the top of the page
 
GroverParkGeorge
post Jan 2 2018, 12:12 PM
Post#3


UA Admin
Posts: 32,357
Joined: 20-June 02
From: Newcastle, WA


Of those two approaches, I'd lean more to converting the sub to a function with a Boolean datatype and calling it like this:

If booFindCorrectConsult = False then Exit Function

and in the renamed sub-->function

Set it this way:

Public Function FindCorrectConsult as Boolean
FindCorrectConsult = False
...
lines of code
---
FindCorrectConsult =True

End Function



--------------------
Go to the top of the page
 
arpirnat
post Jan 2 2018, 12:43 PM
Post#4



Posts: 106
Joined: 25-June 16



Thank you, adding the Boolean worked like a charm smile.gif

This post has been edited by arpirnat: Jan 2 2018, 12:53 PM
Go to the top of the page
 
GroverParkGeorge
post Jan 2 2018, 12:46 PM
Post#5


UA Admin
Posts: 32,357
Joined: 20-June 02
From: Newcastle, WA


We're glad you found a solution.

Continued success with your project.

--------------------
Go to the top of the page
 
arpirnat
post Jan 2 2018, 12:53 PM
Post#6



Posts: 106
Joined: 25-June 16



One thing though is at the end of the 'FindCorrectConsult function if it errors and needs to end everything is displays a message 'There do not seem to be any Consults'.
For some reason it is sending this twice, once during the function as it is supposed to and once after it exits the main function. I put a separate message box in the code after the Call FindCorrectConsult and it sends in order 'There do not seem to be any Consults', 'Test', 'There do not seem to be any Consults'... but I do not have that line anywhere in the main code, only in the FindCorrectConsult function. The next line after the Call is the


CODE
If FindCorrectConsult = False Then Exit Function

so I can't tell where this second msgbox is spawning from.
This post has been edited by arpirnat: Jan 2 2018, 12:54 PM
Go to the top of the page
 
GroverParkGeorge
post Jan 2 2018, 01:24 PM
Post#7


UA Admin
Posts: 32,357
Joined: 20-June 02
From: Newcastle, WA


You only need to include this once.

If you are doing this:

CODE
Call booFindCorrectConsult
If FindCorrectConsult = False Then Exit Function


you can remove the first one. It's redundant because the second line runs it again to return the value.

--------------------
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    19th April 2018 - 10:24 PM