I am trying to run a Stored MS-access Query from excel using VBA. I have created a generic function to preform this task (using ADODB). I can get results with or without parameters.
However when I try to run a stored Query that references an Access vba module, excel barfs a runtime error “undefined function MissCat” (which is the name of the custom function the stored query uses).
I would have expected this to work as access knows the definition of the function, but now I get the feeling the ADODB interface is trying to interperet the SQL in the stored query without the aid of the VBA module i’ve created in access.
I am jumping through these hoops as most of my users do not have access installed on their machines.
Is it possible for me to use the stored query as is (changing the query interface, perhaps)? or is the path of least resistance changing the query such that it doesn’t need the custom function (as you can see below it is quite simple, I’ve used it as a crutch because I don’t know SQL very well)
here is the function
Function MissCat(ProShip As Date, TargetDate As Date) As String
If TargetDate >= ProShip Then
MissCat = "Meets target"
Exit Function
End If
If TargetDate < Date Then
MissCat = "Unrecoverable"
Exit Function
End If
Select Case ProShip - TargetDate
Case 1 To 6
MissCat = "Less than one week"
Case 7 To 14
MissCat = "1-2 Weeks"
Case Else
MissCat = "Greater than 2 Weeks"
End Select
End Function
and here is the SQL
TRANSFORM Count(Shipset.ID) AS CountOfID
SELECT Calendar.[Week Of]
FROM Calendar INNER JOIN Shipset ON Calendar.DateSerial = Shipset.ShipDate
WHERE (((Calendar.[Week Of])>Date()-(13*7)) AND ((Shipset.ShipDate) Is Not Null) AND ((Shipset.ReqOut)=False) AND ((Shipset.TwoTier)=False))
GROUP BY Calendar.[Week Of]
ORDER BY IIf(MissCat([Shipset]![ShipDate],[Shipset]![TargetDate])="Meets Target","Meets Target","Hit") DESC
PIVOT IIf(MissCat([Shipset]![ShipDate],[Shipset]![TargetDate])="Meets Target","Meets Target","Hit");
thanks,
Your query will not be able to use the custom VBA function except when it is run from within an Access session.
You can substitute nested IIf() function statements for the MissCat function. Two challenges with that approach when the nesting is complex: it’s hard to follow the logic; it’s easy to screw up the syntax.
The general form is IIf(condition, truepart, falsepart)
So for the first condition, try this in a temporary procedure you create in Access.
As the next step, replace “else” with an IIf expression for the second condition.
And so forth. This is what I wound up with as the final grand mess.
After you have the procedure working correctly, copy the nested IIf expression and test it in a new Access query.
After looking again at how you used the MissCat function in your original query, I don’t think I’d try to substitute this approach directly. I would create a separate query to transform the base data and then adapt the original query to use the new query as one of its data sources.