# Re: 如何利用for回圈来call子程式(sub)呢?

Posted by Sun Jack on August 07, 1999 at 08:22:34:

In Reply to: 如何利用for回圈来call子程式(sub)呢? posted by 小明 on August 05, 1999 at 20:31:39:

:
: 若是有一大堆子程式名称都很接近
: 好比OpenTable1, OpenTable2, OpenTable3...
: 正常的做法就call OpenTable1, call OpenTable2...
: 不知道有没有更好的方法？
: 好比用for..next来控制call OpenTableXXX
: ^^^
: 谢谢~

如果你要呼叫的副程式是 Object 的 Interface,则可以用
CallByName 的方式来呼叫.下面的例子为执行form1 的Interface 及 Class1 的 Interface

'*****************************
'Form1 的内容
Option Explicit

Private Sub Form_Load()
Dim i As Long
Dim obj As Class1
For i = 1 To 3
CallByName Me, "OpenTable" & i, VbMethod
Next i
Set obj = New Class1
For i = 1 To 3
CallByName obj, "OpenTable" & i, VbMethod
Next i
Set obj = Nothing
End Sub

Public Sub OpenTable1()
MsgBox "Form1.OpenTable1"
End Sub

Public Function OpenTable2() As Boolean
MsgBox "Form1.OpenTable2"
End Function

Public Function OpenTable3() As String
MsgBox "Form1.OpenTable3"
End Function

'**************************
'Class1 的内容

Option Explicit

Public Sub OpenTable1()
MsgBox "Class1.OpenTable1"
End Sub

Public Function OpenTable2() As Boolean
MsgBox "Class1.OpenTable2"
End Function

Public Function OpenTable3() As String
MsgBox "Class1.OpenTable3"
End Function

Follow Ups: