访问 VBA 退出一个被调用的子程序并跳过子程序的其余部分
Access VBA exit a called sub and skip the rest of the sub
我有一个如下所示的子 CreaNewT
来创建一个新的 table。在 CreatNewT
中,我调用一个 public sub ChecTabl
来检查这个 table 是否已经存在。
Sub CreaNewT()
Dim ...
Dim ...
Call ChecTabl("TableName")
...
在 ChecTabl
我有
Dim TS As TableDefs
Dim T As TableDef
Set TS = CurrentDb.TableDefs
For Each T In TS
If T.Name = Str_Tabl Then
MsgBox "This table already exists. Please choose another table name.", vbOKOnly
Exit Sub
End If
Next
我写 Exit Sub
是因为我想退出 ChecTabl
和调用它的子程序,如果这个 table 已经存在的话。但是,它只停止执行 ChecTabl
并继续 CreaNewT
的其余部分。我如何编写代码以使其停止执行 ChecTabl
和调用它的子程序?谢谢
谢谢蒂姆,我把 ChecTabl
改成了一个布尔函数。我可以检查它的返回值并决定是否退出调用这个函数的sub。
Public Function ChecTabl(Str_Tabl As String) As Boolean
Dim TS As TableDefs
Dim T As TableDef
Set TS = CurrentDb.TableDefs
For Each T In TS
If T.Name = Str_Tabl Then
MsgBox "This table already exists. Please choose another table name.", vbOKOnly
ChecTabl = True
Exit Function
End If
Next
ChecTabl = False
Exit_Func:
Set TS = Nothing
Set T = Nothing
End Function
我有一个如下所示的子 CreaNewT
来创建一个新的 table。在 CreatNewT
中,我调用一个 public sub ChecTabl
来检查这个 table 是否已经存在。
Sub CreaNewT()
Dim ...
Dim ...
Call ChecTabl("TableName")
...
在 ChecTabl
我有
Dim TS As TableDefs
Dim T As TableDef
Set TS = CurrentDb.TableDefs
For Each T In TS
If T.Name = Str_Tabl Then
MsgBox "This table already exists. Please choose another table name.", vbOKOnly
Exit Sub
End If
Next
我写 Exit Sub
是因为我想退出 ChecTabl
和调用它的子程序,如果这个 table 已经存在的话。但是,它只停止执行 ChecTabl
并继续 CreaNewT
的其余部分。我如何编写代码以使其停止执行 ChecTabl
和调用它的子程序?谢谢
谢谢蒂姆,我把 ChecTabl
改成了一个布尔函数。我可以检查它的返回值并决定是否退出调用这个函数的sub。
Public Function ChecTabl(Str_Tabl As String) As Boolean
Dim TS As TableDefs
Dim T As TableDef
Set TS = CurrentDb.TableDefs
For Each T In TS
If T.Name = Str_Tabl Then
MsgBox "This table already exists. Please choose another table name.", vbOKOnly
ChecTabl = True
Exit Function
End If
Next
ChecTabl = False
Exit_Func:
Set TS = Nothing
Set T = Nothing
End Function