我写了对于几个txtbox的判断,如下:
if text1.text="" then
msgbox "text1为空!"
end ifif text2.text="" then
msgbox "text2为空!"
end ifif text3.text="" then
msgbox "text3为空!"
end ifif text4.text="" then
msgbox "text4为空!"
end if
如果我四个txtbox都为空,它会一路的判断下来,我想让它发现第一个为空的,判断了,弹出消息框之后,就把焦点回到这个txtbox上让人进行填写,怎么办呢!

解决方案 »

  1.   

    if text1.text="" then 
    msgbox "text1为空!" 
    text1.sefocus
    exit sub
    end if
      

  2.   

    if text1.text="" then  
    msgbox "text1为空!"  
    text1.setfocus 
    exit sub 
    end if
      

  3.   

    你1F这样写当然会一路下来, 建议你使用数组 Text1(0) Text1(1) Text1(2) Text1(3)Private Sub Command1_Click()
       If SetTxtFocus(Text1) Then Exit Sub
       If SetTxtFocus(Text2) Then Exit Sub
       If SetTxtFocus(Text3) Then Exit Sub
       If SetTxtFocus(Text4) Then Exit Sub
    End SubPublic Function SetTxtFocus(ctl As TextBox) As Boolean
       SetTxtFocus = False
       If ctl.Text = "" Then
          ctl.SetFocus
          MsgBox ctl.Name & "为空!"
          SetTxtFocus = True
       End If
    End Function
      

  4.   

    cbm666大大,(ctl As TextBox) As Boolean是什么意思啊?
    还有,Function如何调用?
      

  5.   

    有很多种方法。例如可以利用 Validate 事件。Private Sub Text1_Validate(Cancel As Boolean)
        If Text1 = "" Then
            Cancel = True
            MsgBox "text1为空!"
        End If
    End SubPrivate Sub Text2_Validate(Cancel As Boolean)
        If Text1 = "" Then
            Cancel = True
            MsgBox "text2为空!"
        End If
    End Sub......注意将关闭窗体的按钮(不是的控制栏的X,而是你自己放的按钮)的 CauseValidate 属性设置为 False。当然,利用控件数组代码就更简单:
    Private Sub Text1_Validate(Index As Integer, Cancel As Boolean)
        If Text1(Index) = "" Then
            Cancel = True
            MsgBox "text" & index & "为空!"
        End If
    End Sub