8

我有一个带有多种选择选项的 VBA 表单,包括下拉菜单、文本字段、复选框和单选。

我只是想知道按下按钮清除所有这些字段的最佳方法。我的一个朋友试图通过将下面的代码通过电子邮件发送给我来提供帮助,但不幸的是它不起作用,我检查了变量名。

关于如何改进它的任何建议?

提前致谢。

Private Sub btnReset_Click()

Unload Me
UserForm.Show

End Sub

这是用户表单的其他代码。

Dim DeptCode 'Holds department code

Private Sub UserForm_Initialize()

    Dim c_deptCode As Range
    Dim c_deptName As Range
    Dim deptCodes As Variant
    Dim deptNames As Variant

    Dim ws_dept As Worksheet
    Set ws_dept = Worksheets("lookupDept")

    ' Assign each range to an array containing the values
    deptCodes = Choose(1, ws_dept.Range("deptCode"))
    deptNames = Choose(1, ws_dept.Range("deptName"))

    For i = 1 To ws_dept.Range("deptCode").Rows.Count
        ' Create the combined name (code + space + name)
        CombinedName = deptCodes(i, 1) & " - " & deptNames(i, 1)
        cbo_deptCode.AddItem CombinedName
    Next i

End Sub
4

5 回答 5

19

我认为当它到达 Unload Me 行时,代码执行会停止,这就是它不适合你的原因。这是一个通用事件过程,用于重置表单上的所有(大部分)控件。

Private Sub cmdReset_Click()

    Dim ctl As MSForms.Control

    For Each ctl In Me.Controls
        Select Case TypeName(ctl)
            Case "TextBox"
                ctl.Text = ""
            Case "CheckBox", "OptionButton", "ToggleButton"
                ctl.Value = False
            Case "ComboBox", "ListBox"
                ctl.ListIndex = -1
        End Select
    Next ctl

End Sub

它不会重新填充组合框和列表框,只是清除选择,这是我假设你想要的。

于 2012-10-10T16:51:51.707 回答
6

I know this question is almost 2 years old BUT I was looking for an answer like this. However, I am using Access 2010 and discovered the function did not work entirely as expected:

  • ctl can be Dim-ed simply as Control
  • For a textbox, the ctl.Text property can only be assigned to if the control has focus (use ctl.Value instead)
  • If an OptionButton is part of an OptionGroup it can not be assigned a value

So with these issues in mind, here is my rewritten function:

Private Sub resetForm()

    Dim ctl As Control ' Removed MSForms.

    For Each ctl In Me.Controls
        Select Case TypeName(ctl)
            Case "TextBox"
                ctl.value = ""
            Case "CheckBox", "ToggleButton" ' Removed OptionButton
                ctl.value = False
            Case "OptionGroup" ' Add OptionGroup
                ctl = Null
            Case "OptionButton" ' Add OptionButton
                ' Do not reset an optionbutton if it is part of an OptionGroup
                If TypeName(ctl.Parent) <> "OptionGroup" Then ctl.value = False
            Case "ComboBox", "ListBox"
                ctl.ListIndex = -1
        End Select
    Next ctl

End Sub
于 2015-03-19T08:25:11.327 回答
2

对于最新版本的 Access,Microsoft 现在已经很好地记录了这一点。看起来上面的一些答案是指旧版本。下面的代码在我的应用程序中完美运行。我只包含了我需要的控件类型,但您可以在下面的 Microsoft 链接中找到任何其他控件的文档。

参考:https ://msdn.microsoft.com/en-us/vba/access-vba/articles/textbox-controltype-property-access?f=255&MSPPError=-2147217396

Dim ctl As Control

For Each ctl In Me.Controls
    With ctl
        Select Case .ControlType
            Case acTextBox
                .Value = ""
            Case acCheckBox
                .Value = False
            Case acComboBox
                .SetFocus
                .SelText = ""
            Case acListBox
                .Value = Null
        End Select
    End With
Next ctl

请注意,对于组合框,您必须在设置值之前在那里设置焦点。参考:https ://msdn.microsoft.com/en-us/vba/access-vba/articles/combobox-seltext-property-access

于 2017-11-10T13:54:50.727 回答
2

添加到最新答案 - 如果您的某些控件具有默认值,您可以使用

ctl.Value = ctl.DefaultValue

这对我有用,至少适用于复选框和组合框。

于 2018-02-13T19:43:11.060 回答
1

你可以试试这个:

Private Sub btnReset_Click()

    Call UserForm_Initialize

End Sub
于 2016-01-22T13:57:47.170 回答