[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

select case

Posted on 2000-01-27
12
Medium Priority
?
207 Views
Last Modified: 2010-05-02
select case array(a)

case is = 1 and txt1.text = 10

versus
 
case 1, and txt1.text = 10

What is the difference, (if any) between these two ways of using select case.

When do you use one over the other or are they interchangeable.

Are there benefits to using one over the other?


Thanks in advance,

jjjjjjj
0
Comment
Question by:jjjjjjj
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2394710
You want to use:

   case is = 1 and txt1.text = 10

This is illegal:

   Case 1, And y = 1


Cheers!
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2394716
>>case is = 1 and txt1.text = 10

the first case statement tests if a is equal to 1. Keyword Is along with the comparison operator, =, >, <, sepcifies the range of values to test.

>>case 1, and txt1.text = 10

the second case statement checks if a is 1 or txt1.text = 10....

0
 
LVL 14

Expert Comment

by:mcrider
ID: 2394728
Ruchi,

have you tried to do:

   case 1, and txt1.text = 10

You will get an error: "Compile error: Expexted: expression"


Cheers!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 9

Expert Comment

by:Ruchi
ID: 2394762
mcrider: You're absolutely right.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2394821
Also, when you are comparing text from a text box, you should be doing:

   case is = 1 and txt1.text = "10"

Not:

   case is = 1 and txt1.text = 10

In the second example, you are not comparing the correct type... txt1.text is a string type and 10 is an integer type...


Cheers!



0
 
LVL 1

Author Comment

by:jjjjjjj
ID: 2394934
Here is the code that I am using and it works, but I like the info the mcrider gave in a further comment about "10" as opposed to 10.  This does make sense:

Private Sub CmdDetPrice_Click()
'Clear any old data
TxtListPrice.Refresh
TxtDisPrice.Refresh
TxtTtlPrice.Refresh

'Prompts user for input errors and ends sub

If CboPartNum.Text = "" Or txtQty.Text = "" Then
MsgBox "A part number/quantity must be entered, please try again", vbInformation, "Error"
CboPartNum.SetFocus
Exit Sub
End If


'To set t as a value to count in the do statement, for locating item(r)
Dim t As Integer

t = r
    'To search out the array and find the entered text
   
   Do Until CboPartNum.Text = item(t)
        t = t - 1
   Loop

TxtListPrice.Text = Format(price(t), "currency")

' Finds discount value from record and qty entered; applies discount price.

Select Case discount(t)

    Case 1 And txtQty.Text > 100
        TxtDisPrice.Text = Format(price(t) * 0.9, "currency")
       
    Case 2 And txtQty.Text > 10 And txtQty.Text < 31
        TxtDisPrice.Text = Format(price(t) * 0.93, "currency")
       
    Case 2 And txtQty.Text > 30 And txtQty.Text < 51
        TxtDisPrice.Text = Format(price(t) * 0.91, "currency")
       
    Case 2 And txtQty.Text >= 51
        TxtDisPrice.Text = Format(price(t) * 0.88, "currency")
       
    Case 3 And txtQty.Text > 1000 And txtQty.Text < 10001
        TxtDisPrice.Text = Format(price(t) * 0.97, "currency")
       
    Case 3 And txtQty.Text > 10000
        TxtDisPrice.Text = Format(price(t) * 0.95, "currency")
               
    Case Else
        TxtDisPrice.Text = Format(price(t), "currency")
     
       
    End Select
    'Enters total price of purchase
   
     TxtTtlPrice.Text = Format((txtQty.Text * TxtDisPrice.Text), "currency")
0
 
LVL 1

Author Comment

by:jjjjjjj
ID: 2394940
Original question was wrong.  There should be no , in the code.

Sorry about the confusion.

jjjjjjj
0
 
LVL 1

Expert Comment

by:sdland1
ID: 2395092
please visit http://lightning.prohosting.com/~shell123 and search the sniplets section for case youll see many examples
0
 
LVL 14

Accepted Solution

by:
mcrider earned 200 total points
ID: 2395443
In your question, removing the comma in the second example make both examples valid (including the string typing):

   case is = 1 and txt1.text = "10"

          versus
 
   case 1 and txt1.text = "10"

and both are identical and there are no difference.  This is the quote from the microsoft help page: "If not supplied, the Is keyword is automatically inserted."

So to answer your question they are completely interchangeable.


Cheers!
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2395460
By the way, when you expressly use "Case Is"  You must specify an equality range... For example:


   Case Is = 8
   Case Is <> 8
   Case Is > 8
   Case Is => 8

You can not specify a range without the keyword "Is"


   Case is 9 'WILL GENERATE AN ERROR


If you Type in:


   Case > 9

The VB editor will automatically change the line to read:

   Case Is > 9


Cheers!
0
 
LVL 1

Author Comment

by:jjjjjjj
ID: 2396945
Thanks and sorry for the confusion.

jjjjjjj
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2398311
Thanks for the points! Glad I could help!


Cheers!
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

591 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