【问题标题】:Access VBA exit a called sub and skip the rest of the sub访问 VBA 退出调用的子程序并跳过子程序的其余部分
【发布时间】:2019-03-07 05:17:47
【问题描述】:

我有一个子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 和调用它的子程序?谢谢

【问题讨论】:

  • ChecTabl设为函数,如果表已经存在则返回False。

标签: vba exit


【解决方案1】:

感谢 Tim,我将 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

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 2019-06-19
    • 2016-06-11
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多