Two Hoots Banner



Visual Basic Speed Tips

  • With Select Case and If..ElseIf blocks, always place first the Case clauses that deal with the most frequent values.

  • Use the DoEvents statement sparingly, if possible call it only every so often in a loop not every time.

  • Don't use the IIf function in time critical loops. A regular If..Then..Else block is always faster.

  • Don't use empty For...Next loops to pause your program, use the Sleep API function which releases the CPU and lets other apps in the system effectively multitask.

  • Speed up access to objects properties by reducing the number of "dots" in the expression. For instance :
Form1.Text1.Text = ""
Form1.Text1.ForeColor = 0
Can be replaced by

With Form1.Text
  .Text1=""
  .ForeColor = 0
End With
  • Define a recordset that best supports the features you need. If you need to select records for a report create a forward-only, read-only recordset, which uses fewer resources and often works faster than a dynaset-type recordset.

  • Use Snapshots for recordsets of less than 500 records as they are generally more efficient than dynasets.

  • Use the GetInputState API to test for keyboard interrupt in a long loop and then call DoEvents, rather then calling DoEvents on every iteration.

  • Always close and restart VB before making the final EXE. This can reduce the size of your EXE by 10 to 30 percent, by removing any data structures or variables you used during development.

  • To test the speed of you code declare a single variable TStart and is the line TSTart = Timer at the beginning of the code segment you want to test. At the end put Debug.Print = Timer - TStart.

  • When updating the values of several records in a loop, put a BeginTrans, CommitTrans around the loop, which will speed up the update process.

BeginTrans
Do Until dsData.EOF
  dsData.Edit
  dsData!Country = "EN"
  dsData.Update
Loop
CommitTrans
  • To toggle a boolean between True and False use the Not operator instead of an If statement.

  • When removing a control from your project ensure you remove all the unused code from the controls event procedures.

  • Remove all unused Declare statements as they take up 11 bytes each.

  • Use the Len() function to check for an empty string rather than the "<>" or "=" operators as it is faster.

  • Set the ScreenUpdating property of the Excel application object to false to stop the excel screen from repainting each change you make. Also set the DisplayAlerts property to false to hide any trace of what's going on behind the scenes.

  • The following methods outline ways to transfer data from VB into Excel. The first method involves transferring data cell by cell.
Dim oApplication as Object
Dim oWorkBook as Object
Dim oWorksheet as Object

' Create instance of Excel Object, and Create a workbook
Set oApplication = CreateObject("Excel.Application")
Set oWorkBook = oApplication.Workbooks.Add

' Create Worksheet Object
Set oWorksheet = oWorkbook.Worksheets(1)

' Write data to Cells
With oWorksheet
  .Range("A1").Value = "Title"
  .Range("B1").Value = "Initials"
  .Range("C1").Value = "Surname"
  .Range("A1:C1").Font.Bold = True
  .Range("A2").Value = "Mr."
  .Range("B2").Value = "J"
  .Range("C2").Value = "Smith"
End With

oWorkbook.SaveAs "c:\book1.xls"
oApplication.Quit	
This method is fine for small amounts of data, but each Range object results in an interface request which results in slow performance if large amounts of data are processed. There is a 64K limit on interface requests on Windows 95 and 95 which results in which Excel may stop responding. Another way to transfer data is in an array.

Dim oApplication as Object
Dim oWorkBook as Object
Dim oWorksheet as Object

' Create instance of Excel Object, and Create a workbook
Set oApplication = CreateObject("Excel.Application")
Set oWorkBook = oApplication.Workbooks.Add

' Create array with 3 columns and 100 rows
Dim DataArray(1 To 100, 1 To 3) As Variant
Dim i as Integer
For i = 1 To 100
  DataArray(i, 1) = "ProdID" & Format(r, "0000")
  DataArray(i, 2) = Rnd() * 1000
  Data Array(i,3) = DataArray(i, 2) * 0.175
Next
	
' Create Worksheet Object
Set oWorksheet = oWorkbook.Worksheets(1)

' Add Headers
oWorksheet.Range("A1:C1").Value = Array("Product ID", "Value", "Tax")

' Transfer DataArray to Worksheet
oWorksheet.Range("A2").Resize(100, 3).Value = DataArray

' Save Worksheet and Quit Excel
oWorkbook.SaveAs "c:\book1.xls"
oApplication.Quit	
  • Omit the variable name from the Next statement in a loop will speed up the code.

  • Set the Compiler option 'Favour Pentium Pro' to take advantage of the Pentium Pro instruction set over the normal Pentium one.