将VBA函数调用到子过程中

将VBA函数调用到子过程中,vba,function,ms-access,office-2003,Vba,Function,Ms Access,Office 2003,我知道这对于某些人来说是一个简单的问题,但我从来没有真正使用过函数模块,因为我不理解它们是什么 所以我有很多东西可以使用它(减少冗余),但我想知道如何从表单调用sub(比如点击按钮)过程 我试过这个 Sub Command_Click() Call "pptCreator" End Sub 我知道这很糟糕,但我不知道如何将其引入到过程中。如果pptCreator是同一文件中的函数/过程,您可以如下调用它 调用pptCreator()以下是在Microsoft Access中调用内容的几

我知道这对于某些人来说是一个简单的问题,但我从来没有真正使用过函数模块,因为我不理解它们是什么

所以我有很多东西可以使用它(减少冗余),但我想知道如何从表单调用sub(比如点击按钮)过程

我试过这个

Sub Command_Click()
    Call "pptCreator"
End Sub

我知道这很糟糕,但我不知道如何将其引入到过程中。

如果pptCreator是同一文件中的函数/过程,您可以如下调用它


调用pptCreator()

以下是在Microsoft Access中调用内容的几种不同方式:

从模块调用窗体子对象或函数 您正在调用的表单中的sub必须是公共的,如中所示:

Public Sub DoSomething()
  MsgBox "Foo"
End Sub
按如下方式调用sub:

Call Forms("form1").DoSomething
Public Function DoSomethingElse()
  MsgBox "Bar"
End Function
Call MySub(MyParameter)
Result=MyFunction(MyFarameter)
MySub(MyParameter)
Private Sub Command23_Click()

    Call MyFunctionName

End Sub
在你打电话之前,表格必须打开

要调用事件过程,应调用窗体中的公共过程,并在此公共过程中调用事件过程

从窗体调用模块中的子例程 …只需直接从事件过程调用它:

Call DoSomethingElse
从窗体调用子例程而不使用事件过程 如果需要,实际上可以将函数绑定到表单控件的事件,而不必在控件下创建事件过程。为此,您首先需要在模块中使用公共函数,而不是子函数,如下所示:

Call Forms("form1").DoSomething
Public Function DoSomethingElse()
  MsgBox "Bar"
End Function
Call MySub(MyParameter)
Result=MyFunction(MyFarameter)
MySub(MyParameter)
Private Sub Command23_Click()

    Call MyFunctionName

End Sub
然后,如果表单上有一个按钮,则不要将[Event Procedure]放在属性窗口的OnClick事件中,而是放以下内容:

=DoSomethingElse()
当您单击该按钮时,它将调用模块中的公共函数

调用函数而不是过程 如果调用sub时如下所示:

Call Forms("form1").DoSomething
Public Function DoSomethingElse()
  MsgBox "Bar"
End Function
Call MySub(MyParameter)
Result=MyFunction(MyFarameter)
MySub(MyParameter)
Private Sub Command23_Click()

    Call MyFunctionName

End Sub
然后调用函数如下所示:

Call Forms("form1").DoSomething
Public Function DoSomethingElse()
  MsgBox "Bar"
End Function
Call MySub(MyParameter)
Result=MyFunction(MyFarameter)
MySub(MyParameter)
Private Sub Command23_Click()

    Call MyFunctionName

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

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

Call Forms("form1").DoSomething
Public Function DoSomethingElse()
  MsgBox "Bar"
End Function
Call MySub(MyParameter)
Result=MyFunction(MyFarameter)
MySub(MyParameter)
Private Sub Command23_Click()

    Call MyFunctionName

End Sub

当您传入参数时,模块中的过程开始变得有用和通用

例如:

Public Function DoSomethingElse(strMessage As String)  
    MsgBox strMessage
End Function

现在可以显示通过名为strMessage的字符串变量传入的任何消息。

向窗体上的新按钮添加函数:(并避免使用宏调用函数)

