Exit from Public Sub (1 Viewer)

Design by Sue

Registered User.
Local time
Today, 08:10
Joined
Jul 16, 2010
Messages
652
I have a code that needs to be run on click of every button on my switchboard. It checks to be sure that the user has made a selection in a subform on the switchboard that the entire database then works on. If there is no selection there are all kinds of errors. I have created the code as a public sub and referred to it first in each of the button codes. Problem is that I need to "exit sub" if the criteria of the public sub is met (i.e. no selection is made in the subform) - and putting exit sub in the public sub only exits to return to the button code. Is there someway to stop the code from proceeding to the rest of the code in the private sub from within a public sub?

Thanks!
 

jdraw

Super Moderator
Staff member
Local time
Today, 11:10
Joined
Jan 23, 2006
Messages
15,385
Where do you want the logic to go? If someone hasn't selected a prerequisite value, then what?

Do you have some sort of data flow or control flow diagram with options , conditions and what to do if something is missing?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:10
Joined
Aug 30, 2003
Messages
36,127
I would change the sub to a function and have it return a Boolean (True/False). Then in the code that calls it

Code:
If FunctionName() = True Then
  Do one thing
Else
  Do another
End If
 

Design by Sue

Registered User.
Local time
Today, 08:10
Joined
Jul 16, 2010
Messages
652
What I have is working almost. It is as follows:

Code:
Public Function NoBrand()

If DCount("Brand", "UserBrandsTBL", "Include = " & True & "") = 0 Then
    MsgBox "You must select a brand before proceeding.", vbExclamation, "Select Brand"
    Me.UserBrandsFRM.SetFocus
    Exit Function
    
End If
   

End Function

If a brand is not selected, I need the code to stop at the end of this code. What is happening now is the exit function takes the code back to the button code and runs that too - thus opening the form for which the button runs. I need it to stop at this point (with the exit function) and not open the form. I could put this code on each and every button and use exit sub before the end if but am trying to us a public function to keep the code cleaner.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:10
Joined
Aug 30, 2003
Messages
36,127
I would change to this and call it like I mentioned:

Code:
Public Function NoBrand() As Boolean

If DCount("Brand", "UserBrandsTBL", "Include = " & True & "") = 0 Then
  MsgBox "You must select a brand before proceeding.", vbExclamation, "Select Brand"
  Me.UserBrandsFRM.SetFocus
  NoBrand = False
Else
  NoBrand = True
End If

End Function
 

Design by Sue

Registered User.
Local time
Today, 08:10
Joined
Jul 16, 2010
Messages
652
Figured it out - had to pass a value for NoBrand to the sub and then do an If statement there. - as I see you posted pbaldy - thanks
 
Last edited:

Users who are viewing this thread

Top Bottom