Workbook flash prevent

Brass Contributor

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

7 Replies

@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
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

@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)

thanks i just made the adjustment now and it worked perfectly. my system developed an issue which i was able to fix yesterday.

Please i want to directly print each students report cards in sheet(JSS1RC). the students name are populated in combobox1 and the linked cell of the combobox is F2. i used the code below and it prompts save as before each printing. i want the macro to directly print. 

here's the code please help modify it to print directly:

Sub PrintReportCards()
Dim ws As Worksheet
Dim combobox As Object
Dim i As Integer

' Set the worksheet containing the ComboBox
Set ws = ThisWorkbook.Sheets("JSS1RC")

' Set the ComboBox
Set combobox = ws.OLEObjects("ComboBox1").Object ' Adjust ComboBox name if needed

' Loop through all items in ComboBox
For i = 0 To combobox.ListCount - 1
' Get the student name from the ComboBox
ws.Range("F2").Value = combobox.List(i)

' Print the worksheet
ws.PrintOut
Next i
End Sub

or if it can be modified to work for comboboxchange event but not prompting save for each students. thanks

 @JKPieterse 

This code should not trigger any save? Or are you printing to the print to PDF "printer" by any chance?

i don't understand sir, or probably the settings on my excel print page could cause the save as to always prompt?.

or can this occur when no printer is connected to the system?@JKPieterse 

Yes, if you have set print to pdf, the print command creates a pdf file and needs to know where to save it. Alternatively, you could have your code use the ExportAsFixedFormat method, which allows you to do the same, but give it the location for the file and the name of the file so you are no longer prompted for that information: https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat