Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need help with case statement

Posted on 2003-03-01
9
Medium Priority
?
181 Views
Last Modified: 2010-05-01
I need to do 2 things with case and was wondering how. First suppose a case is true , how can I exit from it and resume evaluating the next case or goto the default case (case else)?

E.g I need to do something like:
case vbkeyA
  if (DoNotHandleA=TRUE) then Proceed to default case.

I also need to do something like as follows in the keydown event and was wondering if its possible:

 Case (KeyCode = 191) And (Shift = 1)

When I try the above code vb always evaluates the case statement to true and executes the code in it irrespective of the condition.
0
Comment
Question by:sambha03
  • 4
  • 3
  • 2
9 Comments
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8050195
dim bHandled as boolean

Select case KeyCode:
    Case vbKeyA:
       if Shift = 1 then
          'Do something
          bHandled =true
       End if
    case Else:
       'Do nothing pick it up later
End select

if not bHandled then
   'Do default
end if
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8050204
Or

Select case KeyCode:
   Case vbKeyA and not DoNotHandleA:
      'Do something
      bHandled =true
   case 191:
      if Shift = 1 then
         'Do something
         bHandled = true
      End if
   End select

if not bHandled then
  'Do default
end if
0
 

Author Comment

by:sambha03
ID: 8050277
I have something like the following already. Is it not possible to evaluate shift==1 in the case statement itself?
case 191:
     if Shift = 1 then
        'Do something
        bHandled = true
     End if
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:sambha03
ID: 8050289
Regarding implementing
case vbkeyA
 if (DoNotHandleA=TRUE) then Proceed to default case

as
Case vbKeyA and not DoNotHandleA:

Thanks but its not exactly the solution i m looking for since I set DoNotHandleA to true/false after evaluating lots of conditions over many lines of code. Is it not possible to transfer out of the case statement to the next case statement after one case statement has already been evaluted to true?

0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8050513
Is it not possible to transfer out of the case statement to the next case statement after one case statement has already been evaluted to true?


No

Use

if not bHandled then
  'Do default
end if

I have something like the following already. Is it not possible to evaluate shift==1 in the case statement itself?

Try using the keyup event.

0
 

Expert Comment

by:qwasted
ID: 8058238

   Select Case KeyCode
      Case vbKeyR, vbKeySpace
          GoTo other
      Case vbKeyE
other:
        MsgBox "'E' pressed."
   End Select

I know. GoTo is foo paux.
0
 

Accepted Solution

by:
qwasted earned 200 total points
ID: 8063165
for your other question, about evaluating keycode and shift in a case ... could you use an If..Then..ElseIf..Then statement instead.

or maybe...


Dim DoNotHandleA As Boolean, KeyCodeHandled As Boolean
Dim OopsShouldBeNKey As Boolean
If vbNo = _
    MsgBox("Handle A?", _
        vbQuestion + vbYesNo, _
        "Question") Then DoNotHandleA = True
If vbYes = _
    MsgBox("You pressed 'B' key, did you mean to press 'N' Key?", _
        vbQuestion + vbYesNo, _
        "Question") Then OopsShouldBeNKey = True

Select Case KeyCode
    Case vbKeyA
        If DoNotHandleA Then
            ' use goto statement to advance to 'default' line-label
            GoTo default
        Else
            MsgBox "A or a"
            KeyCodeHandled = True
        End If
    Case vbKeyB
        If OopsShouldBeNKey Then
            GoTo NCase
        Else
            MsgBox "B or b"
            KeyCodeHandled = True
        End If
End Select
If Not KeyCodeHandled Then
    Select Case KeyCode = 191 And Shift = 1
        Case True
            MsgBox "?"
            KeyCodeHandled = True
        Case False
    End Select
End If
If Not KeyCodeHandled Then
    Select Case KeyCode
        Case vbKeyD
            MsgBox "D or d"
        Case vbKeyE
            MsgBox "E or e"
        Case vbKeyN
NCase:
            MsgBox "N or n"
        Case Else
            ' another way to get to default case
default:
            MsgBox "Some other key"
    End Select
End If
0
 

Expert Comment

by:qwasted
ID: 8063176
Just noticed, that code will always say 'B' was pressed and ask if you meant to press 'N', it doesn't evaluate the case vbKeyB always because of it though.
0
 

Expert Comment

by:qwasted
ID: 8063229
It isn't transfering back into a select after a case has been found true, but i think it simulates what it is you are trying by stretching the cases over multiple select structures.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question