Solved

Error handling in visual basic 6 (Pizza order program..)

Posted on 2003-11-09
11
1,353 Views
Last Modified: 2013-11-25
I have made a pizza order program. It's a school assignment on using arrays. I need to make sure the user doens't enter letters in the textbox that asks them how many pizzas they would like, so I used something from another site: It says to create a label, and then type On error goto 'label' . I've done that, and I'm having a problem. The program doens't ignore the code for error handling when there aren't any errors.. I only know how to make the label work in the given sub, and if there aren't any errors, the program still loads the error handler. Here's my code:

Option Explicit
' Variable holding total price of toppings. not used yet
Dim TOPPINGSPRICE As Integer
' Variables holding the phrase of the given topping
Dim PEPPERONI As String
Dim PEPPERS As String
Dim ONIONS As String
Dim MUSHROOMS As String
' Variables holding the prices of differnet sizes. Not yet used.
Dim LARGEPRICE As Integer
Dim MEDIUMPRICE As Integer
' Variable holding price of current order. Not yet used..
Dim CURRENTORDERPRICE As Integer
' variables holding phrases for size
Dim MEDIUM As String
Dim LARGE As String
' ignore these.
Dim PIZZANUM As Integer
Dim COUNTER As Integer
' checkbox for pepperoni
Private Sub ChkPepperoni_Click()
If ChkPepperoni.Value = Checked Then
    TOPPINGSPRICE = TOPPINGSPRICE + 2
    PEPPERONI = "Pepperoni "
    TOPPINGS(i) = TOPPINGS(i) & PEPPERONI
ElseIf ChkPepperoni.Value = Unchecked Then
    TOPPINGSPRICE = TOPPINGSPRICE - 2
    PEPPERONI = Empty
End If
End Sub
' checkbox for peppers
Private Sub chkpeppers_click()
If ChkPeppers.Value = True Then
    TOPPINGSPRICE = TOPPINGSPRICE + 2
    PEPPERS = " Peppers "
    TOPPINGS(i) = TOPPINGS(i) & PEPPERS
ElseIf ChkPeppers.Value = Unchecked Then
    TOPPINGSPRICE = TOPPINGSPRICE - 2
    PEPPERS = ""
End If
End Sub
' CHeckbox for mushrooms
Private Sub ChkMushrooms_click()
If ChkMushrooms.Value = Checked Then
    TOPPINGSPRICE = TOPPINGSPRICE + 2
    MUSHROOMS = " Mushrooms "
    TOPPINGS(i) = TOPPINGS(i) & MUSHROOMS
ElseIf ChkMushrooms.Value = Unchecked Then
    TOPPINGSPRICE = TOPPINGSPRICE - 2
    MUSHROOMS = ""
End If
End Sub
' Checkbox for onions
Private Sub chkonions_click()
If ChkOnions.Value = Checked Then
    TOPPINGSPRICE = TOPPINGSPRICE + 2
    ONIONS = " Onions "
    TOPPINGS(i) = TOPPINGS(i) & ONIONS
ElseIf ChkOnions.Value = Unchecked Then
    TOPPINGSPRICE = TOPPINGSPRICE - 2
    ONIONS = ""
