VBA Brush Up 05:Decision Making with VBA

Technorati 标签: , ,

来自:Julitta Korol,“Access.2003.Programming.by.Example.with.VBA.XML.and.ASP”,by Wordware Publishing, Inc. 2005, p76-p90

(1)if then结构:单行的不需要end if,多行的需要;单行的如果需要执行多个语句,则加冒号

  1. Sub SimpleIfThen2()
  2.     Dim weeks As String
  3.     On Error GoTo VeryEnd 
  4.     weeks = InputBox("How many weeks are in a year:""Quiz"
  5.     If weeks <> 52 Then MsgBox "Try Again":SimpleIfThen2 
  6.     If weeks = 52 Then MsgBox "Congratulations!"
  7. VeryEnd: 
  8. End Sub 

If condition Then statement1 Else statement2

(2)elseif以及Nested If…Then Statements的用法

  1. Private Sub cmdOK_Click()
  2.     If txtPwd = "FOX" Then 
  3.         MsgBox "You are not authorized to run this report." 
  4.     ElseIf txtPwd = "DOG" Then 
  5.         If txtUser = "John" Then
  6.             MsgBox "You are logged on with restricted privileges." 
  7.         ElseIf txtUser = "Mark" Then 
  8.             MsgBox "Contact the Admin now." 
  9.         ElseIf txtUser = "Anne" Then 
  10.             MsgBox "Go home." 
  11.         Else 
  12.             MsgBox "Incorrect User name."
  13.         End If 
  14.     Else 
  15.         MsgBox "Incorrect password or user name" 
  16.     End If 
  17.     Me.txtUser.SetFocus
  18. End Sub

Here’s the syntax of the If…Then…ElseIf statement:

  1. If condition1 Then 
  2.     statements to be executed if condition1 is True 
  3. ElseIf condition2 Then 
  4.     statements to be executed if condition2 is True 
  5. ElseIf condition3 Then 
  6.     statements to be executed if condition3 is True 
  7. ElseIf conditionN Then 
  8.     statements to be executed if conditionN is True 
  9. Else 
  10.     statements to be executed if all conditions are False 
  11. End If 

The Else clause if optional; you can omit it if there are no actions to be executed when all conditions are false. If the condition is true, Visual Basic will execute the statements between Then and Else, and will ignore the statement between Else and End If. If the condition is false, Visual Basic will omit the statements between Then and Else, and execute the statement between Else and End If.

(3)Select Case Statement:To avoid complex nested If statements that are difficult to follow, you can use the Select Case statement instead. The syntax of this statement is as follows, The Case Else clause is optional. VB执行完match的case之后就立刻退出select.

  1. Select Case testExpression 
  2.     Case expressionList1 
  3.         statements if expressionList1 matches testExpression 
  4.     Case expressionList2 
  5.         statements if expressionList2 matches testExpression 
  6.     Case expressionListN 
  7.         statements if expressionListN matches testExpression 
  8.     Case Else 
  9.         statements to be executed if no values match testExpression 
  10. End Select

(4)在case里用Is做判断、以及Specifying a Range of Values in a Case Clause。once Visual Basic locates a Case clause with a true condition, it doesn’t bother to look at the remaining Case clauses. It jumps over them and continues to execute the procedure with the instructions that may follow the End Select statement.

  1. Select Case unitsSold 
  2.     Case 1 To 100 
  3.         Discount = 0.05 
  4.     Case Is <= 500 
  5.         Discount = 0.1 
  6.     Case 501 To 1000 
  7.         Discount = 0.15 
  8.     Case Is >1000 
  9.         Discount = 0.2 
  10. End Select

(5)Specifying Multiple Expressions in a Case Clause。You may specify multiple conditions within a single Case clause by separating each condition with a comma。The commas used to separate conditions within a Case clause have the same meaning as the OR operator used in the If statement. The Case clause is true if at least one of the conditions is true.

  1. Select Case myMonth 
  2.     Case "January""February""March" 
  3.         Debug.Print myMonth & ": 1st Qtr." 
  4.     Case "April""May""June" 
  5.         Debug.Print myMonth & ": 2nd Qtr." 
  6.     Case "July""August""September" 
  7.         Debug.Print myMonth & ": 3rd Qtr." 
  8.     Case "October""November""December" 
  9.         Debug.Print myMonth & ": 4th Qtr." 
  10. End Select

(6)Here are a few guidelines to help you determine what kind of conditional statement you should use:

    • If you want to supply only one condition, the simple If…Then statement is the best choice.
    • If you need to decide which of two actions to perform, use the If…Then…Else statement.
    • If your procedure requires two or more conditions, use the If…Then…ElseIf or Select Case statements.
    • If your procedure has many conditions, use the Select Case statement. This statement is more flexible and easier to comprehend than the If…Then…ElseIf statement.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值