做了空文本框,并将焦点放在另一个上 [英] Made null textbox and set focus on another
问题描述
我正在使用下面的代码,在退出初始化文本框时检查另一个文本框值,如果为空,则将初始值设为null,并将焦点设置为最终文本框。
I'm using code below, for check another textbox value when exiting initial textbox and if it null, making initial one null and set focus on final textbox.
但是我给出这个错误:运行时错误'-2147467259(80004005)':非特定错误。
But i give this error: Run-time error'-2147467259(80004005)': Unspecific error.
当我发表评论这行( txtTimeUnit = vbNullString
),宏代码正常工作。
when i made comment this line (txtTimeUnit = vbNullString
), macro code works correctly.
该行的命令有什么问题,请帮我纠正代码。 b
$ b
whats the problem of that line's command and please help me correcting code.
Private Sub txtTimeUnit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If txtStartDate.Text = vbNullString Then
txtTimeUnit = vbNullString
txtStartDate.SetFocus
Exit Sub
End If
End Sub
推荐答案
像我说你的代码一样。这是一个例子
Like I said your code works. Here is an example
Private Sub txtTimeUnit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If txtStartDate.Text = vbNullString Then
txtTimeUnit.Text = vbNullString
txtStartDate.SetFocus
Exit Sub
End If
End Sub
唯一不行的方法是当有另一段代码设置 Cancel = True
时。例如
The only way it will not work is when there is another piece of code which is setting the Cancel = True
. For example
Private Sub txtTimeUnit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(txtTimeUnit.Text, Range("intTable[Time Unit]"), 0)) Then
Cancel = True
End If
If txtStartDate.Text = vbNullString Then
txtTimeUnit.Text = vbNullString
txtStartDate.SetFocus
Exit Sub
End If
End Sub
为了防止这种错误,您可以使用布尔值
变量
To prevent such kind of errors you can use a Boolean
Variable
Dim boolOnce As Boolean
Private Sub txtTimeUnit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If boolOnce = False Then
boolOnce = True
If IsError(Application.Match(txtTimeUnit.Text, Range("intTable[Time Unit]"), 0)) Then
Cancel = True
End If
Else
boolOnce = False
End If
If txtStartDate.Text = vbNullString Then
txtTimeUnit.Text = vbNullString
txtStartDate.SetFocus
Exit Sub
End If
End Sub
这篇关于做了空文本框,并将焦点放在另一个上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!