locked
is there a "using" construct in Access VBA (like there is in VB.NET)? RRS feed

  • Question

  • is there a "using" construct in Access VBA (like there is in VB.NET)?

    using connectionobject
      step 1
      step 2
    end using

    Saturday, January 28, 2017 12:00 PM

Answers

  • No, there isn't.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Saturday, January 28, 2017 12:56 PM

All replies

  • No, there isn't.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Saturday, January 28, 2017 12:56 PM
  • Hi Gordon,

    Pardon me for jumping in; I could be wrong but although VBA does not have a "using" statement,  I think VBA does have a similar construct to "using."

    For example, if I understand it correctly, you would use "using" to take advantage of "external" or unmanaged code (i.e. external to your procedure). If so, VBA can use or instantiate (class) objects and use its procedures in your code. For instance, to manipulate an Excel file, you might use something like:

    Dim xlApp As New Excel.Application

    or 

    Set xlApp = New Excel.Application

    Ditto for Outlook or Word.

    You can also create your own "class modules" to be reused in your code. e.g. Dim objMyClass As New MyClass, etc.

    Just my 2 cents...




    • Edited by .theDBguy Saturday, January 28, 2017 4:13 PM
    Saturday, January 28, 2017 4:08 PM
  • In you code snip if the instance of the connection object is ALREADY created, then you can use “with”.

    Eg:

    With connectonobject
       Step 1
       Step 2
    End with

    In your example code, it looks “more” like a “with” example then that of a “using” example.

    The main difference is that using entails that you are creating a instance of the object for a given code block. With simply means that the object already exists, and thus is a shortcut.

    Eg:

       With CurrentDb.QueryDefs("qryPassR")
          .SQL = "EXEC MyBackUp 'test3'"
          .ReturnsRecords = False
          .Execute
       End With


    So in above I don’t have to type the original object over and over. In the case of “using”, it really suggests  you are using “CreateObject” in code, and the "End Using"using means that object you just created will go out of scope and is disposed of.

    Regards,
    Albert D. Kallal (Access MVP)
    Edmonton, Alberta Canada

    Sunday, January 29, 2017 7:52 PM
  • I'm familiar with the "With" construct. I just wondered if "Using" got put into VBA somewhere and that I had missed it. It solves the problem of the "set object = nothing" issue and is a bit shorter to use some of the time.
    Sunday, January 29, 2017 7:58 PM