我有一个名为“dataBox”的用户窗体,当用户单击命令按钮时会显示该窗体。然后,用户输入数据并按用户表单上的“确定”命令按钮来提交数据。
我正在努力解决的问题以及我需要发生的事情是:如果用户单击用户表单上的“确定”按钮,则会运行一个名为“category2”的子程序。如果用户单击“X”,则不会发生任何事情。
这是我的命令按钮代码
Private Sub CommandButton1_Click()
dataBox.Show
If dataBox.Controls(okCommandButton_Click) Then
category2 Range("A1")
End If
End Sub
线路
If dataBox.Controls(okCommandButton_Click) Then
是完全错误的,是在 ok click 事件触发时引用的最新尝试,但希望我清楚我正在尝试做什么。我已经在网上搜索答案,但无法让它工作!谢谢。
编辑: 您应该在代码中更改一些内容。首先,您最好使用
DoCmd
打开表单,如下所示:
DoCmd.OpenForm "dataBox", acNormal
其次,不要在 commandbutton1 的事件中检查按钮是否被按下,而是使用实际事件本身。
Private Sub okCommandButton_Click()
'Do something/add the data to somewhere
End Sub
如果单击“X”按钮,唯一应该发生的事情就是关闭数据框表单,因此:
Private Sub XButton_Click()
DoCmd.Close acForm, "dataBox", acSavePrompt
End Sub
有关输入框的信息:我假设您熟悉消息框或
msgbox()
。 InputBox()
命令本质上是相同的,但允许您输入数据并对其执行某些操作。
“数据框”表单的设置类似于输入框。
上面的输入框是这样调用的:
Dim answer as string
answer = Inputbox("I am an inputbox.", "enter data", "enter data here")
可能会出现以下情况:
If you press cancel:
answer = ""
If you press 'X':
answer = ""
If you press OK:
answer = "Value entered in the text line"
msdn 上的以下文章可能会提供更多见解; http://msdn.microsoft.com/en-us/library/6z0ak68w(v=vs.90).aspx