View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Per Jessen Per Jessen is offline
external usenet poster
 
Posts: 1,533
Default 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