如何在 VBA 中引用用户表单点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25446093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:21:09  来源:igfitidea点击:

How to reference userform click events in VBA

vbaonclickuserform

提问by Braide

I have a userform called 'dataBox' which is shown when the user clicks a command button. The user then inputs data and presses an OK command button on the userform to submit the data.

我有一个名为“dataBox”的用户表单,当用户单击命令按钮时会显示该表单。然后用户输入数据并按下用户表单上的 OK 命令按钮以提交数据。

What I am struggling with and what i need to happen is this: If the user clicks the OK button on the userform, a sub called category2 runs. If the user clicks 'X' nothing happens.

我正在苦苦挣扎以及我需要做的是:如果用户单击用户表单上的“确定”按钮,则会运行一个名为 category2 的子项。如果用户单击“X”,则什么也不会发生。

here is my command button code

这是我的命令按钮代码

Private Sub CommandButton1_Click()

dataBox.Show
If dataBox.Controls(okCommandButton_Click) Then
category2 Range("A1")
End If

End Sub 

The line

线

If dataBox.Controls(okCommandButton_Click) Then

is completely wrong and is the latest attempt at referencing when the ok click event fires but hopefully it is clear what I am trying to do. i have scoured the web for the answer and can't get it to work! Thanks.

是完全错误的,并且是当 ok click 事件触发时引用的最新尝试,但希望很清楚我想要做什么。我已经在网上搜索了答案,但无法使其正常工作!谢谢。

回答by Jens

EDIT:There are a few things you should change in your code. First off, you better open your form by using a DoCmdlike this:

编辑:您应该在代码中更改一些内容。首先,您最好使用DoCmd如下方式打开表单:

DoCmd.OpenForm "dataBox", acNormal

Secondly, instead of checking if the button is pressed in the commandbutton1's event, use the actual event itself.

其次,不要在 commandbutton1 的事件中检查按钮是否被按下,而是使用实际事件本身。

Private Sub okCommandButton_Click()
    'Do something/add the data to somewhere
End Sub

If the 'X' button is clicked, the only thing that should happen is the closure of the databox form, so:

如果单击“X”按钮,唯一应该发生的是关闭数据框表单,因此:

Private Sub XButton_Click()
    DoCmd.Close acForm, "dataBox", acSavePrompt
End Sub


Info on inputbox:I assume you are familiar with a messagebox, or msgbox(). The InputBox()command is essentially the same, but allows you to enter data and do something with it. The setup of your 'databox' form is alike an inputbox.

关于输入框的信息:我假设您熟悉消息框或msgbox(). 该InputBox()命令本质上是相同的,但允许您输入数据并对其进行操作。“数据框”表单的设置类似于输入框。

enter image description here

在此处输入图片说明

The inputbox above is called this way:

上面的输入框是这样调用的:

Dim answer as string
answer = Inputbox("I am an inputbox.", "enter data", "enter data here")

The following situations can occur:

可能会出现以下情况:

If you press cancel:
answer = ""

If you press 'X':
answer = ""

If you press OK:
answer = "Value entered in the text line"

The following article on msdn will probably offer more insight; http://msdn.microsoft.com/en-us/library/6z0ak68w(v=vs.90).aspx

msdn 上的以下文章可能会提供更多见解; http://msdn.microsoft.com/en-us/library/6z0ak68w(v=vs.90).aspx