我有一个下面的子CreaNewT
来创建一个新表。在CreatNewT
中,我叫一个公共子ChecTabl
来检查该表是否已经存在。
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
以及如果该表已经存在的情况下调用它的子程序。但是,它只会停止执行ChecTabl
,并继续执行CreaNewT
的其余部分。如何编码,使其停止执行ChecTabl
和调用它的子程序?谢谢
答案 0 :(得分:0)
感谢Tim,我将ChecTabl
更改为布尔函数。我可以检查其返回值并决定是否退出调用此函数的子程序。
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