Private Sub Form_Load()
Dim ctrls As Controls
Dim ctrl As Control
Set ctrls = Me.Controls
For Each ctrl In ctrls
If ctrl.ControlType = acTextBox Or ctrl.ControlType = acComboBox Then
ctrl.OnGotFocus = "=CtrlSetFocus()"
End If
Next
End Sub
Function CtrlSetFocus()
Dim ctrls As Controls
Dim ctrl As Control
Dim i As Integer, m As Integer
m = 100
Set ctrls = Me.Controls
For i = 0 To ctrls.Count - 1
'ControlType可根据需要进一步枚举,也可再套一层If用于排除非必填控件。
If ctrls(i).ControlType = acTextBox Or ctrls(i).ControlType = acComboBox Then
If IsNull(ctrls(i).Value) = True Then
If ctrls(i).TabIndex < m Then
Set ctrl = ctrls(i)
m = ctrls(i).TabIndex
End If
End If
End If
Next
If m <> 100 Then
ctrl.SetFocus
End If
End Function
[attach]48099[/attach]
[attach]48100[/attach]作者: Henry D. Sy 时间: 2012-1-13 22:11
视乎有点简单问题复杂化了作者: fnsmydyang 时间: 2012-1-14 11:38
其实正如版主所说,大可不必,在保存时检查一下就好了,有一点复杂化了,不过精神可佳。作者: Grant 时间: 2012-1-14 11:59
也是个好办法,如果能用数组控件会更简单些作者: 简 时间: 2012-1-14 13:13
诶,不是我要钻牛角尖,我之所以要求控件不能为空值,一方面涉及到输入人员的习惯,还有生产中的不确定性,另外一方面是根据输入值进行断号的检查,所以才不得不这样处理。
具体效果等我晚上传附件上来,也许你们就明白了。作者: JosephTan 时间: 2012-1-14 15:32
人人为我好啊作者: Henry D. Sy 时间: 2012-1-15 09:18
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then