Skip to Content
VB & VBA in a Nutshell: The Language
book

VB & VBA in a Nutshell: The Language

by Paul Lomax
October 1998
Intermediate to advanced
656 pages
16h 10m
English
O'Reilly Media, Inc.
Content preview from VB & VBA in a Nutshell: The Language

5.1. Creating Object Model References

Before you can use the properties, methods, and events of an object model, you must first create a programmatic reference to the class containing the properties, methods, or events you wish to use. You do this by declaring a local object variable to hold a reference to the object. You then assign a reference to the object to the local variable. There are two methods, detailed next, for doing this in VB.

5.1.1. Generic Object References and Late Binding

In situations in which the object you are instantiating doesn't provide a type library, or when you are unsure at design time precisely which object you need to reference, you can use a local variable dimensioned as the generic type of Object. Then you use the CreateObject or GetObject function to return an object reference to assign to the local generic object variable. For example:

Dim oObjVar As Object
Set oObjVar = CreateObject("myComponent.myClass")

VB6 has also added to the functionality of the CreateObject function by allowing you to pass the server machine name as a string parameter. (For details on how to use CreateObject and GetObject see Chapter 7.)

Sidebar 1. If You Haven't Set It, You Can't Use It!

To assign an object reference to a local object variable, you must use the Set statement. The sole exception is the For Each...Next loop, which can iterate the object members of a collection. It generates an object reference automatically for each object that it iterates.

This ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming Excel with VBA and .NET

Programming Excel with VBA and .NET

Jeff Webb, Steve Saunders

Publisher Resources

ISBN: 1565923588Catalog PageErrata