Forum Discussion

Muhammadiyya's avatar
Muhammadiyya
Brass Contributor
Sep 16, 2024

Workbook flash prevent

Help with the best strategy to prevent workbook flash before Login form displays

7 Replies

  • JKPieterse's avatar
    JKPieterse
    Silver Contributor

    Muhammadiyya I'm afraid you are giving us too little information to be of real help. Kindly provide us with:

    • A clear explanation of what your problem is exactly
    • Code you possibly already have that demonstrates the problem
    • Even better, a sample workbook with instructions on how to replicate the issue
    • Muhammadiyya's avatar
      Muhammadiyya
      Brass Contributor
      Thanks my workbook has a login form to enable user access the workbook.
      The login form displays and if the user entered the correct login details, workbook access will be granted but each time I open the work, a sheet flashes before the login form is displayed and that's what I seek help to prevent.
      A sample of my project has been sent to your PM thanks
      • JKPieterse's avatar
        JKPieterse
        Silver Contributor

        Muhammadiyya 

        Make sure that your workbook contains a new worksheet called "Login" and change the Thisworkbook code to this:

         

         

         

        Option Explicit
        Public LoggedIn As Boolean
        
        Private Sub Workbook_AfterSave(ByVal Success As Boolean)
            If LoggedIn Then
                HideOrSHow False
            End If
        End Sub
        
        Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
            HideOrSHow True
        End Sub
        
        Private Sub Workbook_Open()
            Application.ScreenUpdating = False
            Application.DisplayAlerts = False
            frmLogin.Show
        End Sub
        
        Sub HideOrSHow(justLogin As Boolean)
            Dim sh As Worksheet
            For Each sh In Worksheets
                If LCase(sh.Name) = "login" Then
                    If justLogin Then
                        sh.Visible = True
                    End If
                Else
                    If justLogin Then
                        sh.Visible = xlSheetVeryHidden
                    Else
                        sh.Visible = xlSheetVisible
                    End If
                End If
            Next
        End Sub

         

         

        In the code behind the userform, make these changes:

         

         

            ' Check if the entered credentials match
            If txtUserID.Text = userID And txtPassword.Text = password Then
                MsgBox "Login successful!", vbInformation
                Unload Me  ' Close the login form
        'Next two lines are new
                ThisWorkbook.LoggedIn = True
                ThisWorkbook.HideOrSHow False
                ' Show the workbook and the HOME sheet
                With ThisWorkbook
                    .Windows(1).Visible = True
                    .Sheets("HOME").Visible = xlSheetVisible
                    .Sheets("HOME").Activate
                End With
        
                Application.ScreenUpdating = True
                Application.DisplayAlerts = True
            Else
                MsgBox "Invalid User ID or Password", vbCritical
                txtUserID.SetFocus  ' Set focus back to User ID textbox
            End If

         

         

        Finally, do not hide the application window (remove any Application.Visible = false)