Results 1 to 6 of 6

Thread: Variable Error??? Why???

  1. #1
    Guest

    Question

    What am I missing??? When I run the code without the If Then statements to check for valid field entries... The equation is fine.

    When I drop in the If Then statements, the code will return the message box. Once the Okay button is selected instead of setting focus on the offending text box (If an entry wasn't made and the box is empty) I get a 'Runtime Error 13'. With the curPrice = txtPrice.Text highlighted once I try to Debug it.

    I used the MSDN Library to look it up and it tells me something about variables not being equal.

    Here is the code... Could someone shed light where I have run off the trail???

    Code:
    Private Sub cmdCompute_Click()
        'Use calculation to determine
        'total sales.
        '
        'curPrice is the price of product.
        'intSold is number of units sold.
        'curTotal is the gross sales amount.
        Dim curPrice As Currency, curTotal As Currency
        Dim intSold As Integer, intPress As Integer
        
        'check for valid entry in Price field
    If Val(txtPrice.Text) <= 0 Then
        IntPress = MsgBox("Invalid Price. Please enter Price.", vbExclamation, "Price Request")
        txtPrice.SetFocus
    End If
    
        'Check for valid entry in Sold field
    If Val(txtSold.Text) <= 0 Then
        IntPress = MsgBox("Please enter number of units sold.", vbExclamation, "Units Sold")
        txtSold.SetFocus
    End If
    
    
        curPrice = txtPrice.Text
        intSold = txtSold.Text
        txtTotal.Text = Format(curPrice * intSold, "Currency") 'Calculate gross sales.
        
    End Sub
    Thanks...

  2. #2
    Member
    Join Date
    Jul 2000
    Location
    Ontario, Canada
    Posts
    61
    Try this

    Code:
    Private Sub cmdCompute_Click()
        'Use calculation to determine
        'total sales.
        '
        'curPrice is the price of product.
        'intSold is number of units sold.
        'curTotal is the gross sales amount.
        Dim curPrice As Currency, curTotal As Currency
        Dim intSold As Integer, intPress As Integer
        
        'check for valid entry in Price field
    If txtPrice.Text = "" Then
        IntPress = MsgBox("Invalid Price. Please enter Price.", vbExclamation, "Price Request")
        txtPrice.SetFocus
    End If
    
        'Check for valid entry in Sold field
    If txtSold.Text = "" Then
        IntPress = MsgBox("Please enter number of units sold.", vbExclamation, "Units Sold")
        txtSold.SetFocus
    End If
    
    
        curPrice = txtPrice.Text
        intSold = txtSold.Text
        txtTotal.Text = Format(curPrice * intSold, "Currency") 'Calculate gross sales.
        
    End Sub
    [Edited by absalom on 08-13-2000 at 09:46 AM]
    ---~^ Absalom ^~---

    There is nobody in the world who knows everything there is no one his/her workforce who knows everything what really makes the person smart is that he/she is not affraid to ask for help.

  3. #3
    Guest
    Absalom ~

    I still receive a 'Runtime Error 13'. For some reason the Variables curPrice = txtPrice.Text and intSold = txtSold.Text are not compatible.

    When I click on the Debug it will highlight curPrice = txtPrice.Text. Or the other depending upon which textbox I leave open to test.

    Both codes shown will give the same end-result. But I am going to want to also check them with the IsNumeric eventually. Right now I am self-studying VB.

    Thanks for your help though! :-)

    Back to the books I guess...

  4. #4
    Member
    Join Date
    May 2000
    Location
    Canada
    Posts
    52

    Smile

    txtPrice.text is a string .
    You will need to use the value of this string.
    curPrice = val(txtPrice.Text)


  5. #5
    Member
    Join Date
    Jul 2000
    Location
    Ontario, Canada
    Posts
    61
    Or

    CurPrice = Text1
    ---~^ Absalom ^~---

    There is nobody in the world who knows everything there is no one his/her workforce who knows everything what really makes the person smart is that he/she is not affraid to ask for help.

  6. #6
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    Take care of your value conversions early and use the variables in your value tests (if statements).

    Also, you don't need the intPress variabble since you never do anything with it. Exit the sub if one value is incorrect to avoid doing calulations with invalid data. Merely setting the focus to another control will not stop the code from executing.

    Try this...

    Code:
    Private Sub cmdCompute_Click()
        'Use calculation to determine
        'total sales.
        '
        'curPrice is the price of product.
        'intSold is number of units sold.
        'curTotal is the gross sales amount.
        Dim curPrice As Currency
        Dim curTotal As Currency
        Dim intSold As Integer
        
        'Convert Text to proper value types
        curPrice = CCur(Val(txtPrice.Text))
        intSold = CInt(Val(txtSold.Text))
        
        'check for valid entry in Price field
        If curPrice <= 0 Then
            MsgBox "Invalid Price. Please enter Price.", vbExclamation, "Price Request"
            txtPrice.SetFocus
            Exit Sub
        End If
    
        'Check for valid entry in Sold field
        If intSold <= 0 Then
            MsgBox "Please enter number of units sold.", vbExclamation, "Units Sold"
            txtSold.SetFocus
            Exit Sub
        End If
    
        txtTotal.Text = Format(curPrice * intSold, "Currency") 'Calculate gross sales.
        
    End Sub
    JC

Posting Permissions

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



Click Here to Expand Forum to Full Width