case select drops through
Microsoft Office Training verified by visa - mastercard securecode about microsoft training company london ukadd this page to your favourites/bookmarksAdd to favourites
view a printable version of this pagePrintable version
email this page to somebodyEmail this page
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

The UK's most regular instructor-led training courses.
Training information: introduction access training · Microsoft access courses · Microsoft Office VBA training

resolvedResolved · Medium Priority · Version 2003

Case select drops through valid cases to case else

Peter has attended:
Access Advanced course
Access VBA course

by - delegate Peter [2 posts] (2008 May 9 Fri, 09:38) replyReply

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

Access VBA 2 day course
Version Date Location Places
available
Book Next place rate:
Card Invoice
2003 2008 Dec 4 Thu + 5 Fri Southwark 7 book now £345 £399
2003 2009 Jan 8 Thu + 9 Fri Southwark 6 book now £450 £485
2003 2009 Feb 5 Thu + 6 Fri Southwark 8 book now £275 £399
2003 2009 Mar 5 Thu + 6 Fri Southwark 7 book now £345 £399
2003 2009 Apr 8 Wed + 9 Thu Southwark 8 book now £275 £399
2003 2009 May 7 Thu + 8 Fri Southwark 8 book now £275 £399
Full Schedule: See all 12 Access VBA course dates.
Bookings currently available until 5th November 2009.

RE: Case select drops through valid cases to case else

by - trainer Stephen gold contributer[369 posts] (2008 May 9 Fri, 12:18) replyReply

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

by - delegate Peter [2 posts] (2008 May 13 Tue, 17:41) replyReply

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

by - trainer Roy bronze contributer[28 posts] (2008 May 12 Mon, 12:27) replyReply

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


Related articles

· Microsoft Access Cheat Sheet: Tips and Shortcuts
· Quick MS Access Training: Creating Custom Database Tables in 12 Simple Steps
· Reasons to Consider Microsoft Access Training
· 3 Useful Things You'll Learn in Microsoft Access Training Courses
· Innovation with Microsoft Access

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

Access tip:

Combo box for finding records

You can use a combo box in a form to look up a record. If you place the comb box in the Form header, by then selcting they a field type from the drop down loist, you will see the record for it

View all Access hints and tips

Institute of IT Training - Accredited Training Provider Microsoft Certified Partner
microsoft office
Microsoft Office Specialist Authorised Testing Centre (MOS and MCAS)

Prodigy Platinum Learning Partner

Institute of IT Training - Accredited Training Provider Association of Computer Trainers Valid HTML 4.01 Transitional
Valid CSS Markup

secure online payments - visa - mastercard

Mini sitemap. These are the main areas of our web site. Full sitemap.

Training by application Main information pages See also

Access courses
DreamWeaver courses
Excel courses
MS Project courses
Outlook courses
PowerPoint courses
VBA courses
Word courses
(more...)

Public scheduled courses
On-site training
Closed company courses

Microsoft Office training
Pricing and availability
Training schedule
Training venues

Access training
Dreamweaver training
Excel training
MS Project training
PowerPoint training

London Computer Training
Computer Training London

Microsoft Access training
Microsoft Excel training
Microsoft Project training
Microsoft Outlook training
Microsoft Powerpoint training
Microsoft Word training

Time Management Course London

Interested in Access training? Please see the following pages:
microsoft access courses · microsoft training access course
microsoft+access+training · access courses in london

Training Information

Training Articles

AddThis Social Bookmark Button What's this?
Add to Del.icio.us Add to Facebook Add to Digg Add to Reddit Add to Google Add to Yahoo Add to Diigo Add to Mr. Wong Add to Linkarena Add to Power Oldie Add to Folkd Add to Jumptags Add to Upchuckr Add to Simpy Add to StumbleUpon Add to Slashdot Add to Netscape Add to Furl Add to Spurl Add to Blinklist Add to Blogmarks Add to Technorati Add to Newsvine Add to Blinkbits Add to Ma.Gnolia Add to Smarking Add to Netvouz