Results 1 to 7 of 7

Thread: case select question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    case select question

    Hello

    I originally had the following code
    Code:
     Select Case txtCar.Text
            Case is <= 2500
                msg = MsgBox("Car Price not recommended for this model. Are you sure you want to use this Price", vbQuestion)
            Case is <= 4000
                save_parameters
                NodeBAll.SaveTab2.Enabled = False
                NodeBAll.MousePointer = 0
                'do nothing
            Case Else
                msg = MsgBox("Car Price not recommended for this model. Are you sure you want to use this Price", vbQuestion)
        End Select
    whenever i had a txtcar value of say 35000, it would still og through the second case (case is <=4000). i changed the code to the code below

    Code:
     Select Case txtCar.Text
            Case txtCar.Text <= 2500
                msg = MsgBox("Car Price not recommended for this model. Are you sure you want to use this Price", vbQuestion)
            Case txtCar.Text <= 4000
                save_parameters
                NodeBAll.SaveTab2.Enabled = False
                NodeBAll.MousePointer = 0
                'do nothing
            Case Else
                msg = MsgBox("Car Price not recommended for this model. Are you sure you want to use this Price", vbQuestion)
        End Select
    and it works, i was wondering why does not the is operator work?

  2. #2
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: case select question

    because you are comparing the textbox directly means you are dealing with a string and by doing so that means that 35000 is smaller than 4000, try using this line instead.
    VB Code:
    1. Select Case Val(txtCar.Text)

    casey.

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: case select question

    and it works,
    Does it! It may now work for a value of 35000 but now the case statement won't work for other values (try 1).

    A Select Case statement looks for a True value. Think of it like

    txtCar.Text = txtCar.Text <= "4000"
    which evaluates to
    "35000" = "35000" <= "4000"
    which evaluates to
    "35000" = "True"
    which of course is False - thus the Case Else fires.

    When you compare numeric strings, "35000" is less than "4000".

    Change to a numeric comparison.

    VB Code:
    1. Select Case Val(txtcar.Text)
    2.         Case Is <= 2500
    3.             MsgBox ("<=2500")
    4.         Case Is <= 4000
    5.             MsgBox ("<=4000")
    6.         Case Else
    7.             MsgBox ("Else")
    8.     End Select

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: case select question

    thanks for the reply
    i did not understand your explanation

    txtCar.Text = txtCar.Text <= "4000"
    which evaluates to
    "35000" = "35000" <= "4000"
    which evaluates to
    "35000" = "True"

    should not my code
    Code:
    Select Case txtCar.Text
            Case txtCar.Text <= 2500
                msg = MsgBox("Car Price not recommended for this model. Are you sure you want to use this Price", vbQuestion)
            Case txtCar.Text <= 4000
                save_parameters
                NodeBAll.SaveTab2.Enabled = False
                NodeBAll.MousePointer = 0
                'do nothing
            Case Else
                msg = MsgBox("Car Price not recommended for this model. Are you sure you want to use this Price", vbQuestion)
        End Select
    read as follows for a txtcar.text value of "35000"

    case txtcar.text <= 4000
    which would result in

    case "35000" <= 4000

    which would result in False and would go to the last statement
    i did not understand where we get the extra "35000"
    "35000" = "35000" <= "4000"


    also how does
    Case is <= 2500 differ from Case txtCar.Text <= 2500


    i guess use of

    Code:
    Select Case Val(txtCar.Text)
    woudl result in the following implementation

    Code:
    Select Case Val(txtcar.Text)
            Case Is <= 2500
                MsgBox ("<=2500")
            Case Is <= 4000
                MsgBox ("<=4000")
            Case Else
                MsgBox ("Else")
        End Select
    as well as

    Code:
    Select Case Val(txtcar.Text)
            Case Val(txtcar.Text)<= 2500
                MsgBox ("<=2500")
            Case Val(txtcar.Text)<= 4000
                MsgBox ("<=4000")
            Case Else
                MsgBox ("Else")
        End Select

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: case select question

    my original code seems to be working now

    Select Case txtCar.Text
    Case is <= 2500
    msg = MsgBox("Car Price not recommended for this model. Are you sure you want to use this Price", vbQuestion)
    Case is <= 4000
    save_parameters
    NodeBAll.SaveTab2.Enabled = False
    NodeBAll.MousePointer = 0
    'do nothing
    Case Else
    msg = MsgBox("Car Price not recommended for this model. Are you sure you want to use this Price", vbQuestion)
    End Select
    !!! how?
    i just copy pasted the code

  6. #6
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: case select question

    The text box gives you a string value for testing, not a numeric value
    You should use

    Select Case Val(txtCar.Text)

    Like vbasicgirl sugested.

    Try this, maybe you'll see what we mean.

    Code:
    debug.Print asc("3")

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: case select question

    they both seem to work now !

    i will try debug.print asc("3")

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