CallByName 函数

应用对象
Microsoft 365 专属 Access Access 2024 Access 2021 Access 2019 Access 2016

执行对象的方法,或设置或返回对象的属性。

语法

CallByName (object , procname, calltype [, args () ])

CallByName 函数语法具有以下参数:

参数 说明
对象 必需。 对象) (变体。 将对其执行函数的对象的名称。
procname 必需。 字符串) (变体。 包含对象属性或方法名称的字符串表达式。
callType 必需。 常量。 vbCallType 类型的常量,表示正在调用的过程类型。
args () 可选。 变体 (数组) 。

    

备注

CallByName 函数用于获取或设置属性,或在运行时使用字符串名称调用方法。

在下面的示例中,第一行使用 CallByName 设置文本框的 MousePointer 属性,第二行获取 MousePointer 属性的值,第三行调用 Move 方法来移动文本框:

CallByName Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName (Text1, "MousePointer", vbGet)
CallByName Text1, "Move", vbMethod, 100, 100

示例

注意

下面的示例演示了在Visual Basic for Applications (VBA) 模块中使用此函数。 有关使用 VBA 的详细信息,请在“搜索”旁边的下拉列表中选择“开发人员参考”,然后在搜索框中输入一个或多个术语。

此示例使用 CallByName 函数调用命令按钮的 Move 方法。

该示例还使用一个窗体 (Form1) ,其中包含一个 (Command1) 按钮以及一个 (Label1) 标签。 加载窗体时,标签的 Caption 属性设置为要调用的方法的名称,在本例中为“Move”。 单击该按钮时, CallByName 函数会调用该方法来更改按钮的位置。

Option Explicit
Private Sub Form_Load()
  Label1.Caption = "Move"' Name of Move method.
End Sub
Private Sub Command1_Click()
If Command1.Left <> 0 Then
  CallByName Command1, Label1.Caption, vbMethod, 0, 0
Else
  CallByName Command1, Label1.Caption, vbMethod, 500, 500
End If