Improve Visual Basic code performance
Often, the best way to improve the speed at which your Microsoft Visual Basic code runs is to choose a more efficient approach. However, certain techniques can help make your code run faster.
- Make sure the code in your database is compiled. Renaming a database decompiles the code in it so recompile your code if you rename the database.
- Always explicitly declare variables. You can require that variables in your database be explicitly declared before they are used in a procedure.
- Use the most specific type possible when you declare variables. For example, declare a variable that will be used to represent a form as type Form rather than as type Object or Variant.
- Use variables to refer to properties, controls, and data access objects. If you refer more than once to the value of a property or control on a form, or to a data access object or its property, create object variables and refer to the variables rather than using full identifiers.
- Use the Me keyword for form references within an event procedure.
- Use the IIf function sparingly. Avoid using the IIf function if either of the return expressions takes a long time to evaluate.
- Use string functions when appropriate. Use the $ functions (for example, the Str$ function) when working with strings and the non-$ functions when working with Variant or other data types.
- Use the Integer or Long data type for math when possible.
- Use dynamic arrays and the Erase or ReDim statement to reclaim memory.
- Use constants whenever possible.
- Use bookmarks for fast relocation. Use bookmarks instead of the FindNext method or some other means to return to a particular record.
- Use the FindRecord and FindNext methods only on indexed fields. These methods are much more efficient when used on a field that is indexed.
- Organize your modules. Visual Basic loads modules on demand
— that is, it loads a module into memory only when your code calls one of the procedures in that module. Placing related procedures in the same module causes Visual Basic to load additional modules only as needed. - Eliminate unused procedures and unused variables. As you develop and modify your applications, you may leave behind procedures that aren't called from anywhere in your code or declared variables that are no longer used. Review your code frequently to find and remove unused procedures and variables.