如何将VBA函数调用到子过程中? [英] How do I call a VBA Function into a Sub Procedure

查看:479
本文介绍了如何将VBA函数调用到子过程中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这对某个人来说是一个简单的问题,但我从来没有真正使用过功能模块,因为我不明白它们是什么。



所以我有很多东西可以用来减少冗余,但我想知道如何从窗体调用sub(像点击按钮)过程。



我试过这个......

pre $ c $ Sub Command_Click()
调用pptCreator
End Sub

我知道这很糟糕,但我不知道如何将它带入一个过程。

解决方案

下面是一些您可以在Microsoft Access中调用事物的不同方法:



从模块调用表单子或函数



您调用的表单中的子项必须是public的,如下所示:



  Public Sub DoSomething()
MsgBoxFoo
End Sub

像这样调用sub:

  Call Forms(form1)。DoSomething 

表格必须在您打电话前打开。



要调用事件过程,您应该在表单中调用公共过程,并调用事件程序在这个公共过程中。



从表单中调用模块中的子程序



  Public Sub DoSomethingElse()
MsgBoxBar
End Sub



 打电话给DoSomethingElse 



从表单中调用子例程而不使用事件过程



如果你想,您实际上可以将该函数绑定到窗体控件的事件,而无需在控件下创建事件过程。为此,您首先需要模块中的公共函数而不是子类,如下所示:

  Public Function DoSomethingElse 
MsgBoxBar
End Function

然后,如果您有一个按钮在窗体上,不要在属性窗口的OnClick事件中放置[Event Procedure],而应该放置:

  = DoSomethingElse )

单击按钮时,它将调用模块中的公共函数。



调用一个函数而不是一个过程

如果调用一个子类如下所示:

 呼叫MySub(MyParameter)

然后调用一个函数如下所示:

  Result = MyFunction(MyFarameter)

其中Result是函数返回的类型变量。

注意:您并不总是需要Call关键字。大多数情况下,你可以像这样调用它:

  MySub(MyParameter)


I know this is a simple question for someone out there, but I have never really used function module at all because I did not understand what they were.

So I have a whole bunch of things I can use this for (cut down on redundancy), but I want to know how I call into a sub (like a button click) procedure from a form.

I tried this...

Sub Command_Click()
    Call "pptCreator"
End Sub

I know that is pretty bad, but I have no idea how to bring this into a procedure.

解决方案

Here are some of the different ways you can call things in Microsoft Access:

To call a form sub or function from a module

The sub in the form you are calling MUST be public, as in:

Public Sub DoSomething()
  MsgBox "Foo"
End Sub

Call the sub like this:

Call Forms("form1").DoSomething

The form must be open before you make the call.

To call an event procedure, you should call a public procedure within the form, and call the event procedure within this public procedure.

To call a subroutine in a module from a form

Public Sub DoSomethingElse()
  MsgBox "Bar"
End Sub

...just call it directly from your event procedure:

Call DoSomethingElse

To call a subroutine from a form without using an event procedure

If you want, you can actually bind the function to the form control's event without having to create an event procedure under the control. To do this, you first need a public function in the module instead of a sub, like this:

Public Function DoSomethingElse()
  MsgBox "Bar"
End Function

Then, if you have a button on the form, instead of putting [Event Procedure] in the OnClick event of the property window, put this:

=DoSomethingElse()

When you click the button, it will call the public function in the module.

To call a function instead of a procedure

If calling a sub looks like this:

Call MySub(MyParameter)

Then calling a function looks like this:

Result=MyFunction(MyFarameter)

where Result is a variable of type returned by the function.

NOTE: You don't always need the Call keyword. Most of the time, you can just call the sub like this:

MySub(MyParameter)

这篇关于如何将VBA函数调用到子过程中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