473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Understanding the Select Case Statement

lee123
556 512MB
hi there i want to understand the "Select Case Statement" fully and the way i think i would is to use it in a real situation such as using a certain "City and Zipcodes" lets say a user enters in a certain City and have the zipcode pop in the textbox. can anyone help me with this understanding?

lee123
Mar 13 '08 #1
11 33295
missinglinq
3,532 Expert 2GB
That's a poor example, because a city can have many, many Zipcodes. A much better example would be just the opposite! The user enters a Zipcode and the correct city would populate the textbox. This is good because it allows the demonstration of a number of ways presenting the case.

Expand|Select|Wrap|Line Numbers
  1. Private Sub ZipCode_AfterUpdate()
  2. Select Case ZipCode
  3.   Case 23139
  4.    Me.City = "Powhatan"
  5.   Case 23803 To 23805
  6.    Me.City = "Petersburg"
  7.   Case 23111, 23116
  8.    Me.City = "Mechanicsville"
  9.   Case 23220 To 23231, 23234
  10.    Me.City = "Richmond"
  11.   Case Else
  12.    Me.City = "Unknown"
  13. End Select
  14. End Sub
  15.  
"Powhatan" shows a single value for the case expression.

"Petersburg" shows the use with a range of values for the case expression.

"Mechanicsville" shows the use when multiple, non-consecutive values are present.

"Richmond" shows the use with a combination of consecutive and non-consecutive values.

The final Case Else allows for the event that none of the Case expressions has been entered.


Linq ;0)>
Mar 13 '08 #2
missinglinq
3,532 Expert 2GB
Here's another little known way to use Select Case.

In this case, an average is being derived at and Ratings assigned accordingly. If the PointAverage is above a certain value, the Rating is thus and so.

If the PointAverage was, for the sake of this demo, 6.0, then the Case expression would satisfy every Case, because 6.0 is greater than 0, 1.5, 2.5, 3.5, 4.5 and 5.5! So which value is assigned to the control Rating?

In the Select Case construct, if a value satisfies more than one Case, the first Case that is satisfied is the one that is executed! To take advantage of this, you construct your Select Case in descending order, i.e. with the highest value first. So in this case, Rating would be assigned "U."

Likewise, with a PointAverage of 4.7, Rating would be assigned "E" because Case Is > 4.5 is the first Case that 4.7 satisfies.

Expand|Select|Wrap|Line Numbers
  1. Select Case PointAverage
  2. Case Is > 5.5
  3.   Me.Rating = "U"
  4. Case Is > 4.5
  5.    Me.Rating = "E"
  6. Case Is > 3.5
  7.    Me.Rating = "D"
  8. Case Is > 2.5
  9.    Me.Rating = "C"
  10. Case Is > 1.5
  11.    Me.Rating = "B"
  12. Case Is > 0
  13.    Me.Rating = "A"
  14. End Select
Linq ;0)>
Mar 13 '08 #3
lee123
556 512MB
Thanks missinglinq this will help me out alot in my programming.

lee123
Mar 13 '08 #4
missinglinq
3,532 Expert 2GB
You can also use Select Case with string values and to do things other than assigning values to other controls. This example uses it to decide which reports to open:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ReportSelection_AfterUpdate()
  2.  
  3. Select Case ReportSelection
  4.  Case "New Personnel"
  5.    DoCmd.OpenReport "NewHires", acViewNormal
  6.  Case "Absentees"
  7.   DoCmd.OpenReport "MissedTime", acViewNormal
  8.  Case "Vacations"
  9.   DoCmd.OpenReport "RequestedTiemOff", acViewNormal
  10. End Select
  11.  
  12. End Sub
Linq ;0)>
Mar 13 '08 #5
lee123
556 512MB
hey linq i tried a little experiment with the select case method and it didn't work. i put two textboxes (unbound) and named one name & the other one Lastname, but when i did the code. it didn't work i entered it like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Name_AfterUpdate()
  2.  
  3.     Select Case Name
  4.         Case "jerry"
  5.             LastName = "goods"
  6.         End Select
  7.  
  8. End Sub
maybe i did this wrong but inorder for me to get the full understanding i wanted to try it out. so i did and what do you know it didn't work.

