Creating Automation Objects

team lib

Because automation objects are just objects, you can create them in the same way as you've created other objects. For example, to create a recordset object variable you use a Dim statement:

   Dim objRec As Recordset   

Similarly, to create an instance of an application object, you generally use this format:

   Dim objApp As application_name.Application   

Here, application_name would be Word , Outlook , or Excel . Using this just creates a variable to hold an object of the appropriate type, and doesn't actually create the object. This method is generally used when the object already exists, and you are just using your own variable. For example, consider a Field in a recordset - you would declare the variable like this:

   Dim objFld As Field   

This doesn't create a new Field object, but just creates a variable to hold an existing Field . This means that you cannot do this:

   objFld.Name = "NewField"   

If you try this you will get an error ( Error 91 - Object variable or With block variable not set ). That's because objFld is not an object, just an object variable - it hasn't been instantiated . If you want to create a new object then you use the New keyword. For example:

   Dim objFld As New Field   

You can then set the field name because the object has been instantiated. In reality the object is instantiated when you access the first property or method of the object.

So, the rules are:

  • If you are going to point to an object that already exists, just use the normal syntax.

  • If you are creating a new instance of an object, then you must use the New keyword. We usually do this in the Set statement rather than in the Dim statement, though either works.

team lib

Beginning Access 2002 VBA
Beginning Access 2002 VBA (Programmer to Programmer)
ISBN: 0764544020
EAN: 2147483647
Year: 2003
Pages: 256 © 2008-2017.
If you may any questions please contact us: