case select drops through
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

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

Case select drops through valid cases to case else

resolvedResolved · Medium Priority · Version 2003

replyReply Fri 9 May 2008, 09:38Delegate Peter said...

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

For upcoming training course dates see: Pricing & availability

replyReply Fri 9 May 2008, 12:18Trainer Stephen said...

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

replyReply Tue 13 May 2008, 17:41Delegate Peter said...

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

replyReply Mon 12 May 2008, 12:27Trainer Roy said...

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

 

Please browse our web site to find out more about
introduction access training and other Microsoft training courses.

Access tip:

Closing form after a certain time period

To make a form close automatically after a certain time period, you need to assign the close function to a macro.

Save the macro and ope up the form in design view. open the Properties sheet.

In the TimerInterval property enter the length of time you want the form open for. This should be in milliseconds, so for instance if you want the form open for 5 seconds enter 5000, for an minute enter 60000.

You now need to attach your macro (to execute the Close action) to the OnTimer event property of the form.



View all Access hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard