ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   UserForm MsgBox problems (https://www.excelbanter.com/excel-programming/425890-userform-msgbox-problems.html)

jeq214

UserForm MsgBox problems
 
I have a userform that requests a password and opens up a specific sheet
depending on the password. My problem is when I enter in an incorrect
password, a MsgBox appears, but it won't go away when I hit Ok. It'll go
through the count and exit the userform. Here's my code:

Private Sub cmdOk_Click()

Dim lCount As Long

'Allow 3 attempts at password
For lCount = 1 To 3
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit For
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit For
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
End Select
Next lCount

If lCount = 4 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
Exit Sub
End If
End Sub

JLGWhiz[_2_]

UserForm MsgBox problems
 
I don't see where you are requesting the password. It needs to be inside
the for next loop if you want an opportunity to change the input, otherwise
the for next loop is evaluating the original input three time and will keep
displaying the message box.




"jeq214" wrote in message
...
I have a userform that requests a password and opens up a specific sheet
depending on the password. My problem is when I enter in an incorrect
password, a MsgBox appears, but it won't go away when I hit Ok. It'll go
through the count and exit the userform. Here's my code:

Private Sub cmdOk_Click()

Dim lCount As Long

'Allow 3 attempts at password
For lCount = 1 To 3
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit For
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit For
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
End Select
Next lCount

If lCount = 4 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
Exit Sub
End If
End Sub




Per Jessen

UserForm MsgBox problems
 
Hello

The code never allow the user to reenter a password.

Try this, just note the variable declaration has to be pasted at the very
top of the userform module.

Dim PasswordCount As Integer
Private Sub cmdOk_Click()

PasswordCount = PasswordCount + 1
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit Sub
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit Sub
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
Me.txtPassword.Value = ""
Me.txtPassword.SetFocus
End Select

If PasswordCount = 3 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
End If
End Sub

Regards,
Per

"jeq214" skrev i meddelelsen
...
I have a userform that requests a password and opens up a specific sheet
depending on the password. My problem is when I enter in an incorrect
password, a MsgBox appears, but it won't go away when I hit Ok. It'll go
through the count and exit the userform. Here's my code:

Private Sub cmdOk_Click()

Dim lCount As Long

'Allow 3 attempts at password
For lCount = 1 To 3
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit For
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit For
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
End Select
Next lCount

If lCount = 4 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
Exit Sub
End If
End Sub



jeq214

UserForm MsgBox problems
 
Thanks, that worked.

"Per Jessen" wrote:

Hello

The code never allow the user to reenter a password.

Try this, just note the variable declaration has to be pasted at the very
top of the userform module.

Dim PasswordCount As Integer
Private Sub cmdOk_Click()

PasswordCount = PasswordCount + 1
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit Sub
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit Sub
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
Me.txtPassword.Value = ""
Me.txtPassword.SetFocus
End Select

If PasswordCount = 3 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
End If
End Sub

Regards,
Per

"jeq214" skrev i meddelelsen
...
I have a userform that requests a password and opens up a specific sheet
depending on the password. My problem is when I enter in an incorrect
password, a MsgBox appears, but it won't go away when I hit Ok. It'll go
through the count and exit the userform. Here's my code:

Private Sub cmdOk_Click()

Dim lCount As Long

'Allow 3 attempts at password
For lCount = 1 To 3
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit For
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit For
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
End Select
Next lCount

If lCount = 4 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
Exit Sub
End If
End Sub




Mike H

UserForm MsgBox problems
 
Hi,

This is a bit more awkward then it seem. You never give the user a chance to
enter a second password so it evaluates the same incorrect one 3 times. Nor
can you use a counter to count the attempts because it resets every time you
click the button. this rather messy way works. I use a1 of sheet 1 to keep
the count but in practice use an out of the way cell

Private Sub cmdOk_Click()
Select Case Me.Txtpassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
Sheets("Sheet1").[A1] = Sheets("Sheet1").[A1] + 1
End Select
If Sheets("Sheet1").[A1] = 3 Then
Answer = MsgBox("No more tries", vbCritical)
Sheets("Sheet1").[A1] = ""
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
End If
End Sub


Mike



"jeq214" wrote:

I have a userform that requests a password and opens up a specific sheet
depending on the password. My problem is when I enter in an incorrect
password, a MsgBox appears, but it won't go away when I hit Ok. It'll go
through the count and exit the userform. Here's my code:

Private Sub cmdOk_Click()

Dim lCount As Long

'Allow 3 attempts at password
For lCount = 1 To 3
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit For
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit For
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
End Select
Next lCount

If lCount = 4 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
Exit Sub
End If
End Sub


Mike H

UserForm MsgBox problems
 
Per

passwordcount will reset to zero every time the sub terminates and goes back
to the userform so will never reach 3.

Mike

"Per Jessen" wrote:

Hello

The code never allow the user to reenter a password.

Try this, just note the variable declaration has to be pasted at the very
top of the userform module.

Dim PasswordCount As Integer
Private Sub cmdOk_Click()

PasswordCount = PasswordCount + 1
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit Sub
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit Sub
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
Me.txtPassword.Value = ""
Me.txtPassword.SetFocus
End Select

If PasswordCount = 3 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
End If
End Sub

Regards,
Per

"jeq214" skrev i meddelelsen
...
I have a userform that requests a password and opens up a specific sheet
depending on the password. My problem is when I enter in an incorrect
password, a MsgBox appears, but it won't go away when I hit Ok. It'll go
through the count and exit the userform. Here's my code:

Private Sub cmdOk_Click()

Dim lCount As Long

'Allow 3 attempts at password
For lCount = 1 To 3
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit For
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit For
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
End Select
Next lCount

If lCount = 4 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
Exit Sub
End If
End Sub




Mike H

UserForm MsgBox problems
 
Apologies, of course it will increment

Mike

"Mike H" wrote:

Per

passwordcount will reset to zero every time the sub terminates and goes back
to the userform so will never reach 3.

Mike

"Per Jessen" wrote:

Hello

The code never allow the user to reenter a password.

Try this, just note the variable declaration has to be pasted at the very
top of the userform module.

Dim PasswordCount As Integer
Private Sub cmdOk_Click()

PasswordCount = PasswordCount + 1
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit Sub
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit Sub
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
Me.txtPassword.Value = ""
Me.txtPassword.SetFocus
End Select

If PasswordCount = 3 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
End If
End Sub

Regards,
Per

"jeq214" skrev i meddelelsen
...
I have a userform that requests a password and opens up a specific sheet
depending on the password. My problem is when I enter in an incorrect
password, a MsgBox appears, but it won't go away when I hit Ok. It'll go
through the count and exit the userform. Here's my code:

Private Sub cmdOk_Click()

Dim lCount As Long

'Allow 3 attempts at password
For lCount = 1 To 3
If Me.txtPassword.Value = vbNullString Then 'Cancelled
Unload Me
Exit Sub
End If
Select Case Me.txtPassword.Value
Case "Sheet2"
ActiveWorkbook.Sheets("Sheet2").Visible = True
Unload Me
Exit For
Case "Sheet3"
ActiveWorkbook.Sheets("Sheet3").Visible = True
Unload Me
Exit For
Case Else
Answer = MsgBox("Password incorrect", vbCritical)
End Select
Next lCount

If lCount = 4 Then 'They use up their 3 attempts
ActiveWorkbook.Sheets("Sheet1").Activate
Unload Me
Exit Sub
End If
End Sub





All times are GMT +1. The time now is 08:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com