Link to home
Start Free TrialLog in
Avatar of Halofreak117
Halofreak117

asked on

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

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.
Avatar of Halofreak117
Halofreak117

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Mike McCracken
Mike McCracken

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
Wow, thanks a lot. Quick and working answer = ) Points go to you.
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 =)
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
Try something like this

for i = 1 to numorders

mlmcc
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 =)
Glad I could help

mlmcc
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...
Nevermind. I figured it out.
I put If chkpeppers.value = true instead of checked.
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