CallByName 函数

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

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

语法

CallByName (objectprocnamecalltype [, 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