我正在尝试在禁用的文本框Txttravel
上触发事件GotFocus()。我希望如果我们想要编辑这个文本框 - 我们必须输入密码,然后启用文本框,我们就可以编辑它了。帮助实现它。我试过下面的代码。但是当文本框处于禁用状态时,这并不是一种琐事。
Private Sub Txttravel_GotFocus()
Dim Password As String
Password = InputBox("Enter password to edit this field:", "Password")
If Password = "Cloudbu@hcl" Then
Txttravel.Enabled = True
Me.Range("A1").Select
End If
Txttravel.Enabled = False
End Sub
答案 0 :(得分:2)
首先,只有当控件可见并启用时,才能获得焦点,如Microsoft here所述。
作为替代方案,如果密码错误且未禁用文本框,我建议您只在表单中选择其他控件。
尝试将代码更改为此
Private Sub Txttravel_Enter()
Dim Password As String
Password = InputBox("Enter password to edit this field:", "Password")
If Password = "Cloudbu@hcl" Then
Txttravel.SetFocus
Else
'Select another control in your form here
TextBox2.SetFocus
End If
End Sub
正如KostasK在评论中提出的那样,你可以锁定文本框。
Private Sub Txttravel_Enter()
Dim Password As String
Password = InputBox("Enter password to edit this field:", "Password")
If Password = "Cloudbu@hcl" Then
Txttravel.Locked = False
Else
Txttravel.Locked = True
End If
End Sub
注意:请务必将此代码放在表单中:表格上的右侧clic>查看代码