Exit current subroutine from called function

NealeBabb

Registered User.
Local time
Today, 08:33
Joined
Aug 1, 2012
Messages
20
Hi,
is there a was of calling a function at the start of a sub that will allow you to exit the sub from the function,

so if i were to have a database that requires logging in so when the form loads it uses the function CheckCurrentUser to see if the user has loged in, if not then it will make them login

Private Sub Form_Load()
CheckCurrentUser
Me.CurrentUser = CurrentUserName
End Sub

Function CheckCurrentUser()
If CurrentUserName = "" Then
For intIndex = Forms.Count - 1 To 0 Step -1
DoCmd.Close acForm, Forms(intIndex).Name, acSaveNo
Next intIndex
CurrentUserName = "Default"
DoCmd.OpenForm "Login", , , , , acDialog
End If
End Function


the problem i have is that if the current user name is empty then it will close down the form and open the login form, exactly how i want it, but it will still continue to run the code from the Form_Load sub causing errors

what i need is to be able to be able to exit the subroutine from the function if the current user name is empty

Any ideas

Thanks
 
NealeBabb, you can make the function return a Boolean value, if it is True then Exit the Sub else continue.. Something along the lines of..
Code:
Private Sub Form_Load()
    [COLOR=Blue]If[/COLOR] CheckCurrentUser [COLOR=Blue]Then Exit Sub[/COLOR]
    Me.CurrentUser = CurrentUserName
End Sub

Function CheckCurrentUser() [COLOR=Blue][B]As Boolean[/B][/COLOR]
    If CurrentUserName = "" Then
        For intIndex = Forms.Count - 1 To 0 Step -1
            DoCmd.Close acForm, Forms(intIndex).Name, acSaveNo
        Next intIndex
        CurrentUserName = "Default"
        DoCmd.OpenForm "Login", , , , , acDialog
        [COLOR=Blue]CheckCurrentUser = True
    Else
        CheckCurrentUser = False[/COLOR]
    End If
End Function
 
NealeBabb, you can make the function return a Boolean value, if it is True then Exit the Sub else continue.. Something along the lines of..
Code:
Private Sub Form_Load()
    [COLOR=Blue]If[/COLOR] CheckCurrentUser [COLOR=Blue]Then Exit Sub[/COLOR]
    Me.CurrentUser = CurrentUserName
End Sub

Function CheckCurrentUser() [COLOR=Blue][B]As Boolean[/B][/COLOR]
    If CurrentUserName = "" Then
        For intIndex = Forms.Count - 1 To 0 Step -1
            DoCmd.Close acForm, Forms(intIndex).Name, acSaveNo
        Next intIndex
        CurrentUserName = "Default"
        DoCmd.OpenForm "Login", , , , , acDialog
        [COLOR=Blue]CheckCurrentUser = True
    Else
        CheckCurrentUser = False[/COLOR]
    End If
End Function


This worked great, you are my saviour, thanks
 

Users who are viewing this thread

Back
Top Bottom