End If
End Sub
' Command button for finishing all orders
Public Sub CmdDone_Click()
' Assigns the number of pizzas for the current order
NUMBER(i) = txtNum.Text
' Checks for an error, and tells it where to go. PROBLEM AREA
On Error GoTo Errorhandle
NUMBER(i) = NUMBER(i) * 1
Errorhandle:
MsgBox ("You are supposed to enter whole numbers in the text boxes, not alphabets and not decimal numbers.")
' Empties the cells of the current order
NUMBER(i) = Empty
TOPPINGS(i) = Empty
SIZES(i) = Empty
i = i - 1
' Empties options for current order.
OptMedium.Value = False
OptLarge.Value = False
ChkPepperoni.Value = Unchecked
ChkPeppers.Value = Unchecked
ChkMushrooms.Value = Unchecked
ChkOnions.Value = Unchecked
txtNum.Text = Empty
' Loads the form with the receipt and gives it info
i = 0
FrmPizzaOrder.Hide
Form1.Show
For i = 1 To 99
Form1.Picture1.Print NUMBER(i) & SIZES(i) & TOPPINGS(i)
Next i
End Sub
' Button for ordering another pizza
Private Sub CmdOrderAnother_Click()
' Assigsn the number of pizzas to the array
NUMBER(i) = txtNum.Text
' Checks for error of alphabet in numerical box, then calls error handler PROBLEM AREA
On Error GoTo Errorhandle
NUMBER(i) = NUMBER(i) * 1
Errorhandle:
MsgBox ("You are supposed to enter whole numbers in the text boxes, not alphabets and not decimal numbers.")
' Clears the cells
NUMBER(i) = Empty
TOPPINGS(i) = Empty
SIZES(i) = Empty
i = i - 1
' Clears options
OptMedium.Value = False
OptLarge.Value = False
ChkPepperoni.Value = Unchecked
ChkPeppers.Value = Unchecked
ChkMushrooms.Value = Unchecked
ChkOnions.Value = Unchecked
txtNum.Text = Empty
i = i + 1
OptMedium.Value = False
OptLarge.Value = False
ChkPepperoni.Value = Unchecked
ChkPeppers.Value = Unchecked
ChkOnions.Value = Unchecked
ChkMushrooms.Value = Unchecked
txtNum.Text = Empty
End Sub

Private Sub CmdQuit_Click()
End
End Sub
Private Sub Form_Load()
MEDIUMPRICE = 15
LARGEPRICE = 18
MEDIUM = " Medium "
LARGE = " Large "
i = 0
i = 1
End Sub


Private Sub OptMedium_Click()
If OptMedium.Value = True Then
    SIZES(i) = MEDIUM
ElseIf OptMedium.Value = False Then
    SIZES(i) = SIZES(i) - MEDIUM
End If
End Sub
Private Sub optlarge_click()
If OptLarge.Value = True Then
    SIZES(i) = LARGE
ElseIf OptLarge.Value = False Then
    SIZES(i) = SIZES(i) - LARGE
End If
End Sub

Module:
' array for toppings
Public TOPPINGS(1 To 99) As String
' var for loops
Public i As Integer
' Array for sizes
Public SIZES(1 To 99) As String
' array for numbers
Public NUMBER(1 To 99) As String
' Label for errorhandling
Public Errorhandle As Label


Thanks for any help. It would also be great if anyone can tell me of a way to make the program more efficient (keeping in mind that arrays are a must), but right now the main issue is the error handling.
0
Comment
Question by:Halofreak117
  • 6
  • 4
11 Comments
 

Author Comment

by:Halofreak117
ID: 9711512
Hm.. I forgot another thing.

I'm in grade 10, and I would love to have a career in computer programming. THe problem is, I can't decide what language to learn... I asked on another programming forum, and they said PHP, Java or C. I ruled out C because I can't find a free compiler that's easy to install. And I really don't know with which to go: Java or PHP? What do you think would help give me more understanding and make university programming easier for me? If you think I should learn some other programming language, I'm open to the idea. But I'm not open to the visual programming series because with text only series, I think I would be able to learn more and make university somewhat easier.

Thanks for any input on this as well.
0
 
LVL 100

Accepted Solution

by:
mlmcc earned 50 total points
ID: 9711553
You must skip over the error handler.  Generally I do this by putting the error handler last in the subroutine and exiting before the error handling code like this

Private sub MySub()
On Error GoTo ErrHandle

....


Exit Sub
ErrHandle:
' Handle the error

end sub

mlmcc
0
 

Author Comment

by:Halofreak117
ID: 9711569
Wow, thanks a lot. Quick and working answer = ) Points go to you.
0
 

Author Comment

