View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default SetFocus in a custom dialog

I would use both textbox_change procedures to decide to enable/disable the login
button. I wouldn't move focus to that button at all.

Option Explicit
Private Sub CommandButton1_Click()
MsgBox "Login was clicked"
Unload Me
End Sub
Private Sub CommandButton2_Click()
MsgBox "Cancel was clicked"
Unload Me
End Sub
Private Sub TextBox1_Change()
Call EnableLoginCheck
End Sub
Private Sub TextBox2_Change()
Call EnableLoginCheck
End Sub
Private Sub UserForm_Initialize()
With Me.CommandButton1
.Caption = "Login"
.Default = True
.Enabled = False
.TakeFocusOnClick = False
End With
With Me.CommandButton2
.Caption = "Cancel"
.Cancel = True
.TakeFocusOnClick = False
End With
Me.TextBox1.SetFocus
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Call CommandButton2_Click
End If
End Sub
Sub EnableLoginCheck()

Dim Ok As Boolean
Ok = True
If Trim(Me.TextBox1.Value) = "" Then
Ok = False
End If
If Trim(Me.TextBox2.Value) = "" Then
Ok = False
End If

Me.CommandButton1.Enabled = Ok

End Sub


wrote:

Hi

I have a logon dialog created with two fields (username & password)
and two buttons (login and cancel).

I'm checking with an event if the fields are empty and enable the
login-button only then.

Private Sub txtPwd_AfterUpdate()
If Me.txtUsr.Value < "" And Me.txtPwd.Value < "" Then
Me.cmdLogin.Enabled = True
Me.cmdLogin.SetFocus
Else
Me.cmdLogin.Enabled = False
Me.txtPwd.SetFocus
End If
End Sub

If both fields (Username and Password) are different than "" then I
enable the login-button. And I also want to set the focus on this
login button. But although I do this using the Me.cmdLogin.SetFocus
the focus is set to the next item (the cancel-button). I've tried to
set it on the password-field or any other object, but it just isn't
setting the focus on the login-button.

What am I doing wrong?

Any hint appreciated.

Matthias


--

Dave Peterson