Download Total Visual CodeTools Manual
Transcript
Do this:
If fInitialized Then
MsgBox "I am ready"
End If
The Code Cleanup feature lets you convert these single line IF
statements into the If..End If block.
See the Split Single-line IF Statements option on page 31 for more
details.
Use Else with Select Case
The Select Case construct makes it easy for your program to branch based
on multiple possible values for a variable or expression. Make sure to use a
Case Else clause in your Select Case blocks. Without a Case Else statement,
your code does not handle unanticipated values.
For example, assume your application allows the user to add new employee
categories, and your application uses VBA code to give employee raises
based on their job type. Your Select Case statement to handle raises may
look like this:
Select Case intEmployeeType
Case EmpType_Manager
intRaise = 10
Case EmpType_Clerical
intRaise = 5
Case EmpType_Driver
intRaise = 2
End Select
If the user adds a new “Programmer” category and hires a few
Programmers, they are not handled by this code.
If you follow the practice of always adding a Case Else clause, problems like
this are easier to handle. For example, the above code could be re-written
to prompt the user for the raise amount in the case of new employee types:
Select Case intEmployeeType
Case EmpType_Manager
intRaise = 10
Case EmpType_Clerical
intRaise = 5
Case EmpType_Driver
intRaise = 2
Case Else
Beep
intRaise = InputBox ("Enter raise amount")
End Select
Total Visual CodeTools
Appendix: Coding Techniques and Tips 171