• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

From Case to if...then....else

Can someone help me change this code from case to using if, then, else.  Thanks in advance!

__________________________________________________________

Private Sub cmdCalc_Click()
    Const conBtns As String = vbOKOnly + vbInformation _
        + vbDefaultButton1 + vbApplicationModal
    Const conMsg As String = _
            "The number of units must be greater than 0."
    Dim intRetVal As Integer, intUnits As Integer, sngTotal As Single
    intUnits = Val(txtUnits.Text)
    Select Case intUnits
        Case 1 To 4
            sngTotal = intUnits * 10
        Case 5 To 10
            sngTotal = intUnits * 9
        Case Is >= 11
            sngTotal = intUnits * 7
        Case Else
            intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
    End Select
    lblTotal.Caption = Format(sngTotal, "currency")
    txtUnits.SelStart = 0
    txtUnits.SelLength = Len(txtUnits.Text)
    txtUnits.SetFocus
End Sub

__________________________________________________________
0
GlobalFax
Asked:
GlobalFax
  • 14
  • 7
  • 6
  • +3
1 Solution
 
bobbit31Commented:
   intUnits = Val(txtUnits.Text)
    if intUnits > 0 and intUnits <= 4 then
            sngTotal = intUnits * 10
    else if intUnits >= 5 and intUnits <= 10 then
            sngTotal = intUnits * 9
    elese if intUnits >= 11 then
            sngTotal = intUnits * 7
    else
            intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
    End if
0
 
jjardineCommented:
Change your Select Case to the following


If intUnits < 0 And intUnits < 5 Then
    sngTotal = intUnits * 10
ElseIf intUnits > 4 And intUnits < 11 Then
    sngTotal = intUnits * 9
ElseIf intUnits >= 11 Then
    sngTotal = intUnits * 7
Else
    intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
End If
0
 
bobbit31Commented:
yeah sorry, i'm kind of drunk, the 'else if' should be 'elseif' ;)

btw, jjardine, shouldn't it be intUnits > 0 andintUnits < 5
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Arthur_WoodCommented:
why change the Select Case form.  It is actually more efficient, and easier to read.
0
 
GlobalFaxAuthor Commented:
jjardine, is the below line item needed as bobbit31 added?

intUnits = Val(txtUnits.Text)
0
 
GlobalFaxAuthor Commented:
If I replaced as above I get an "unexpected end sub"
0
 
GlobalFaxAuthor Commented:
Arthur Wood, I would agree if I we a programmer, I am just helping my younger sister with her homework.  I and a network engineer, so any help you can give me is greatly appreciated.  Look forward to any of you guys expertise help!

thanks   ;-)
0
 
jjardineCommented:
I apologize   yes the first line of mine should have been
If intUnits > 0 And intUnits < 5   sorry I used the wrong operator.

In regards to the line    intUnits = Val(txtUnits.Text)  you already have that.  It us used to grab the numeric values out of the text.

I agree with Arthur_Wood  with the Select being more efficient and easier to read.  I assume that the homework is specific on what it wants.

Please post the code that you now have  that is giving you the error.  Hope that helps
0
 
jjardineCommented:
I apologize   yes the first line of mine should have been
If intUnits > 0 And intUnits < 5   sorry I used the wrong operator.

In regards to the line    intUnits = Val(txtUnits.Text)  you already have that.  It us used to grab the numeric values out of the text.

I agree with Arthur_Wood  with the Select being more efficient and easier to read.  I assume that the homework is specific on what it wants.

Please post the code that you now have  that is giving you the error.  Hope that helps
0
 
GlobalFaxAuthor Commented:

Here it is in it's entirety as it stands
_________________________

Option Explicit

Private Sub cmdCalc_Click()
    Const conBtns As String = vbOKOnly + vbInformation _
        + vbDefaultButton1 + vbApplicationModal
    Const conMsg As String = _
            "The number of units must be greater than 0."
    Dim intRetVal As Integer, intUnits As Integer, sngTotal As Single
    intUnits = Val(txtUnits.Text)
    Select Case intUnits
        Case 1 To 4
            sngTotal = intUnits * 10
        Case 5 To 10
            sngTotal = intUnits * 9
        Case Is >= 11
            sngTotal = intUnits * 7
        Case Else
            intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
    End Select
    lblTotal.Caption = Format(sngTotal, "currency")
    txtUnits.SelStart = 0
    txtUnits.SelLength = Len(txtUnits.Text)
    txtUnits.SetFocus
End Sub

Private Sub cmdExit_Click()
    End
End Sub

Private Sub cmdPrint_Click()
    fraButtons.Visible = False
    PrintForm
    fraButtons.Visible = True
End Sub


Private Sub Form_Load()
    frmJacob.Left = (Screen.Width - frmJacob.Width) / 2
    frmJacob.Top = (Screen.Height - frmJacob.Height) / 2