by:Halofreak117
ID: 9711586
Hmm... can you give me an answer for this too please?
As you can see in my module, the program assumes a maximum of 99 orders. I finally added the pricing to it, and since it's an integer it prints 0s all the way down. I tried making making the arrays (1 to var) but variables don't work in arrays. is there a way to make it either stop printing the 0s or make a variable work in the arrray?
Thanks, and ofcourse I'm still taking any input on my second question =)
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9711595
You r code would be much easier to read, maintain, and debug if you did some indenting and and added white space.

Private Sub optlarge_click()

'
'   This tests - comments stand out
'
   If OptLarge.Value = True Then
       SIZES(i) = LARGE
   ElseIf OptLarge.Value = False Then
       SIZES(i) = SIZES(i) - LARGE
   End If

End Sub


>>The problem is, I can't decide what language to learn
  Don't concentrate so much on the language as on the concepts of programming.  Using good design, proper formatting, structured code, etc.  Learn the way looping  and selection works.  Learn when each construct is used and when the different methods are appropriate.  

A good grounding in the concepts of programming is much more important than learning a language.  If you can design a program then it is relatively easy to translae it to the language of choice.

VB is an excellent first language since it is easy to develop programs and get things working.

Ask lots of questions.  Get so you understand how the construct works and why it was used.  

I work with several good programmers.  One understands programming in general while the other is good with VBA.  When we switched to VB the guy who knew VBA had difficulty with the new concepts and his code still looks like VBA.  The other picked up VB quickly and writes very good code.

If you understand programming concepts, the language syntax is easy to pick up.
 
mlmcc
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 100

Expert Comment

by:mlmcc
ID: 9711605
Try something like this

for i = 1 to numorders

mlmcc
0
 

Author Comment

by:Halofreak117
ID: 9711642
Wow thanks alot. That works too =) I'll keep in mind what you said about the indentation, as I"m sure I'll be back here with more questions again
>> Don't concentrate so much on the language as on the concepts of programming.  Using good design, proper formatting, structured code, etc.  Learn the way looping  and selection works.  Learn when each construct is used and when the different methods are appropriate.  
I understand that. My teacher keeps telling us that as well, and the other day I took a quick look at Java, and made the connection pretty easily (to the point of the level we are at school currently). so i'll keep that in mind.
Thanks again for everything =)
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9711686
Glad I could help

mlmcc
0
 

Author Comment

by:Halofreak117
ID: 9711850
Hm...... I Just found a bug and I can't seem to find the culprit. Peppers simply doens't print in the toppings section of my second form. I haven't got a clue why, and it also doesn't include peppers to the price. Here's my renewed code with better spacing:

Option Explicit
'
' Variable holding total price of toppings. not used yet
'
Dim TOPPINGSPRICE As Integer
'
' Variables holding the phrase of the given topping
'
Dim PEPPERONI As String
Dim PEPPERS As String
Dim ONIONS As String
Dim MUSHROOMS As String
'
' Variables holding the prices of differnet sizes. Not yet used.
'
Dim LARGEPRICE As Integer
Dim MEDIUMPRICE As Integer
'
' Variable holding price of current order. Not yet used..
'
Dim CURRENTORDERPRICE As Integer
'
' variables holding phrases for size
'
Dim MEDIUM As String
Dim LARGE As String
'
' checkbox for pepperoni
'
Private Sub ChkPepperoni_Click()
'
' Adds 2 dollars to the price of the pizza, and makes the arrays add pepperoni to the receipt
'
    If ChkPepperoni.Value = Checked Then
        PIZZAPRICES(i) = PIZZAPRICES(i) + 2
        PEPPERONI = "Pepperoni "
        TOPPINGS(i) = TOPPINGS(i) & PEPPERONI
    '
    'Removes 2 dollars from the price and removes pepperoni from the receipt
    '
    ElseIf ChkPepperoni.Value = Unchecked Then
        PIZZAPRICES(i) = PIZZAPRICES(i) - 2
        PEPPERONI = Empty
    End If

