Connecting Tech Pros Worldwide Forums | Help | Site Map

Cache Object References

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#1   Sep 23 '07
In writing code you often need to retrieve or set properties of the various Forms, Reports, Controls, or other Objects in your application. Generally, you refer to these Objects with statements like:
Expand|Select|Wrap|Line Numbers
  1. strCaption = Forms!("frmTest").cmdButton1.Caption 
  2.  
For a single reference to an Object, there's not much you can do to speed up the reference. If, on the other hand, you are going to be referring to many of the properties of that Object in a loop of some sort, you can achieve a substantial speed increase by pointing an Object Variable at that Object and using that Variable to reference the Object. For example, if you were going to reference many of a specific Control's properties, you would be well served to use code like this rather than to refer to the Control with the full syntax each time:
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As CommandButton
  2. Set ctl = Forms("frmTest").cmdButton1
  3.  
  4. Debug.Print ctl.Name
  5. Debug.Print ctl.Width
  6. 'etc...
  7.  
In another example, I'll use a particular Field within an ADO Recordset to prove my point:
Expand|Select|Wrap|Line Numbers
  1. 'This would be the slooooower case
  2. Set rst = New ADODB.Recordset
  3. Set rst.ActiveConnection = CurrentProject.Connection
  4. rst.Source = "tblTests"
  5. rst.Open
  6.  
  7. For intCounter = 1 to lngSomeReallyBigNumber
  8.   strName = rst.Fields(0).Name
  9. Next intCounter
  10.  
  11. 'The much faster procedure caches the reference to the Field, as such:
  12. Set rst = New ADODB.Recordset
  13. Set rst.ActiveConnection = CurrentProject.Connection
  14. rst.Source = "tblTests"
  15. rst.Open
  16.  
  17. Set fld = rst.Fields(0)
  18. For intCounter = 1 to lngSomeReallyBigNumber
  19.   strName = fld.Name
  20. Next intCounter
  21.  
  22. Even in this simple case, in which only a single dot was removed from within the expression, this faster code will take about 40% of the time it took in the slooooower version.
In addition to the previously stated, using VBA's With..End With syntax affords the same improvements in code execution time.

In closing, the crucial point of this Tip is:
Dots in Object References will always slow down your code. You should take whatever effort you can to reduce the number of dots!



BradHodge's Avatar
Expert
 
Join Date: Apr 2007
Location: Lubbock, Texas - U.S.A
Posts: 160
#2   Sep 24 '07

re: Cache Object References


Very helpful ADezii. I never knew that about dots slowing things down. I'm going to stop using periods in all my writing Maybe that will help me get more things done during the day :)

Brad
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#3   Sep 24 '07

re: Cache Object References


Quote:

Originally Posted by BradHodge

Very helpful ADezii. I never knew that about dots slowing things down. I'm going to stop using periods in all my writing Maybe that will help me get more things done during the day :)

Brad

Thanks BradHodge. Next Week's Tip will probably be the With...End With Statement which is closely tied into this week's Topic. In you are interested in Caching Object References, and improving code execution time, you will definitely want to look into this Statement also. The Topic of next week's Tip is a closely guarded secret with only myself, the president, and Mary knowing what it is - forget we ever had this conversation ! (LOL).
BradHodge's Avatar
Expert
 
Join Date: Apr 2007
Location: Lubbock, Texas - U.S.A
Posts: 160
#4   Sep 25 '07

re: Cache Object References


Quote:

Originally Posted by ADezii

Thanks BradHodge. Next Week's Tip will probably be the With...End With Statement which is closely tied into this week's Topic. In you are interested in Caching Object References, and improving code execution time, you will definitely want to look into this Statement also. The Topic of next week's Tip is a closely guarded secret with only myself, the president, and Mary knowing what it is - forget we ever had this conversation ! (LOL).

What conversation???
Reply