Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access USING语句的VBA是什么_Ms Access_Vba_Using Statement_Using Directives - Fatal编程技术网

Ms access USING语句的VBA是什么

Ms access USING语句的VBA是什么,ms-access,vba,using-statement,using-directives,Ms Access,Vba,Using Statement,Using Directives,在csharp或vb.net中,我们使用using语句的原因我们知道:可以在不显式写入的情况下自动打开和关闭数据库。 我想在VBA 怎么做 是否所有VB.NET语句/关键字/都在VBA中可用 如何判断给定语句在VBA中是否已知?是否有所有VBA语句/关键字/运算符的库(glosary) c# vb.net Using s = New MyDbContext() '--..do work here End Using 没有与使用语句的等效的VBA 提供了VBA文档的示例

在csharp或vb.net中,我们使用
using
语句的原因我们知道:可以在不显式写入的情况下自动打开和关闭数据库。 我想在
VBA

  • 怎么做
  • 是否所有VB.NET语句/关键字/都在VBA中可用
  • 如何判断给定语句在VBA中是否已知?是否有所有VBA语句/关键字/运算符的库(glosary)
  • c#

    vb.net

        Using s = New MyDbContext()
         '--..do work here
        End Using
    

    没有与使用语句的
    等效的VBA


    提供了VBA文档的示例甚至可下载的脱机版本。

    回答您的第一个问题,正如您所暗示的,
    使用
    只是在实现了
    IDisposable
    接口的对象实例上调用
    Dispose()
    的语法糖

    相当于

    Dim s as MyDbContext
    Try
       s = New MyDbContext()
       // ...
    Finally
       s.Dispose()
    End Try
    

    由于
    VBA
    不支持使用
    sugar的
    ,并且在没有结构化
    Try..Catch
    异常处理的情况下,您需要在控制对象寿命的所有路径上显式调用
    Dispose()
    。在这种情况下,您最好使用
    .Close()

    非常感谢@StuartLC!它回答了我的问题,因为
    Dispose()
    通常只调用
    Close()
    对于大多数数据库资源,实际上没有必要在
    Close()
    Dispose()
    之间进行选择。您可以在VBA中模拟TRY-CATCH块,请参见此处
    Dim s as MyDbContext
    Try
       s = New MyDbContext()
       // ...
    Finally
       s.Dispose()
    End Try