Then i did this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Name_AfterUpdate()
  2.  
  3.     Select Case Name
  4.         Case "FF"
  5.             LastName = "goods"
  6.         End Select
  7.  
  8. End Sub
And this one worked. why is that. I also sub. the "FF" with a number (With No Quotes) and nothing. but the two letters worked. is this strange or did i miss something about this.

lee123
Mar 14 '08 #6
NeoPa
32,556 Expert Mod 16PB
Firstly to respond to Linq's excellent and illustrative examples, but I will get back to your last question later Lee.

I recently found another way of using the Select Case construct for when the checks were not all comparing a single variable or state. It involves doing things logically 'A... about Face' if I can use that term. Otherwise think 'Backwards'.

Say you want to flag something to the operator if any of a number of conditions were true, then you could make something like :
Expand|Select|Wrap|Line Numbers
  1. Select Case True
  2. Case Var1 > 74, Var2 = "Success"
  3.   Call MsgBox("Success")
  4. Case Var1 > 49, Var2 = "Please Retry"
  5.   Call MsgBox("Please Retry")
  6. Case Else
  7.   Call MsgBox("Failure")
  8. End Select
Mar 14 '08 #7
NeoPa
32,556 Expert Mod 16PB
Your code looks fine in both cases Lee, although I would take a closer look at the indenting used either in my example or in Linq's. Using Select Case can be very powerful, but wrong indenting can make it very complicated to read correctly.

We don't know how it failed or what data was entered (I'm thinking case particularly here) so I can be little further help at this time except to say that the code looked as if it should work. Look to the data entered.

Alternatively, try :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Name_AfterUpdate()
  2.   Select Case UCase(Me.Name)
  3.   Case "JERRY"
  4.     LastName = "Goods"
  5.   End Select
  6. End Sub
Mar 14 '08 #8
lee123
556 512MB
hi neopa thanks i'll try it and thanks for the other example

lee123
Mar 14 '08 #9
missinglinq
3,532 Expert 2GB
That's an interesting application of it, Ade!

lee123, go into your code window behind your form and go to the very top of it. I think you'll either not see any Option Compare statement, or you'll see the statement
Option Compare Binary. Absent an Option Compare statement, it defaults to Binary. This means that all comparison (like the Case statements) are case sensitive.

Change this statement or if missing simply enter Option Compare Database.
That should solve your problems.

Linq
;0)>
Mar 14 '08 #10
lee123
556 512MB
thanks linq I'll try it.

lee123
Mar 14 '08 #11
NeoPa
32,556 Expert Mod 16PB
I've split another (hijack) question off into its own thread. Interested parties will find it here (Find Using Wildcard).
Mar 14 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gadrin77 | last post by:
as a newbie to XSL, is it possible to mimic a SELECT/CASE statement using XSL? I tried a quickie and I kept getting errors either using PARAM or WITH-PARAM in the wrong place or VARIABLE. I...
7
by: Lucas Tam | last post by:
How do I do the following? Select Case(SomeValue) Case NOT A, M, Q, Y 'Do stuff if it is not A,M,Q,Y End Select
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
3
by: fanoftvb | last post by:
Hi, i have problem with my select case statement. When i click on the button, nothing happened. Can someone help me see where i go wrong, thanks a lot. :) Sub GridView1_RowCommand(ByVal sender...
3
by: bpw22az | last post by:
I am currently in the process of setting up an asp page that sends an inquiring student an email regarding his/her application status. The student enters his/her email address on a web page, the...
3
by: fran7 | last post by:
Hi I have this query. lngCategoryID=CLng(Request("CategoryID")) If lngCategoryID <> "" And lngCategoryID <> 0 Then Set connPostCardSoft=Server.CreateObject("ADODB.Connection") ...
5
by: John | last post by:
Hi I have the following criteria to calculate grade form marks; Fail = less than 50% Pass = 50% - 59% Pass = 60% - 69% Merit = 70% - 79% Merit = 80% - 89% Distinction = 90% - 100%
44
by: Desitech | last post by:
I am very new at this and trying to learn. I have built a database and on my main form, I have a combo box titled cbodocumenttype, a text box for user input entitled txtuserinput, and a command...
3
by: CEO123 | last post by:
Does anyone know if it is possible and the syntax to test mutlitple expressions in a select case statement? This is the best I could come up with but it doesn't appear to be working Example: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.