创建函数(函数MyFunctionName())并进入表单设计视图后:

  • 添加一个新按钮(我不认为您可以重新分配一个旧按钮-但不确定)

  • 当按钮向导窗口打开时,单击取消

  • 转到按钮属性事件选项卡-单击-字段

  • 在该字段下拉菜单中,选择:事件过程

  • 现在单击下拉菜单旁边的按钮,该下拉菜单具有。。。在它中,您将被带到窗体Visual Basic窗口中的新建专用子窗口

  • 在该私有子类型中:调用MyFunctionName
    它应该是这样的:

    Call Forms("form1").DoSomething
    
    Public Function DoSomethingElse()
      MsgBox "Bar"
    End Function
    
    Call MySub(MyParameter)
    
    Result=MyFunction(MyFarameter)
    
    MySub(MyParameter)
    
    Private Sub Command23_Click()
    
        Call MyFunctionName
    
    End Sub
    
  • 那就保存它吧


  • 调用子过程–三向技术

    一旦有了过程,无论是创建的还是作为Visual Basic语言的一部分,都可以使用它。使用过程也称为调用过程

    在调用过程之前,应该首先找到要在其中使用它的代码部分。要调用简单过程,请键入其名称。以下是一个例子:

    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    
    msgbox strFullName
    End Sub
    
    Sub Exercise()
        CreateCustomer
    End Sub
    
    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    Sub Exercise()
        Call CreateCustomer
    End Sub
    
    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    Sub Exercise()
        CreateCustomer()
    End Sub
    
    Private Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    除了使用过程的名称来调用它之外,还可以在它前面加上call关键字。以下是一个例子:

    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    
    msgbox strFullName
    End Sub
    
    Sub Exercise()
        CreateCustomer
    End Sub
    
    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    Sub Exercise()
        Call CreateCustomer
    End Sub
    
    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    Sub Exercise()
        CreateCustomer()
    End Sub
    
    Private Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    调用过程时,无论是否使用Call关键字,都可以选择在过程名称的右侧键入左括号和右括号。以下是一个例子:

    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    
    msgbox strFullName
    End Sub
    
    Sub Exercise()
        CreateCustomer
    End Sub
    
    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    Sub Exercise()
        Call CreateCustomer
    End Sub
    
    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    Sub Exercise()
        CreateCustomer()
    End Sub
    
    Private Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    程序和访问级别

    与变量访问一样,对过程的访问可以由访问级别控制。程序可以是私有的,也可以是公共的。要指定过程的访问级别,请在其前面加上Private或Public关键字。以下是一个例子:

    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    
    msgbox strFullName
    End Sub
    
    Sub Exercise()
        CreateCustomer
    End Sub
    
    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    Sub Exercise()
        Call CreateCustomer
    End Sub
    
    Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    Sub Exercise()
        CreateCustomer()
    End Sub
    
    Private Sub CreateCustomer()
        Dim strFullName As String
    
        strFullName = "Paul Bertrand Yamaguchi"
    End Sub
    
    应用于全局变量的规则相同:

    Private:如果一个过程被设置为Private,那么它可以被同一模块的其他过程调用。外部模块的程序无法访问此类程序

    此外,当过程是私有的时,其名称不会显示在“宏”对话框中

    Public:创建为Public的过程可以由同一模块的过程调用,也可以由其他模块的过程调用

    此外,如果某个过程创建为公共过程,则当您访问“宏”对话框时,会显示其名称,您可以从该对话框运行该过程


    那么,这是否意味着我将不再将函数“调用”到表单的sub中,而是“将表单调用到功能模块中??我想我明白了,但还是有点困惑。我有一个powerpoint自动化程序,我想在整个数据库中使用,因为这个数据库的目的是跟踪指标,而客户端的最终游戏是演示文稿(主要是power point)。我有分散在不同形式中的不同代码集,它们为我完成了这项工作,但大部分都是重复的。从最近的解释来看,模块中的函数将帮助我……通过调用模块函数中的“特定代码集”,消除大多数重复编码。但它是在表格之外运行的。因此,事件触发器仍然是表单中的“按钮单击”。听起来我的想法正好相反,我必须调用函数的formsub?而不是相反。对不起,我知道这很明显,我在这方面充其量只是个新手。。。非常感谢您的帮助。在您的情况下,我认为您只需要将您的sub放入一个模块中,将sub公开,然后将其命名为:call pptCreator。请参阅本答案中的“从窗体调用模块中的子例程”。我在模块中编写了函数,并将其保存为pptCreator。然后我转到一个我想与之结合使用的表单,事件触发器是一个but