End Sub


Private Sub txtUnits_GotFocus()
    txtUnits.SelStart = 0
    txtUnits.SelLength = Len(txtUnits.Text)
End Sub


0
 
GlobalFaxAuthor Commented:
btw, yes the homeowkr is specific in the task as recoding with the if, then else.
0
 
GlobalFaxAuthor Commented:
btw, yes the homeowkr is specific in the task as recoding with the if, then else.
0
 
sridhar12345Commented:
If intUnits = 0 then
   MsgBox(conMsg, conBtns, "Entry Error")
elseif intUnits < 5 Then
   sngTotal = intUnits * 10
ElseIf < 11 Then
   sngTotal = intUnits * 9
Else
   sngTotal = intUnits * 7
End If
0
 
GlobalFaxAuthor Commented:
OK jjardine,

I got it to function:

Now my dilemma is as follows:

When I click on the print button, I receive a runtime error '424.

And the other thing is that when the message: "The number of units must be greater than 0." appears after typing a 0 or < the message box appears as a flash and does not prompt the user to click OK to continue.


0
 
sridhar12345Commented:
Private Sub cmdCalc_Click()
   Const conBtns As String = vbOKOnly + vbInformation _
       + vbDefaultButton1 + vbApplicationModal
   Const conMsg As String = _
           "The number of units must be greater than 0."
   Dim intRetVal As Integer, intUnits As Integer, sngTotal As Single
   intUnits = Val(txtUnits.Text)

If intUnits = 0 then
  intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
  txtunits.setfocus
  exit sub

elseif intUnits < 5 Then
  sngTotal = intUnits * 10
ElseIf intUnits < 11 Then
  sngTotal = intUnits * 9
Else
  sngTotal = intUnits * 7
End If

   lblTotal.Caption = Format(sngTotal, "currency")
   txtUnits.SelStart = 0
   txtUnits.SelLength = Len(txtUnits.Text)
   txtUnits.SetFocus
End Sub
0
 
sridhar12345Commented:
Private Sub cmdCalc_Click()
   Const conBtns As String = vbOKOnly + vbInformation _
       + vbDefaultButton1 + vbApplicationModal
   Const conMsg As String = _
           "The number of units must be greater than 0."
   Dim intRetVal As Integer, intUnits As Integer, sngTotal As Single
   intUnits = Val(txtUnits.Text)

If intUnits = 0 then
  intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
  txtunits.setfocus
  exit sub

elseif intUnits < 5 Then
  sngTotal = intUnits * 10
ElseIf intUnits < 11 Then
  sngTotal = intUnits * 9
Else
  sngTotal = intUnits * 7
End If

   lblTotal.Caption = Format(sngTotal, "currency")
   txtUnits.SelStart = 0
   txtUnits.SelLength = Len(txtUnits.Text)
   txtUnits.SetFocus
End Sub
0
 
sridhar12345Commented:
Private Sub cmdCalc_Click()
   Const conBtns As String = vbOKOnly + vbInformation _
       + vbDefaultButton1 + vbApplicationModal
   Const conMsg As String = _
           "The number of units must be greater than 0."
   Dim intRetVal As Integer, intUnits As Integer, sngTotal As Single
   intUnits = Val(txtUnits.Text)

If intUnits = 0 then
  intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
  txtunits.setfocus
  exit sub

elseif intUnits < 5 Then
  sngTotal = intUnits * 10
ElseIf intUnits < 11 Then
  sngTotal = intUnits * 9
Else
  sngTotal = intUnits * 7
End If

   lblTotal.Caption = Format(sngTotal, "currency")
   txtUnits.SelStart = 0
   txtUnits.SelLength = Len(txtUnits.Text)
   txtUnits.SetFocus
End Sub
0
 
drydenhoggCommented:
Just as a side comment, instead of using format(value,"currency") use the VB6 formatcurrency command which is specifically designed for this, and is far more aware of locale than format.

Using Val() for the conversion is another can of worms, I wont open.

ADH.
0
 
GlobalFaxAuthor Commented:
sridhart2345,

If intUnits = 0 then
 intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
 txtunits.setfocus
 exit sub

I receive the below error when I add the above lines

_____

Compile Error:

Unexpected End Sub
0
 
drydenhoggCommented:
You havnt terminated the end if, so its telling you it encountered an 'end sub' line unexpectedly.

Add the end if into the code.

ADH.
0
 
jjardineCommented:
Option Explicit

Private Sub cmdCalc_Click()
   Const conBtns As String = vbOKOnly + vbInformation _
       + vbDefaultButton1 + vbApplicationModal
   Const conMsg As String = _
           "The number of units must be greater than 0."
   Dim intRetVal As Integer, intUnits As Integer, sngTotal As Single
   intUnits = Val(txtUnits.Text)
   If intUnits > 0 AND intUnits < 5 Then
           sngTotal = intUnits * 10
   ElseIf intUnits > 4 AND < 11 Then
           sngTotal = intUnits * 9
   ElseIf intUnits >= 11 Then
           sngTotal = intUnits * 7
   Else
           intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
   End If
   lblTotal.Caption = Format(sngTotal, "currency")
   txtUnits.SelStart = 0
   txtUnits.SelLength = Len(txtUnits.Text)
   txtUnits.SetFocus