End Sub
'
' checkbox for peppers
'
Private Sub chkpeppers_click()
'
' Adds 2 dollars to the price and adds Peppers to the array for the receipt
'
    If ChkPeppers.Value = True Then
        PIZZAPRICES(i) = PIZZAPRICES(i) + 2
        PEPPERS = "Peppers "
        TOPPINGS(i) = TOPPINGS(i) & PEPPERS
    ElseIf ChkPeppers.Value = Unchecked Then
'
'Removes the 2 dollars from the price and removes peppers from the receipt
        PIZZAPRICES(i) = PIZZAPRICES(i) - 2
        PEPPERS = Empty
       
    End If

End Sub
'
' Checkbox for mushrooms
'
Private Sub ChkMushrooms_click()
    '
    ' Adds 2 dollars to the price and mushrooms to the array for receipt.
    '
    If ChkMushrooms.Value = Checked Then
        PIZZAPRICES(i) = PIZZAPRICES(i) + 2
        MUSHROOMS = " Mushrooms "
        TOPPINGS(i) = TOPPINGS(i) & MUSHROOMS
    ElseIf ChkMushrooms.Value = Unchecked Then
    '
    ' Removes the 2 dollars added to the price and removes mushrooms from receiept
    '
        PIZZAPRICES(i) = PIZZAPRICES(i) - 2
        MUSHROOMS = Empty
    End If
   
End Sub
'
' Checkbox for onions
'
Private Sub chkonions_click()
   
    If ChkOnions.Value = Checked Then
        PIZZAPRICES(i) = PIZZAPRICES(i) + 2
        ONIONS = " Onions "
        TOPPINGS(i) = TOPPINGS(i) & ONIONS
    '
    ' Removes the 2 dollars added and removes Onions from the receipt
    '
    ElseIf ChkOnions.Value = Unchecked Then
        PIZZAPRICES(i) = PIZZAPRICES(i) - 2
        ONIONS = Empty
    End If

End Sub
'
' Command button for finishing all orders
'
Public Sub CmdDone_Click()
'
' Assigns the number of pizzas for the current order
'
    NUMBER(i) = txtNum.Text
'
' Checks for error of alphabet in numerical box, then calls error handler PROBLEM AREA
'
    On Error GoTo Errorhandle
    NUMBER(i) = NUMBER(i) * 1
'
' Calculates the price of each order.
'
    If OptMedium.Value = True Then
        PIZZAPRICES(i) = (PIZZAPRICES(i) + MEDIUMPRICE) * NUMBER(i)
    Else
        PIZZAPRICES(i) = (PIZZAPRICES(i) + LARGEPRICE) * NUMBER(i)
    End If
'
' Increments numorders
'
    NUMORDERS = NUMORDERS + 1
    i = 0
    ORDERNUMBER = ORDERNUMBER + 1
'
' Loads the form with the receipt and gives it info
'

    FrmPizzaOrder.Hide
    FrmReceipt.Show
   
'
' Formatting for the receipt
'
    FrmReceipt.PicTop.Print "", "*******************", ""
    FrmReceipt.PicTop.Print "", "* R E C E I P T *", ""
    FrmReceipt.PicTop.Print "", "*******************", ""
   
    FrmReceipt.PicNum.Print "Number"
    FrmReceipt.PicSize.Print "Size"
    FrmReceipt.Pictoppings.Print "Toppings"
    FrmReceipt.PicPrice.Print "Price"
   
    For i = 1 To NUMORDERS
       
        FrmReceipt.PicNum.Print ""
        FrmReceipt.PicNum.Print NUMBER(i)
        FrmReceipt.PicSize.Print ""
        FrmReceipt.PicSize.Print SIZES(i)
        FrmReceipt.Pictoppings.Print ""
        FrmReceipt.Pictoppings.Print TOPPINGS(i)
        FrmReceipt.PicPrice.Print ""
        FrmReceipt.PicPrice.Print PIZZAPRICES(i)
       
    Next i
   
