Select Case

Valedlobo

New member
Local time
Today, 19:52
Joined
Dec 4, 2005
Messages
9
Hi, this is my first post so go easy on me please.

I have done some very basic VB in the past and am trying to start from the basics again.

Can anyone tell me what I am doing wrong here? Basically all I am tring to do is to input a number between 15.00 and 59.99 and then round it up and output to another text box?

Any idea why this isn't working?

Regards

Matt



Code:
Private Sub Command0_Click()
Dim oldValue As Integer
Dim newValue As Integer

oldValue = Text1.Value
newValue = Text7.Value


Select Case oldValue

   Case 1 To 14.99
      GoTo Error
   Case 15 To 19.99
      newValue = 19.99
   Case 20 To 24.99
      newValue = 24.99
   Case 25 To 29.99
      newValue = 29.99
   Case 30 To 34.99
      newValue = 34.99
   Case 35 To 39.99
      newValue = 39.99
   Case 40 To 44.99
      newValue = 44.99
   Case 45 To 49.99
      newValue = 49.99
   Case 50 To 54.99
      newValue = 54.99
   Case 55 To 59.99
      newValue = 59.99
   Case Else
      GoTo Error

Text7.Text = newValue

Error: MsgBox ("That is not a valid number")


End Select
End Sub
 
Last edited:
Integers can only store whole numbers. You need to define old and new as maybe Currency or Single/Double to use a decimal.
 
RuralGuy said:
Integers can only store whole numbers. You need to define old and new as maybe Currency or Single/Double to use a decimal.
Thanks I have changed that, but when I click button0, Text7 remains blank, I am not sure I have coded the result to be output properly??
 
Don't use the Text property of Text7. Use the Value property instead. The control must have the focus in order to use the Text property. The Value property is the default property so you do not have to specify it:
Me.Text7 = newValue
will work.
 
Also Text7.Text = newValue is in the Else condition and would only be executed if none of the other Case conditions is true. However, even if the Else condition is true the GoTo Error will jump it.

In other words no condition executes the Text7.Text = newValue line, which is the reason no error is raised on the Text box not having the focus. It can’t have the focus because a button was just clicked.

Regards,
Chris.
 
Good points Chris. Thanks for pointing them out.
 
Also, you probably want a: "Exit Sub" before the Error: :)
 
What about
Code:
If me.Text1< 15 Or  me.Text1 > 59.99 Then
   MsgBox "Invalid Value"
   Exit Sub
Else
    me.Text7= (Int(me.Text1 / 5) + 1) * 5 - 0.01
End If

HTH

Peter
 
Bat17 said:
What about
Code:
If me.Text1< 15 Or  me.Text1 > 59.99 Then
   MsgBox "Invalid Value"
   Exit Sub
Else
    me.Text7= (Int(me.Text1 / 5) + 1) * 5 - 0.01
End If

HTH

Peter

Cheers that is spot on - thing I overcomplicate things sometimes.
 
Case was the right wat to go though if the values were iregular.

Peter
 
What anout something like:

Select Case oldValue
Case < 15
GoTo Error
Case < 19

etc...
 

Users who are viewing this thread

Back
Top Bottom