End Sub

Private Sub cmdExit_Click()
   End
End Sub

Private Sub cmdPrint_Click()
   fraButtons.Visible = False
   PrintForm
   fraButtons.Visible = True
End Sub


Private Sub Form_Load()
   frmJacob.Left = (Screen.Width - frmJacob.Width) / 2
   frmJacob.Top = (Screen.Height - frmJacob.Height) / 2
End Sub


Private Sub txtUnits_GotFocus()
   txtUnits.SelStart = 0
   txtUnits.SelLength = Len(txtUnits.Text)
End Sub
0
 
GlobalFaxAuthor Commented:
Thanks jjardine, I will test the recommended changes this afternoon as I get home.  Will post update
0
 
jjardineCommented:
One more thing  I was just noticing..  In your button click event after your Case Statement you set your SelStart and SelLenth and then set Focus.  You also do this in your txtUnits.GotFocus event.  You don't have to have that after your Case Statement because it is goignt o happen in the GotFocus Event anyway.
0
 
GlobalFaxAuthor Commented:
jjardine

when the message box of
"The number of units must be greater than 0."
pops up, it just flashes and does not wait for the user to click "OK".  What would be missing to have this the msgbox wiat for a cmd?
0
 
jjardineCommented:
I am sorry again about a mistake in the second ElseIf statement.  That should read ElseIf intUnits > 4 AND intUnits < 11 Then      I had forgotten the intUnits on the < 11.  

I pasted that code into a project on my pc and the msgbox works just fine.  It waits for me to click ok.   Do you use the retval that the msgbox returns??  If not  you can just call

Else
    MsgBox conMsg, conBtns, "Entry Error"
End If

I don't think that would make a difference but I guess it is worth a try.    I used the code I posted above for my program and it worked just fine after I fixed the above mentioned IfElse Statement.
0
 
GlobalFaxAuthor Commented:
Here is the current code, as it looks the same as yours but yet I do not get a chance to click OK to the Msg, as it just flashes.   And I get a runtime error when I clcik print


Private Sub cmdCalc_Click()
Const conBtns As String = vbOKOnly + vbInformation _
        + vbDefaultButton1 + vbApplicationModal
    Const conMsg As String = _
            "The number of units must be greater than 0."
    Dim intRetVal As Integer, intUnits As Integer, sngTotal As Single
    intUnits = Val(txtUnits.Text)
    If intUnits > 0 And intUnits < 5 Then
        sngTotal = intUnits * 10
    ElseIf intUnits > 4 And intUnits < 11 Then
        sngTotal = intUnits * 9
    ElseIf intUnits >= 11 Then
        sngTotal = intUnits * 7
    Else
        intRetVal = MsgBox(conMsg, conBtns, "Entry Error")
End If
    lblTotalDue.Caption = Format(sngTotal, "currency")
        txtUnits.SelStart = 0
        txtUnits.SelLength = Len(txtUnits.Text)
        txtUnits.SetFocus
End Sub

Private Sub cmdPrint_Click()
    fraButtons.Visible = False
    PrintForm
    fraButtons.Visible = True
End Sub

Private Sub cmdExit_Click()
    End
End Sub

Private Sub Form_Load()
    frmJacob.Left = (Screen.Width - frmJacob.Width) / 2
    frmJacab.Top = (Screen.Height - frmJacob.Height) / 2
End Sub

Private Sub txtUnits_GotFocus()
    txtUnits.SelStart = 0
    txtUnits.SelLength = Len(txtUnits.Text)
End Sub
0
 
sridhar12345Commented:
globalfax
The Code works perfect in mine..It waits for the Ok button.Whic version of VB r u using?
0
 
GlobalFaxAuthor Commented:
Version 8176   VBA:Retail 6.0.8169   Forms3: 2.01
0
 
sridhar12345Commented:
globalfax
The Code works perfect in mine..It waits for the Ok button.Whic version of VB r u using?
0
 
sridhar12345Commented:
abt the runtime error ,the error message will help us.

and abt the message box there is nothin to change in the code
0
 
GlobalFaxAuthor Commented:
Run Time Error '424:
Object Required

debugging back to the below line item:

 fraButtons.Visible = False
0
 
GlobalFaxAuthor Commented:
JJARDINE,

Thanks for all your help.

The print debug error was my bust as my sister's laptop does not have a printer setup I found out this was causing the debug error.

Your code changes as recommended worked great!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 14
  • 7
  • 6
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now