Results 1 to 5 of 5

Thread: CASE statements (beginner)

  1. #1
    adamr1001
    Guest

    CASE statements (beginner)

    I've got no help files, so excuse my novice question...

    What is the vb syntax for writing CASE expressions?... pertaining to the user's selection of an item in a combo box?

    Thanks a lot

    Adam.

  2. #2
    Addicted Member chrisvl's Avatar
    Join Date
    Jul 2001
    Location
    Belgium - Somewhere between the bedroom and the toilet...or at work playing Oracle DBA :-)
    Posts
    233
    eg if you want to check on Key:

    Select Case Key

    Case <value>
    code

    Case <another value>
    code
    ...
    ...

    End Select

  3. #3
    Fanatic Member Bonker Gudd's Avatar
    Join Date
    Mar 2000
    Location
    Saturn
    Posts
    748
    VB Code:
    1. Select Case cboCombo.Text
    2.  
    3.         Case 1
    4.             do stuff
    5.  
    6.         Case 2
    7.             do stuff
    8.  
    9.         Case Else
    10.             do stuff
    11.          
    12.     End Select

  4. #4
    Matthew Gates
    Guest
    Incase you wanted it, here is what is in VB's MSDN Library:


    Select Case Statement

    Description
    Executes one of several groups of statements, depending on the value of an expression.
    Syntax
    Select Case testexpression
    [Case expressionlist-n
    [statements-n]] . . .
    [Case Else expressionlist-n
    [elsestatements-n]]
    End Select
    The Select Case statement syntax has these parts:

    Part Description
    testexpression Any numeric or string expression.
    expressionlist-n Required if Case appears. Delimited list of one or more expressions.
    statements-n One or more statements executed if testexpression matches any part of expressionlist-n.
    elsestatements-n One or more statements executed if testexpression doesn't match any of the Case clauses.


    Remarks
    If testexpression matches any Case expressionlist expression, the statements following that Case clause are executed up to the next Case clause, or for the last clause, up to End Select. Control then passes to the statement following End Select. If testexpression matches an expressionlist expression in more than one Case clause, only the statements following the first match are executed.
    The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist in any of the other Case selections. Although not required, it is a good idea to have a Case Else statement in your Select Case block to handle unforeseen testexpression values. If no Case expressionlist matches testexpression and there is no Case Else statement, execution continues at the statement following End Select.

    Select Case statements can be nested. Each nested Select Case statement must have a matching End Select statement.

    The following example illustrates the use of the Select Case statement:

    Dim Color, MyVar
    Sub ChangeBackground (Color)
    MyVar = lcase (Color)
    Select Case MyVar
    Case "red" document.bgColor = "red"
    Case "green" document.bgColor = "green"
    Case "blue" document.bgColor = "blue"
    Case Else MsgBox "pick another color"
    End Select
    End Sub

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    I just wanted to point out that the MSDN Library (i.e all documentation and help files) are available on the Net.
    http://msdn.microsoft.com/library/

    Best regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width