循环文本框直到找到空字符串

时间:2015-09-11 17:51:20

标签: vba ms-access access-vba ms-access-2010

这是我的代码:

With me
If Len(.txtFactContact1) Or Len(.txtFactFonction1) Or Len(.txtFactTel1) 
Or  Len(.txtFactPosteTel1) Or Len(.txtFactCell1) 
Or Len(.txtFactCourriel1) Or Len(.txtFactNote1) Or Len(.txtFactContact2)
Or Len(.txtFactFonction2) Or Len(.txtFactTel2) Or Len(.txtFactPosteTel2)
Or Len(.txtFactCell2) Or Len(.txtFactCourriel2) Or Len(.txtFactNote2) <>
0 Then 
If MsgBox("Les Données Saisi seront perdus, Désirez-vous continuer?", vbExclamation Or vbYesNo, "Continuer?") = vbYes Then
        .Undo
     Else
        MsgBox "no"
     End If
 End If
End With

它位于“取消”按钮上,因此当在这些文本框中输入数据时,它会宣传您将丢失该数据。

如何在开头减少Len()代码?

3 个答案:

答案 0 :(得分:1)

在评论中,您表示要检查所有表单的文本框。所以你不必关心文本框名称。您可以简单地遍历所有这些内容,看看是否有Value Len&gt; 0.

Private Sub cmdCancel_Click()
    Dim ctl As Control
    With Me
        For Each ctl In .Controls
            If ctl.ControlType = acTextBox Then
                If Len(ctl.Value) > 0 Then
                    If MsgBox("Les Données Saisi seront perdus, Désirez-vous continuer?", _
                            vbExclamation + vbYesNo, "Continuer?") = vbYes Then
                        .Undo
                    Else
                        MsgBox "no"
                    End If ' MsgBox
                    Exit For
                End If ' Value
            End If ' ControlType
        Next
    End With
End Sub

答案 1 :(得分:0)

假设您正在检查表单上每个文本框控件的长度,您可以迭代它们并检查这样的长度。

Dim mytxt As TextBox
Dim c As Control
For Each c In UserForm1.Controls
    If TypeOf c Is msforms.TextBox Then
        If Len(c) > 0 Then Stop
    End If
Next

答案 2 :(得分:0)

由于正在检查所有文本框,因此这应该适合您。它与其他答案的不同之处在于它是一个单独的函数,并且它正在检查Null值,因为“空白”文本框会在Null时给出错误。 此外,我的Access 2010 Control对象不包含ControlType作为成员,因此我使用的原始属性应适用于任何版本。

功能:

Function TextsHaveText() As Boolean
  Dim ctl As Control

  TextsHaveText = False 'default is already False, but this wont hurt
  For Each ctl In Me.Controls

    If ctl.Properties("ControlType").value = acTextBox Then
      If Not IsNull(ctl.value) Then 'you could get an error if you don't check for nulls
        TextsHaveText = True
        Exit Function 'no point in going on if any text has value
      End If
    End If

  Next

End Function

电话:

If TextsHaveText = True Then
  If MsgBox("Les Données Saisi seront perdus, Désirez-vous continuer?", vbExclamation Or vbYesNo, "Continuer?") = vbYes Then
    Me.Undo
  Else
    MsgBox "no"
  End If
End If

只是因为您决定添加不相关的文本框,您可以使用textbox .tag属性来查看某些文本框。

[1]将所有需要的TextBox.tag属性设置为您可以检查的内容,例如: Text1.tag =“CHKLEN”(将非关联文本框标记留空)

[2]改变TextHaveText函数以包含.tag:

的acheck
Function TextsHaveText() As Boolean
  Dim ctl As Control

  TextsHaveText = False
  For Each ctl In Me.Controls
    If ctl.Properties("ControlType").value = acTextBox Then
      If ctl.Tag = "CHKLEN" Then 'check the tag of the textbox control for the indicator
        If Not IsNull(ctl.value) Then 'you could get an error if you don't check for nulls
          TextsHaveText = True
          Exit Function 'no point in going on if any text has value
        End If
      End If
    End If

  Next

End Function
相关问题