Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Option Buttons VB6

Options
  • 13-10-2010 4:41pm
    #1
    Registered Users Posts: 74 ✭✭


    Hi all, can anyone please help me with this one? Its on VB6.

    I've got a simple enough payroll form with three option buttons. It works if one is selected but I'm trying to give an message box to the user if they don't select any of them. I've written the code below but when I run it it gives me 'Run Time Error 13, Type Mismatch'.

    Private Sub cmdConfirm_Click()

    If optRateA.Value = True Then
    GrossResult.Caption = "€" & Hours.Text * 10
    ElseIf OptRateB.Value = True Then
    GrossResult.Caption = "€" & Hours.Text * 12.5
    ElseIf OptRateC.Value = True Then
    GrossResult.Caption = "€" & Hours.Text * 15
    End If

    If OptTax15 = True Then
    NetResult = GrossResult * 85 / 100
    ElseIf OptTax20 = True Then
    NetResult = GrossResult * 80 / 100
    ElseIf OptTax40 = True Then
    NetResult = GrossResult * 60 / 100
    End If

    PAYE = "€" & GrossResult - NetResult
    NetResult = "€" & NetResult

    End Sub


Comments

  • Registered Users Posts: 2,781 ✭✭✭amen


    well first thing if you are multiplying a text value with an int which is giving you the type mismatch

    secondly you are repeating the code to calculate the GrossResult three times. Consider moving to a function.

    Same for the Netresult.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    ^^ What amen said.

    The value in Hours.Text, which I'm going to assume is a textbox (see nameing conventions below) isn't numeric or isn't present then you will get a type mismatch error. You should validate the text property by testing it using the IsNumeric function.

    If it were me I wouldn't be storing values in form controls, I'd store them in procedure or module level variables and only display the values in controls once their ready to be displayed. The "€" symbol in the GrossResult label (again see below) could also be causing a type mismatch error when you multiplying.

    In VB best practice for naming conventions is to use hungarian notation. So a textbox name starts with txt, which would give txtHours, if GrossResult is a label then it's named lblGrossResult. Makes it easier for you and other devs to understand what each type of control is in memory. Similar thing for variable names, I'd declare PAYE as dim sPAYE as String. (Do you have option explicit turned on? If not you need too.)


  • Closed Accounts Posts: 18,056 ✭✭✭✭BostonB


    I don't think you know the difference between types, int, string etc. Definately use hungarian notation. it would be more obvious to you.

    It would be better as a case statement.

    You've nothing in there to handle nothing being selected.


  • Registered Users Posts: 1,456 ✭✭✭FSL


    You have three option buttons. All or none of them could be clicked.

    Assuming you have checked the validity of the data in the text boxes.

    You need to use the val function when performing arithmetic operations where an operand is a text box.

    e.g. answer = Val(textbox1) * 27


Advertisement