Link to home
Start Free TrialLog in
Avatar of jjjjjjj
jjjjjjj

asked on

select case

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
Avatar of mcrider
mcrider

You want to use:

   case is = 1 and txt1.text = 10

This is illegal:

   Case 1, And y = 1


Cheers!
>>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....

Ruchi,

have you tried to do:

   case 1, and txt1.text = 10

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


Cheers!
mcrider: You're absolutely right.
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!



Avatar of jjjjjjj

ASKER

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")
Avatar of jjjjjjj

ASKER

Original question was wrong.  There should be no , in the code.

Sorry about the confusion.

jjjjjjj
please visit http://lightning.prohosting.com/~shell123 and search the sniplets section for case youll see many examples
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
Avatar of jjjjjjj

ASKER

Thanks and sorry for the confusion.

jjjjjjj
Thanks for the points! Glad I could help!


Cheers!