I have the follow VBA code and it returns a ‘data type mismatch in criteria expression‘ while executing. I cannot seem to find out why it is giving me this error.
Can anybody help me?
VBA:
Public Function GezaagdeOmzet(ByVal TotaalPrijs As Double, ByVal AantalArtiklesPerOrder As Double, ByVal TotaalArtiklesPerOrder As Double, ByVal AantalArtiklesVerwijderedUitZaaglijst As Double) As Double
Dim result As Double
On Error GoTo ErrHandler
If IsNumeric(TotaalPrijs) = False Then
MsgBox ("TotaalPrijs not a number")
MsgBox (TotaalPrijs)
End If
If IsNumeric(AantalArtiklesPerOrder) = False Then
MsgBox ("AantalArtiklesPerOrder not a number")
MsgBox (AantalArtiklesPerOrder)
End If
If IsNumeric(TotaalArtiklesPerOrder) = False Then
MsgBox ("TotaalArtiklesPerOrder not a number")
MsgBox (TotaalArtiklesPerOrder)
End If
If IsNumeric(AantalArtiklesVerwijderedUitZaaglijst) = False Then
MsgBox ("AantalArtiklesVerwijderedUitZaaglijst not a number")
MsgBox (AantalArtiklesVerwijderedUitZaaglijst)
End If
If Not TotaalPrijs = 0 Then
If AantalArtiklesPerOrder > 0 Then
result = TotaalPrijs / (AantalArtiklesPerOrder * TotaalArtiklesPerOrder) * AantalArtiklesVerwijderedUitZaaglijst
On Error GoTo ErrHandler
Else
MsgBox ("AantalArtiklesPerOrder is null, Cannot do calculation")
End If
Else
MsgBox ("TotaalPrijs is null, cannot do division")
End If
Exit Function
ErrHandler:
MsgBox ("TotaalPrijs: " & TotaalPrijs & " TotaalArtiklesPerOrder: " & TotaalArtiklesPerOrder & " AantalArtiklesPerOrder: " & AantalArtiklesPerOrder & " AantalArtiklesVerwijderedUitZaaglijst: " & AantalArtiklesVerwijderedUitZaaglijst)
End Function
SQL Query in MS Access
GezaagdeOmzet: Sum(GezaagdeOmzet([TotaalPrijs],[tbl_ArtikelsPerOrder]![Aantal],[Totaal],[tbl_ArtikelVerwijderdUitZaaglijst]![Aantal]))
Is there anyway to catch the error I’m getting with VBA?
Cstr or CDec or CDbl is not handling this error.
The above function is a little strange, as haarrrgh says.
It should look more like the code below. You do not need to check that each of the arguments (TotaalPrijs, ByVal AantalArtiklesPerOrder etc) is a number, because you pass them As Double. If you pass anything except a number, such as a letter or Null, you will get an error. If this is not what you want, consider passing the arguments As Variant, you can then check that they are numbers. However, as you are using this in a query, I suggest that you do not use message boxes, if the argument is null, make it zero, if that is what it is supposed to be.
Note also GezaagdeOmzet = , rather than result =
EDIT re Comments