VBA. True boolean state does not match
This is probably a really stupid question, but I cannot find a way to get around it ... I have defined a global variable called safe_mode and is boolean. On one of my subnets, I need to check its value and give it a new value accordingly. The code below will provide you with more information.
Private Sub Dev_Mode_Click()
Dim pass As String
If safe_mode = False Then
pass = InputBox("Password")
If pass <> "admin_rights" Then
Sheets("Database").Visible = xlSheetVeryHidden
Sheets("NewFile").Visible = xlSheetVeryHidden
Sheets("LandingPage").Dev_Mode.Caption = "Admin"
MsgBox ("Invalid Password")
safe_mode = False
MsgBox (safe_mode)
Exit Sub
Else
Sheets("Database").Visible = True
Sheets("NewFile").Visible = True
Sheets("LandingPage").Dev_Mode.Caption = "Log Off"
safe_mode = True
MsgBox (safe_mode)
Exit Sub
End If
End If
If safe_mode = True Then
Sheets("Database").Visible = xlSheetVeryHidden
Sheets("NewFile").Visible = xlSheetVeryHidden
safe_mode = False
Sheets("LandingPage").Dev_Mode.Caption = "Admin"
MsgBox ("Disconnect successful")
MsgBox (safe_mode)
Exit Sub
End If
End Sub
Safe_mode is assigned correctly. In other words, I become true when in admin mode and False when logged in. However, the condition If safe_mode = true is not met. I am getting prompted for the password even when I log out, which doesn't make sense as this code only runs when safe_mode = false and the variable returns true in the MsgBox. If the password is entered correctly, when trying to exit the variable, it remains true. The only way to return false is by omitting the password. Does anyone know about this?
Thank! D.
source to share
so I think your variable is empty. It is not clear to me when you insert the value into safe_mode, e. d. When you reopen your workbook, safe_mode will be cleared, so maybe you need a sub that starts in the workbook and sets the safe_mode value? The second option is to store the value in a hidden VBA tab.
I modified my code a little so you can test it (to check if safe_mode is not safe):
Public safe_mode As Boolean
Private Sub Dev_Mode_Click()
Dim pass As String
If IsEmpty(safe_mode) = True Then
MsgBox("Cannot check if admin mode")
Else
If (safe_mode = False) Then
pass = InputBox("Password")
If pass <> "admin_rights" Then
Sheets("Database").Visible = xlSheetVeryHidden
Sheets("NewFile").Visible = xlSheetVeryHidden
Sheets("LandingPage").Dev_Mode.Caption = "Admin"
MsgBox ("Invalid Password")
safe_mode = False
MsgBox (safe_mode)
Exit Sub
Else
Sheets("Database").Visible = True
Sheets("NewFile").Visible = True
Sheets("LandingPage").Dev_Mode.Caption = "Log Off"
safe_mode = True
MsgBox (safe_mode)
Exit Sub
End If
ElseIf (safe_mode = True) Then
Sheets("Database").Visible = xlSheetVeryHidden
Sheets("NewFile").Visible = xlSheetVeryHidden
safe_mode = False
Sheets("LandingPage").Dev_Mode.Caption = "Admin"
MsgBox ("Disconnect successful")
MsgBox (safe_mode)
Exit Sub
End If
End If
End Sub
source to share