472,983 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,983 developers and data experts.

Early vs Late Binding

ADezii
8,834 Expert 8TB
The process of verifying that an Object exists and that a specified Property or Method is valid is called Binding. There are two times when this verification process can take place: during compile time (Early Binding) or run time (Late Binding). When you declare an Object Variable as a specific Data Type, you are using Early Binding so the verification can take place during compile time. When you declare a Variable of the generic Object Data Type, you are using Late Binding. In this case, VBA must find and verify the Object information during any execution of VBA statement that includes a Reference to the Object or one of its Properties or Methods. The time difference between Early and Late Binding can be quite significant. Some examples of Early Binding are listed below:
  1. Dim appAccess As Access.Application
  2. Dim appExcel As Excel.Application
  3. Dim winExcel As Excel.Window
  4. Dim winProject As Project.Window
  5. Dim chkBox As CheckBox
  6. Dim cboFinance As ComboBox
  7. Dim chtMain As Chart
  8. Dim lstLookup As ListBox
  9. Dim pvtNew As PivotTable
As an example, I'll refer to an ADO Field as an ADODB.Field and also as an Object (commented out). Benchmark Test Procedures using the code below report a hugh difference in the speed of accessing the ADO Field's Properties, naturally, in favor of Early Binding. The code is listed below.
Expand|Select|Wrap|Line Numbers
  1. Dim rst As ADODB.Recordset, strName As String
  2.  
  3. 'Early Binding. Declaring fld as ADODB.Field results in
  4. 'times that are around 7% of those measured declaring fld
  5. 'as Object. In the case of several hundred Records, the difference
  6. 'would not be significant, but in the case of several hundred
  7. 'thousand Records, the time difference would be enormous.
  8. Dim fld As ADODB.Field
  9.  
  10. 'Late Binding
  11. 'Dim fld As Object
  12.  
  13. Set rst = New ADODB.Recordset
  14. Set rst.ActiveConnection = CurrentProject.Connection
  15. rst.Source = "tblEmployee"
  16. rst.CursorType = adOpenStatic
  17.  
  18. rst.Open
  19.  
  20. rst.MoveFirst
  21.  
  22. Set fld = rst.Fields(0)
  23. Do While Not rst.EOF
  24.   strName = fld.Name
  25.     rst.MoveNext
  26. Loop
  27.  
  28. rst.Close
  29. Set fld = Nothing
  30. Set rst = Nothing
In every case, if at all possible, declare a Variable using the most specific Object Type that you can. For Access Controls, that means using, for example:
Expand|Select|Wrap|Line Numbers
  1. Dim cmd As CommandButton
instead of:
Expand|Select|Wrap|Line Numbers
  1. Dim cmd As Control
or, worst of all:
Expand|Select|Wrap|Line Numbers
  1. Dim cmd As Object
Aug 5 '07 #1
3 15944
dima69
181 Expert 100+
The process of verifying that an Object exists and that a specified Property or Method is valid is called Binding. There are two times when this verification process can take place: during compile time (Early Binding) or run time (Late Binding). When you declare an Object Variable as a specific Data Type, you are using Early Binding so the verification can take place during compile time. When you declare a Variable of the generic Object Data Type, you are using Late Binding. In this case, VBA must find and verify the Object information during any execution of VBA statement that includes a Reference to the Object or one of its Properties or Methods. The time difference between Early and Late Binding can be quite significant. Some examples of Early Binding are listed below:
  1. Dim appAccess As Access.Application
  2. Dim appExcel As Excel.Application
  3. Dim winExcel As Excel.Window
  4. Dim winProject As Project.Window
  5. Dim chkBox As CheckBox
  6. Dim cboFinance As ComboBox
  7. Dim chtMain As Chart
  8. Dim lstLookup As ListBox
  9. Dim pvtNew As PivotTable
As an example, I'll refer to an ADO Field as an ADODB.Field and also as an Object (commented out). Benchmark Test Procedures using the code below report a hugh difference in the speed of accessing the ADO Field's Properties, naturally, in favor of Early Binding. The code is listed below.
Expand|Select|Wrap|Line Numbers
  1. Dim rst As ADODB.Recordset, strName As String
  2.  
  3. 'Early Binding. Declaring fld as ADODB.Field results in
  4. 'times that are around 7% of those measured declaring fld
  5. 'as Object. In the case of several hundred Records, the difference
  6. 'would not be significant, but in the case of several hundred
  7. 'thousand Records, the time difference would be enormous.
  8. Dim fld As ADODB.Field
  9.  
  10. 'Late Binding
  11. 'Dim fld As Object
  12.  
  13. Set rst = New ADODB.Recordset
  14. Set rst.ActiveConnection = CurrentProject.Connection
  15. rst.Source = "tblEmployee"
  16. rst.CursorType = adOpenStatic
  17.  
  18. rst.Open
  19.  
  20. rst.MoveFirst
  21.  
  22. Set fld = rst.Fields(0)
  23. Do While Not rst.EOF
  24.   strName = fld.Name
  25.     rst.MoveNext
  26. Loop
  27.  
  28. rst.Close
  29. Set fld = Nothing
  30. Set rst = Nothing
