VB Newibe, already searched.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: VB Newibe, already searched.

  1. #1
    Join Date
    Oct 2005
    Posts
    7

    VB Newibe, already searched.

    I'm running into a couple of problems with this program.

    1. When the program runs, after a few "orders" are entered and you try and "update summary" the values are not what they should be. Also for whatever reason the program goes through a validation for the "Description" before you can hit any of the lower buttons on my form.

    2. My validation for "sales tax" only for califonia residents seems to be firing off whenever it wants to.

    3. The above leads me to believe my function is not logical.

    If anymore information is required please ask! I would be happy to give a copy of the files for anyone who would like to take a closer look for me!


    Here is the code:

    Code:
    'Description: This program is designed to take order information
    'from a customer, such as shipping information, items , quantity.
    'The program will then compute a total price for the customer, inluding
    'item price, shipping charges, applicable sales tax when asked. The program
    'will also print and end when requested.
    
    Option Explicit
        Dim curPriceSum As Currency
        Dim intWeightSum As Integer
        Dim curTaxSum As Currency
        Dim curShippingSum As Currency
        Dim curTotalSum As Currency
        Dim curShipping As Currency
        Dim curAmountSum As Currency
        Dim intQuantitySum As Integer
        Dim intTax As Integer
        
        
    Private Sub cmdExit_Click()
    'End the program
    
        End
        
    End Sub
    
    Private Sub cmdNext_Click()
    'Validates the quantity, weight, and price of
    'an item and adds the charges and weights to the
    'appropriate totals. Does not however calculate
    'shipping and handling totals.
        
        Dim curTotalAmount As Currency
        Dim intQuantity As Integer
        Dim intWeight As Integer
        Dim curPrice As Currency
         
        If txtDescription.Text <> "" Then
            If txtQuantity.Text <> "" Then
                If txtWeight.Text <> "" Then
                     If txtPrice.Text <> "" Then
                     
                     Else
                         MsgBox "Please enter the price", vbOKOnly, "Error"
                     End If
                Else
                    MsgBox "Please enter the weight", vbOKOnly, "Error"
                End If
            Else
                MsgBox "Please enter the quantity", vbOKOnly, "Error"
            End If
        Else
            MsgBox "Please enter the description", vbOKOnly, "Error"
            txtDescription.SetFocus
            
        End If
         
         
        curPrice = txtPrice.Text
        intWeight = txtWeight.Text
        intQuantity = txtQuantity.Text
        intWeight = txtWeight.Text
        
        curPriceSum = txtPrice.Text + curPriceSum
        intWeightSum = intWeight + intWeightSum
        intQuantitySum = intQuantity + intQuantitySum
        curAmountSum = curShipping + curPrice + curAmountSum
        
        txtNonAmount.Text = intQuantity * curPrice
        
        txtDescription.Text = ""
        txtQuantity.Text = ""
        txtWeight.Text = ""
        txtPrice.Text = ""
        txtDescription.SetFocus
        
    End Sub
    
    Private Sub cmdPrint_Click()
        'Print the form
        
        PrintForm
    End Sub
    
    Private Sub cmdUpdate_Click()
    'Caluculates sales tax, shipping and handling
    'and the total amount due.
        
        
        curShippingSum = curShippingFun(curPriceSum, intWeightSum, intTax, _
        intQuantitySum)
        
        txtNonTotal.Text = curPriceSum + curShippingSum
        txtNonShip.Text = curShippingSum
        txtSaleNonTax.Text = curPriceSum
        
    End Sub
    
    Private Sub Form_Load()
    'Centers the form on the screen
    
        Top = (Screen.Height - Height) / 2
        Left = (Screen.Width - Width) / 2
        
    End Sub
    
    Private Sub mnuFileSummary_Click()
        cmdUpdate = True
        
    End Sub
    
    Private Sub txtCity_Validate(Cancel As Boolean)
        
        If Len(txtCity) = 0 Then
            MsgBox "Please enter you city", vbOKOnly, "Required Field"
            With txtCity
                .SelStart = 0
            End With
            Cancel = True
        End If
        
    End Sub
    
    Private Sub txtDescription_validate(Cancel As Boolean)
        
        If Len(txtDescription) = 0 Then
            MsgBox "Please enter the items description", vbOKOnly, "Required Field"
            With txtDescription
                .SelStart = 0
            End With
            Cancel = True
        End If
        
    End Sub
    
    Private Sub txtName_Validate(Cancel As Boolean)
        
        If Len(txtName) = 0 Then
            MsgBox "Please enter your name", vbOKOnly, "Required Field"
            With txtName
                .SelStart = 0
            End With
            Cancel = True
        End If
        
    End Sub
    
    Private Sub txtPrice_Validate(Cancel As Boolean)
        
        If Len(txtPrice) = 0 Or IsNumeric(txtPrice) = False Then
            MsgBox "Please enter the numeric price", vbOKOnly, "Required Field"
            With txtPrice
                .SelStart = 0
            End With
            Cancel = True
        End If
        
    End Sub
    
    Private Sub txtQuantity_Validate(Cancel As Boolean)
        
        If Len(txtQuantity) = 0 Or IsNumeric(txtQuantity) = False Then
            MsgBox "Please enter your numeric quantity", vbOKOnly, "Required Field"
            With txtQuantity
                .SelStart = 0
            End With
            Cancel = True
        End If
            
    End Sub
    
    Private Sub txtState_validate(Cancel As Boolean)
        
        If Len(txtState) = 0 Or Len(txtState) < 2 Then
            MsgBox "Please enter your two letter state", vbOKOnly, "Required Field"
                With txtState
                  .SelStart = 0
                End With
             Cancel = True
        End If
        
    End Sub
    
    Private Sub txtStreet_Validate(Cancel As Boolean)
    
        If Len(txtStreet) = 0 Then
            MsgBox "Please enter your street", vbOKOnly, "Required Field"
            With txtStreet
                .SelStart = 0
            End With
            Cancel = True
        End If
        
    End Sub
    
    Private Sub txtWeight_validate(Cancel As Boolean)
        
        If Len(txtWeight) = 0 Or IsNumeric(txtWeight) = False Then
            MsgBox "Please enter a numeric weight", vbOKOnly, "Required Field"
            With txtWeight
                .SelStart = 0
            End With
            Cancel = True
        End If
            
    End Sub
    
    Private Sub txtZip_validate(Cancel As Boolean)
        
        If Len(txtZip) = 0 Or Len(txtZip) < 5 Or IsNumeric(txtZip) = False Then
            MsgBox "Please enter your zip code", vbOKOnly, "Required Field"
            With txtZip
                .SelStart = 0
            End With
            Cancel = True
        End If
        
    End Sub
    
    Private Sub mnuEditColor_Click()
        
        With dlgCommon
            .Flags = cdlCCRGBInit
            .Color = frmVbmo.BackColor
            .ShowColor
        End With
        
        frmVbmo.BackColor = dlgCommon.Color
    End Sub
    
    Private Sub mnuEditFont_Click()
        
        With dlgCommon
            .Flags = cdlCFScreenFonts
            .ShowFont
        End With
        
    End Sub
    
    Private Sub mnuFileExit_Click()
        Call cmdExit_Click
        
    End Sub
    
    Private Sub mnuFilePrint_Click()
        dlgCommon.ShowPrinter
        
    End Sub
    
    Private Sub mnuHelpAbout_Click()
       MsgBox "Programed by xxxxxxx", vbInformation = vbOKOnly, "About my Application"
        
    End Sub
    
    
    Private Function curShippingFun(ByVal curPriceSum As Currency, intWeightSum As Integer, _
    intTax As Integer, intQuantitySum As Integer) As Currency
        
        intTax = 0.08
        
        If txtState.Text = "CA" Then
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = curPriceSum * intTax
        
        ElseIf txtState.Text = "Ca" Then
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = curPriceSum * intTax
        
        ElseIf txtState.Text = "cA" Then
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = curPriceSum * intTax
        
        ElseIf txtState.Text = "ca" Then
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = curPriceSum * intTax
            
        Else
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = 0
        
        End If
        
    End Function

  2. #2
    Join Date
    Apr 2005
    Posts
    102

    Re: VB Newibe, already searched.

    One problem is "intTax = 0.08" is set to an integer. It equals 0 as an integer. Try setting it to single. Also this "intWeightSum * 0.25" will not have the correct value being set as an integer.

    All this

    Code:
    If txtState.Text = "CA" Then
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = curPriceSum * intTax
        
        ElseIf txtState.Text = "Ca" Then
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = curPriceSum * intTax
        
        ElseIf txtState.Text = "cA" Then
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = curPriceSum * intTax
        
        ElseIf txtState.Text = "ca" Then
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = curPriceSum * intTax
            
        Else
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = 0
        
        End If
    could just be this


    Code:
    If ucase(txtState.Text) = "CA" Then
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = curPriceSum * intTax
    Else
            curShipping = (intWeightSum * 0.25) * intQuantitySum
                txtSaleNonTax = 0
        
    End If
    Last edited by superuser; October 30th, 2005 at 07:41 PM.

  3. #3
    Join Date
    Oct 2005
    Posts
    7

    Re: VB Newibe, already searched.

    Thanks for the timely reply superuser. I knew there had to be something to take out that area of code and make it smaller, I just didn't know what. Anyone else have any pointers for me?

  4. #4
    Join Date
    Oct 2005
    Posts
    7

    Re: VB Newibe, already searched.

    Quote Originally Posted by superuser
    Couple of more things. I edited the above post.

    Yes, I see. Integers do not carry over the decimal place. However the Single variable type does. A stupid mistake I won't make twice. Thanks again!



    *Edit


    After making the changes and making the variables Single, I am still not getting the right data to come out.
    Last edited by Blacksunshine; October 30th, 2005 at 08:18 PM.

  5. #5
    Join Date
    Apr 2005
    Posts
    102

    Re: VB Newibe, already searched.

    If these validations only need to be checked on the cmdnext click event and not on the update click event then you could call them only on the cmdnext event. One of many ways would be to use a boolean flag to show that the cmdnext click event has been clicked before processing the validation.

    Code:
    
    Private Sub txtDescriptionValidate(Cancel As Boolean)
    if cmdnextTrue then
        If Len(txtDescription) = 0 Then
            MsgBox "Please enter the items description", vbOKOnly, "Required Field"
            With txtDescription
                .SelStart = 0
            End With
            Cancel = True
        End If
    end if
    End Sub
    HTH

  6. #6
    Join Date
    Oct 2005
    Posts
    7

    Re: VB Newibe, already searched.

    How would I go about calling them? I had some trouble trying to put a call in the cmdNext area.

    I tried something like this

    Code:
        txtDescription_validate

  7. #7
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: VB Newibe, already searched.

    [QUOTE=Blacksunshine]
    Code:
        If txtDescription.Text <> "" Then
            If txtQuantity.Text <> "" Then
                If txtWeight.Text <> "" Then
                     If txtPrice.Text <> "" Then
                     
                     Else
                         MsgBox "Please enter the price", vbOKOnly, "Error"
                     End If
                Else
                    MsgBox "Please enter the weight", vbOKOnly, "Error"
                End If
            Else
                MsgBox "Please enter the quantity", vbOKOnly, "Error"
            End If
        Else
            MsgBox "Please enter the description", vbOKOnly, "Error"
            txtDescription.SetFocus
            
        End If
    [/QUOTE=Blacksunshine]
    Fisrtly, to simplify your code:
    Code:
        If (txtDescription.Text = "") Then MsgBox "Please enter the description", vbOKOnly, "Error"
        If (txtQuantity.Text = "") Then MsgBox "Please enter the quantity", vbOKOnly, "Error"
        If (txtWeight.Text = "") Then MsgBox "Please enter the weight", vbOKOnly, "Error"
        If (txtPrice.Text = "") Then MsgBox "Please enter the price", vbOKOnly, "Error"
    This will give the same effect as you previous code, but is easier to read (and slightly shorter too)...


    Secondly... in your code consider what will happen when none of the textboxes are empty. All of the conditions are false. So no messagebox appears, then program continues into the "calculation" section
    i.e. It goes to this bit:
    Code:
        curPriceSum = txtPrice.Text + curPriceSum
        intWeightSum = intWeight + intWeightSum
        intQuantitySum = intQuantity + intQuantitySum
        curAmountSum = curShipping + curPrice + curAmountSum
    Okay. Fine. No problem there.

    Now, what about when a text box IS blank. Lets say "txtWeight". The first statement If txtDescription.Text <> "" is true... the second If txtQuantity.Text <> "" is true... the third If txtWeight.Text <> "" is false... so the message box is shown telling the user to enter a wheight. BUT, what then? The user closes the message box and then? The function continues. It moves to the end of the IF statement block and carries on running. It STILL does the calculation section. It STILL places the result into "txtNonAmount" and it STILL clears all of the text boxes and STILL sets the focus to the description box.

    What you need to do is put a "Exit Function" into places where you what the code to exit. i.e. you validation code should look something like this
    Code:
        If (txtDescription.Text = "") Then MsgBox "Please enter the description", vbOKOnly, "Error" : txtDescription.SetFocus: Exit Function
        If (txtQuantity.Text = "") Then MsgBox "Please enter the quantity", vbOKOnly, "Error": Exit Function
        If (txtWeight.Text = "") Then MsgBox "Please enter the weight", vbOKOnly, "Error": Exit Function
        If (txtPrice.Text = "") Then MsgBox "Please enter the price", vbOKOnly, "Error": Exit Function
    BTW: The colon ( simply lets VB place two commands on one line.
    Mike

  8. #8
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: VB Newibe, already searched.

    Quote Originally Posted by Blacksunshine
    Yes, I see. Integers do not carry over the decimal place. However the Single variable type does. A stupid mistake I won't make twice.
    Yes you can use Double, Single or Currency for storing real numbers.
    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center