Solved

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

Posted on 2003-02-24
Medium Priority
223 Views
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
Question by:GlobalFax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 14
• 7
• 6
• +3

LVL 18

Expert Comment

ID: 8013892
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

LVL 14

Expert Comment

ID: 8013910
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

LVL 18

Expert Comment

ID: 8013938
yeah sorry, i'm kind of drunk, the 'else if' should be 'elseif' ;)

btw, jjardine, shouldn't it be intUnits > 0 andintUnits < 5
0

LVL 44

Expert Comment

ID: 8013961
why change the Select Case form.  It is actually more efficient, and easier to read.
0

LVL 1

Author Comment

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

intUnits = Val(txtUnits.Text)
0

LVL 1

Author Comment

ID: 8014014
If I replaced as above I get an "unexpected end sub"
0

LVL 1

Author Comment

ID: 8014044
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

LVL 14

Expert Comment

ID: 8014090
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

LVL 14

Expert Comment

ID: 8014093
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

LVL 1

Author Comment

ID: 8014104

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

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

LVL 1

Author Comment

ID: 8014117
btw, yes the homeowkr is specific in the task as recoding with the if, then else.
0

LVL 1

Author Comment

ID: 8014183
btw, yes the homeowkr is specific in the task as recoding with the if, then else.
0

Expert Comment

ID: 8014233
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

LVL 1

Author Comment

ID: 8014551
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

Expert Comment

ID: 8014830
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

Expert Comment

ID: 8015054
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

Expert Comment

ID: 8015126
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

LVL 8

Expert Comment

ID: 8015384
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.

0

LVL 1

Author Comment

ID: 8016145
sridhart2345,

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

_____

Compile Error:

Unexpected End Sub
0

LVL 8

Expert Comment

ID: 8016283
You havnt terminated the end if, so its telling you it encountered an 'end sub' line unexpectedly.

Add the end if into the code.

0

LVL 14

Expert Comment

ID: 8018399
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

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

LVL 1

Author Comment

ID: 8018672
Thanks jjardine, I will test the recommended changes this afternoon as I get home.  Will post update
0

LVL 14

Expert Comment

ID: 8020461
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

LVL 1

Author Comment

ID: 8021413
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

LVL 14

Accepted Solution

jjardine earned 2000 total points
ID: 8021654
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

LVL 1

Author Comment

ID: 8022273
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

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

Expert Comment

ID: 8022774
globalfax
The Code works perfect in mine..It waits for the Ok button.Whic version of VB r u using?
0

LVL 1

Author Comment

ID: 8022806
Version 8176   VBA:Retail 6.0.8169   Forms3: 2.01
0

Expert Comment

ID: 8022893
globalfax
The Code works perfect in mine..It waits for the Ok button.Whic version of VB r u using?
0

Expert Comment

ID: 8022908
abt the runtime error ,the error message will help us.

and abt the message box there is nothin to change in the code
0

LVL 1

Author Comment

ID: 8024825
Run Time Error '424:
Object Required

debugging back to the below line item:

fraButtons.Visible = False
0

LVL 1

Author Comment

ID: 8045785
JJARDINE,

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library aâ€¦
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). Uâ€¦
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
Suggested Courses
Course of the Month10 days, 16 hours left to enroll