Link to home
Start Free TrialLog in
Avatar of GlobalFax
GlobalFax

asked on

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

__________________________________________________________
Avatar of bobbit31
bobbit31
Flag of United States of America image

   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
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
yeah sorry, i'm kind of drunk, the 'else if' should be 'elseif' ;)

btw, jjardine, shouldn't it be intUnits > 0 andintUnits < 5
why change the Select Case form.  It is actually more efficient, and easier to read.
Avatar of GlobalFax
GlobalFax

ASKER

jjardine, is the below line item needed as bobbit31 added?

intUnits = Val(txtUnits.Text)
If I replaced as above I get an "unexpected end sub"
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   ;-)
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
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

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


btw, yes the homeowkr is specific in the task as recoding with the if, then else.
btw, yes the homeowkr is specific in the task as recoding with the if, then else.
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
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.


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
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
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
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.
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
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.
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
Thanks jjardine, I will test the recommended changes this afternoon as I get home.  Will post update
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.
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?
ASKER CERTIFIED SOLUTION
Avatar of jjardine
jjardine
Flag of United States of America image

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
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
globalfax
The Code works perfect in mine..It waits for the Ok button.Whic version of VB r u using?
Version 8176   VBA:Retail 6.0.8169   Forms3: 2.01
globalfax
The Code works perfect in mine..It waits for the Ok button.Whic version of VB r u using?
abt the runtime error ,the error message will help us.

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

debugging back to the below line item:

 fraButtons.Visible = False
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!