Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type Mismatch Using Function 1

Status
Not open for further replies.

MeldrethMan

Technical User
Feb 3, 2012
69
GB

I'm calling this function from a form but am getting a type mismatch error. It works fine in the Immediate window eg ?Points(8,11,5,5) returns 2.

Code:
Public Function Points(ShotAllowance As Byte, SI As Byte, Score As Byte, Par As Byte)

Dim Diff As Byte
Dim Netscore As Byte

    If ShotAllowance > 18 Then
        Netscore = IIf(SI > (ShotAllowance - 18), Score - 1, Score - 2)
    Else
        Netscore = IIf(SI > ShotAllowance, Score - 0, Score - 1)
    End If

    Diff = Netscore - Par

    If Diff = -3 Then Points = 5
    If Diff = -2 Then Points = 4
    If Diff = -1 Then Points = 3
    If Diff = 0 Then Points = 2
    If Diff = 1 Then Points = 1
    If Diff > 1 Then Points = 0
    
End Function

The subform contains the command button that calls the function, and two of its parameters. The other two parameters are in the frmComps. This is one of the calls, where Points1 is type Number/Byte

Code:
Points1 = Points(ShotAllowance, [Forms]![frmComps].SI1, Score1, [Forms]![frmComps].[Par1])

I also get the error if using a test set of numbers as parameters instead of these references.

Any thoughts?
 
Trye
Points1 = Points(cbyte(ShotAllowance), cbyte([Forms]![frmComps].SI1), cbyte(Score1), cbyte([Forms]![frmComps].[Par1]))

The forms reference will return a variant datatype that will not automatically cast to a byte.
 
Thanks for taking the time to reply MaJP.

I think part of the the problem may have been declaring Diff as Byte because it can take negative values. Before implementing your suggestion I'll continue to test the results because at the moment they're sensible.

Useful tip anyway, thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top