In every case, if at all possible, declare a Variable using the most specific Object Type that you can. For Access Controls, that means using, for example:
Expand|Select|Wrap|Line Numbers
  1. Dim cmd As CommandButton
instead of:
Expand|Select|Wrap|Line Numbers
  1. Dim cmd As Control
or, worst of all:
Expand|Select|Wrap|Line Numbers
  1. Dim cmd As Object
Some drawbacks from early binding.
In order to use early binding to the objects like Excel, i.e.
Expand|Select|Wrap|Line Numbers
  1. Dim appExcel As Excel.Application
you must explicitly set the reference to that object library. This can cause your code to stop running on the user machine, (If, for example, he doesn't have Excel installed) - without any chance of error handling.
On the other hand, although late binding is much slower, you usually don't need to set it more than once, so the difference can hardly be noticed in overall application performance.
Aug 6 '07 #2
ADezii
8,834 Expert 8TB
Some drawbacks from early binding.
In order to use early binding to the objects like Excel, i.e.
Expand|Select|Wrap|Line Numbers
  1. Dim appExcel As Excel.Application
you must explicitly set the reference to that object library. This can cause your code to stop running on the user machine, (If, for example, he doesn't have Excel installed) - without any chance of error handling.
On the other hand, although late binding is much slower, you usually don't need to set it more than once, so the difference can hardly be noticed in overall application performance.
  1. I would still much rather explicitly check for the Reference, and if it exists, use the much faster Early Binding. You could always, as a last resort, resort to Late Binding - something similar to Conditional Compilation.
    Expand|Select|Wrap|Line Numbers
    1. Public Function fExcelExists() As Boolean
    2. Dim ref As Reference
    3.  
    4. For Each ref In Application.References
    5.   If ref.Name = "Excel" Then
    6.     fExcelExists = True
    7.       Exit Function
    8.   End If
    9. Next
    10.     fExcelExists = False
    11. End Function
    Expand|Select|Wrap|Line Numbers
    1. If fExcelExists() Then
    2.   'continue normal processing here
    3. End If
  2. In the illustrated code demo demonstrating Early vs Late Binding, (ADODB.Field vs Object), the difference was staggering. ADODB.Field process time was only 7% of the parallel Object approach.
Aug 6 '07 #3
dima69
181 Expert 100+
[list=1][*] I would still much rather explicitly check for the Reference, and if it exists, use the much faster Early Binding. You could always, as a last resort, resort to Late Binding - something similar to Conditional Compilation.
Expand|Select|Wrap|Line Numbers
  1. Public Function fExcelExists() As Boolean
  2. Dim ref As Reference
  3.  
  4. For Each ref In Application.References
  5.   If ref.Name = "Excel" Then
  6.     fExcelExists = True
  7.       Exit Function
  8.   End If
  9. Next
  10.     fExcelExists = False
  11. End Function
Expand|Select|Wrap|Line Numbers
  1. If fExcelExists() Then
  2.   'continue normal processing here
  3. End If
The only problem with this code is that it will not work if the reference is actually missing. Although checking for reference validity at runtime is possible, it is much more complicated, and still the solution is not 100% robust. The most complete explanation I saw on this is here: http://www.trigeminal.com/usenet/usenet026.asp
Aug 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Olaf Meding | last post by:
Is there a way to find out if I am using early or late binding given the reference ("excel" in the example below) returned by Dispatch()? >>> import win32com.client >>> excel =...
8
by: deko | last post by:
When using automation (and especially with early binding) I've heard it is best to use explicit references to everything. For example: Dim xlChart as Excel.Chart rather than Dim objChart...
2
by: Mystery Man | last post by:
We are developing a C# application that has many interfaces to the Microsoft suite (eg Word, Excel, Outlook, Powerpoint, etc). We need to support Office 97, 2000, 2002 and any future versions. ...
5
by: Plat | last post by:
Summary: I've got some *.ASPX pages that still use COM objects. I'd like to enable Option Strict, but I get "error BC30574: Option Strict On disallows late binding" errors. How can I bypass this...
2
by: mark | last post by:
I understand that writing programs with option strict on is the best way to obtain stable applications. I have also found the applications to run much faster. Option strict on disallows late...
21
by: ManningFan | last post by:
I need to use late binding in a project because it's company standard to not include references which aren't MS defaults, so I can't add the scripting runtime. I need to be able to search...
2
by: kogrover | last post by:
ISSUE: COM Excel Sort works with Early Binding, but not Late Binding, but py2exe only does Late Binding I have code similar to this (type from notes, so there may be a typo...) import...
0
by: RN1 | last post by:
Is this late binding? -------------------------------------------------------------------------------- Dim dSet As DataSet dSet = New DataSet...
1
by: pedestrian via DotNetMonster.com | last post by:
What are the example of early binding? How about late binding? Thank you for replying. (: -- Warmest Regards, Pedestrian Message posted via DotNetMonster.com
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.