Results 1 to 13 of 13

Thread: Select case for option buttons

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194

    Select case for option buttons

    I have an array of 3 option buttons Option1(0), Option(1) and Option(2) and a textbox txtNum

    Depending on which option button is chosen and what number is input into text box, I want to write a select case: (But I can't see where I'm going wrong! Any hints or tips folks?
    Code:
    Private Sub cmdSubmit_Click()
    
    Dim Wholesale as String
    Dim price As String
    Dim Total As Integer
    
    Select Case Wholesale ' Wholesale is the Caption of Option1(0)
    Case "1"
    
        If txtNumRequired <= 5 Then
        price = 50
        Total = txtNumRequired * price
        End If
        
    Case "2"
    
        If txtNumRequired >= 6 And txtNumRequired <= 10 Then
        price = 45
        Total = txtNumRequired * price
    
    End Select
    
         lblDisplayType.Caption = Option1(0).Tag
         lblDisplay.Caption = "You ordered " + txtNumRequired + " Items" '+ "The cost will be " + Total
         lblDisplayPrice.Caption = Total
    
    End Sub
    appreciation for any comments

  2. #2
    Hyperactive Member Stiletto's Avatar
    Join Date
    Aug 2002
    Location
    Jerusalem, Israel
    Posts
    287
    Do you get any error? if yes, what it says?
    Replace Wholesale with Option(0).Caption

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194
    I get a compile error:
    End Select without select case

  4. #4
    Hyperactive Member Stiletto's Avatar
    Join Date
    Aug 2002
    Location
    Jerusalem, Israel
    Posts
    287
    Under Case "2", After the If sentence, add End If.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194

    Red face

    thanks for that - can't see the wood for the trees

    Can u see why the total is not being displayed in the label?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194
    just used step into and it is skipping the if statements - justs jumps from case 1 to case 2

  7. #7
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    I really don't understand what you are trying to do. But it will definatly skip the case statements.

    You dim a variable "Wholesale" as a string. Wholesale is now an EMPTY string.

    You then run select to check the contents of Wholesale to see if it is 1 or 2. It is neither. It is just an empty string.

    Maybe if you explain a bit more what you are trying to do, we can help you with the code.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194
    2 option butts = wholesaler, retailer
    1 checkbox = special customer
    if option1(0) wholesaler then prices 4 item:
    if buying 1 -5 item then 50.00 each
    6-10 items then 45.00 each
    11-20 items then 40.00

    if option1(1) retailer
    if buying 1 -3item then 60.00 each
    4-8 items then 55.00 each
    9 - 15 iems then 50.00 each

    if check1 special customer then 10% discount on total

    so far I have :
    Code:
    Option Explicit
    
    Private Sub cmdSubmit_Click()
    
    Dim Wholesale As String
    Dim price As String
    Dim Total As Integer
    
    Select Case txtNumRequired.Text
    Case Is <= 5
    
        If txtNumRequired <= 5 Then
        price = 50
        Total = txtNumRequired * price
        End If
        
    Case Is > 5 ' need to say >5 and <=10 but it won't work!
    
        If txtNumRequired >= 6 And txtNumRequired <= 10 Then
        price = 45
        Total = txtNumRequired * price
        End If
    End Select
    
         lblDisplayType.Caption = Option1(0).Tag
         lblDisplay.Caption = "You ordered " + txtNumRequired + " Items" '+ "The cost will be " + Total
         lblDisplayPrice.Caption = Total
         
    
    End Sub

  9. #9
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    VB Code:
    1. Case Is > 5, Is <= 10
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194
    thank you very much

  11. #11
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Some other pointers for you:

    1. You set price to a string, but then assigned it a number, could cause lots of problems down the road
    2. Prefix your variables! Strings should begin with str, integers should begin with int. It will keep things much easier to follow as your program grows.
    3. Use Val() when reading from text boxes. If the user puts in text, it could cause problems.
    4. The if statements where redundant. You already checked what txtNumRequired was in your select case statement

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdSubmit_Click()
    4.  
    5. Dim strWholesale As String
    6. Dim curPrice As Currency
    7. Dim curTotal As Currency
    8.  
    9.     Select Case Val(txtNumRequired.Text)
    10.     Case Is <= 5
    11.    
    12.         curPrice = 50
    13.         curTotal = Val(txtNumRequired.Text) * curPrice
    14.            
    15.     Case Is > 5, Is <= 10
    16.    
    17.         intPrice = 45
    18.         curTotal = Val(txtNumRequired.Text) * curPrice
    19.        
    20.     End Select
    21.    
    22.     lblDisplayType.Caption = Option1(0).Tag
    23.     lblDisplay.Caption = "You ordered " + Val(txtNumRequired.Text) + " Items" '+ "The cost will be " + Total
    24.     lblDisplayPrice.Caption = curTotal
    25.  
    26.  
    27. End Sub
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  12. #12
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by mbonfyre
    thank you very much
    You are quite welcome!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    Propped up at a PC near you...
    Posts
    194

    Smile

    I was just in the process of removing the if statements as I realised that they were not needed now, thanks for the tip re: prefixing variables etc. I take you point about val() - will use it and then look up what it means/how it works

    God I LOVE this site and its helpful people

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width