Exit Sub
'
' Error handling
'
Errorhandle:
    MsgBox ("You are supposed to enter whole numbers in the text boxes, not alphabets and not decimal numbers.")
'
' Clears the cells with the error
'
    NUMBER(i) = Empty
    TOPPINGS(i) = Empty
    SIZES(i) = Empty
'
' Clears options to re-input the order
'
    OptMedium.Value = False
    OptLarge.Value = False
    ChkPepperoni.Value = Unchecked
    ChkPeppers.Value = Unchecked
    ChkMushrooms.Value = Unchecked
    ChkOnions.Value = Unchecked
    txtNum.Text = Empty

End Sub
'
' Button for ordering another pizza
'
Private Sub CmdOrderAnother_Click()
'
' Assigsn the number of pizzas to the array
'
    NUMBER(i) = txtNum.Text
'
' Checks for error of alphabet in numerical box, then calls error handler PROBLEM AREA
'
    On Error GoTo Errorhandle
    NUMBER(i) = NUMBER(i) * 1

    If OptMedium.Value = True Then
        PIZZAPRICES(i) = (PIZZAPRICES(i) + MEDIUMPRICE) * NUMBER(i)
    Else
        PIZZAPRICES(i) = (PIZZAPRICES(i) + LARGEPRICE) * NUMBER(i)
    End If
'
' Prepares the form for another order
'
    i = i + 1
    NUMORDERS = NUMORDERS + 1
    OptMedium.Value = False
    OptLarge.Value = False
    ChkPepperoni.Value = Unchecked
    ChkPeppers.Value = Unchecked
    ChkOnions.Value = Unchecked
    ChkMushrooms.Value = Unchecked
    txtNum.Text = Empty
Exit Sub
'
' Error handling
'
Errorhandle:
    MsgBox ("You are supposed to enter whole numbers in the text boxes, not alphabets and not decimal numbers.")
'
' Clears the cells with the error
'
    NUMBER(i) = Empty
    TOPPINGS(i) = Empty
    SIZES(i) = Empty
'
' Clears options to re-input the order
'
    OptMedium.Value = False
    OptLarge.Value = False
    ChkPepperoni.Value = Unchecked
    ChkPeppers.Value = Unchecked
    ChkMushrooms.Value = Unchecked
    ChkOnions.Value = Unchecked
    txtNum.Text = Empty

End Sub
'
' Ends the program
'
Private Sub CmdQuit_Click()

    Unload FrmPizzaOrder, FrmReceipt
    End

End Sub
Private Sub Form_Load()
    ' Sets the Prices and the string values for the size on the receipt
    MEDIUMPRICE = 15
    LARGEPRICE = 18
    MEDIUM = " Medium "
    LARGE = " Large "
    i = 1

End Sub

Private Sub OptMedium_Click()
'
' Changes the value of current array cell to medium
'
    If OptMedium.Value = True Then
        SIZES(i) = MEDIUM
    ElseIf OptMedium.Value = False Then
        SIZES(i) = SIZES(i) - MEDIUM
    End If

End Sub
Private Sub optlarge_click()

    If OptLarge.Value = True Then
        SIZES(i) = LARGE
    ElseIf OptLarge.Value = False Then
        SIZES(i) = SIZES(i) - LARGE
    End If

End Sub

I don't see any reason why it should do this. It's the same as the pepperoni, mushroom and onion code...
0
 

Author Comment

by:Halofreak117
ID: 9711917
Nevermind. I figured it out.
I put If chkpeppers.value = true instead of checked.
0
 

Expert Comment

by:Tricky
ID: 9715485
Hey Dude,

Try using the LIKE (use the F1 help with VB for a complete definition) operator in conjunction with NOT; eg

If txtNum.Text Like Not"[!0-9]" Then
  (True case)
Else
  (False case)
End If

Using square brackets inside quote marks denotes an array - in this case numeric.

You could of course streamline all of your If clauses by using IIf; eg

IIf txtNum.Text Like Not"[!0-9]", (True case), (False case)

HTH :)

tricky
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now