Referring to Objects

team lib

So far, you have only used an index into the collection to reference the required item, but there are other ways to find the object you require. The first way we'll look at is probably the most common. It allows you to refer to an object explicitly as a member of a collection. For example, to set an object variable to the form frmCompany you can use:

 Dim frmP As Form Set frmP = Forms!frmCompany 

You use the exclamation mark to separate the collection from the object. If the object has spaces in its name , you have to enclose it within square brackets. For example, if the form were called Company Details , then you would do this:

 Set frmP = Forms![Company Details] 

The second method is similar to using an index, but instead of a number you use the name of the object or a string variable:

 Set frmP = Forms("frmCompany") 

Or, using a string containing the name:

 strFormName = "frmCompany" Set frmP = Forms(strFormName) 

Of course, you can use a number to refer to an object in a collection just like you would with an array:

 Set frmP = Forms(3) 

This would set frmP to point to the fourth form in the collection (collection numbers start at 0). So, you can access a form either from its name or its position in the Forms collection. If you use For Each to cycle through the collection, you use the numeric position in the collection. Using the name allows you to access single objects directly.

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: