case select drops through

Forum home » Delegate support and help forum » Microsoft Access VBA Training and help » Case select drops through valid cases to case else | Access forum

Case select drops through valid cases to case else | Access forum

resolvedResolved · Medium Priority · Version 2003

Peter has attended:
Access Advanced course
Access VBA course

Case select drops through valid cases to case else

What is wrong with this code ?
When I run it it drops through to Case Else
Public Sub TestCase()

Dim r As Integer
r = 1
Debug.Print "opening values ", r, vbOK, vbCancel
Select Case r
Case r = 1
Debug.Print " R = 1"
Case r = vbOK
Debug.Print " R = vbOK"
Case r = vbCancel
Debug.Print " R = vb Cancel"
Case Else
Debug.Print "else case values ", r, vbOK, vbCancel
End Select

End Sub

RE: Case select drops through valid cases to case else

Hi Peter

Thank You for your question

The third line of your code is in fact incorrect. Instead of

debug.print "Opening Values",r, vbOK, VbCancel

Should it not be

inputbox "Opening Values",r, vbOK, VbCancel

Your code is not falling through to case else, but is rather simple excecuting the third line of code

If this doesn't help, feel free to get back to me

Regards

Stephen

RE: Case select drops through valid cases to case else

Dont see that - in particular I dont see why inputbox is necessary to set the opening values.
When I run it the results from the immediate window are:
opening values 1 1 2
else case values 1 1 2

Which suggests to me that
Line 3 is executing
and that all the variables have their expected values
and then that
it is dropping thru to the case else line

Peter

RE: Case select drops through valid cases to case else

Peter,

You've been caught out by VBA's syntactic confusion between = as assignment and = as a boolean equality expression. This is bad language design (most other languages distinguish these in some way, either := for assignment, or == for equality).

The syntax for what you want is:

Select Case r
Case 1 'NOT Case r = 1
...

The VBA syntax allows Case X, where X is any expression, not just a literal value. So if r equals 1, then it would match

Case (4 - 3)

So in your code X is the boolean expression 'r = 1', which happens to be True. Since r does not equal True, the case fails.

Now, you might wonder how a Boolean expression can type-match an Integer. The problem is that True and False are implemented as Integers. Specifically, False is zero. To show this, try this:

Public Sub TestCase2()

Dim r As Integer

r = 0
Debug.Print "opening values ", r

Select Case r
Case False
Debug.Print "0 = False"
Case Else
Debug.Print "else case values ", r
End Select

End Sub

You should get the output "0 = False".

This has to be regarded as a serious fault with the language, in my opinion.

/Roy

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Access tip:

Related tables

When you have related tables such as Customers and their Orders, the Customer table is the Primary table.

Open the Customers table in datasheet view and go to the Home Tab and Records group. Click on the More option and choose Subdatasheet and then click on Subdatasheet again. Now choose the related table (Orders) and click ok.

Now you can click the + symbol by each company to show the related orders.

View all Access hints and tips


Server loaded in 0.05 secs.