Question

I am writing a code in MS Access VBA, which is as follows:

Private Sub Form_Load()
    MsgBox "loggedIn = " + CStr(loggedIn)
    If (loggedIn = 1) Then
    Else
        Exit Sub
    End If
End Sub

I want to decide whether to load the form or not based on loggedIn variable. If loggedIn variable is 1, form shall be loaded. If the same is not 1, form shall not be loaded.

Issue that I am facing is that, whatever I do in ELSE part of the code above, I am not able to stop the form from loading.

How can I achieve this?

Pls comment if any additional information is required.

Thank you.

Was it helpful?

Solution

If the loggedIn value is available at form open, you could cancel the form open event.

Private Sub Form_Open(Cancel As Integer)
    Cancel = Not (loggedIn = 1)
End Sub

If the value of loggedIn is not available until the form's load event, you can close the form.

Private Sub Form_Load()
    If loggedIn <> 1 Then
        DoCmd.Close acForm, Me.Name
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top