Hey all, long time no see.
I've run into an issue where it is necessary to run a VBA Function inside a SQL Query and my problem is simple: The performance is HORRIBLE! The Query only returns 33 Records and takes almost 3 seconds to run!!
Is there any way I can improve performance on the Function/Query?
Query:
Code:
SELECT
[SFGMaster].[SFGMGroupNo] AS [Group No],
[SFGMaster].[SFGMPartNo] AS [Part No],
IIf(
Int([PartMaster].[PartGlassThick]*25.4)=3,
3.2,
IIf(
[PartMaster].[PartGlassThick]>0.315,
Int([PartMaster].[PartGlassThick]*25.4)+1,
Int([PartMaster].[PartGlassThick]*25.4)
)
) &
"MM " &
GetShortDesc([PartMaster].[PartNumber]) AS [Description]
FROM
SFGMaster
INNER JOIN
PartMaster
ON
[SFGMaster].[SFGMPartNo] = [PartMaster].[PartNumber]
WHERE
[PartMaster].[PartGlassType]<>"G" AND
[PartMaster].[PartGlassType]<>"I" AND
[PartMaster].[PartGlassType]<>"L"
VBA Function:
Code:
Public Function GetShortDesc(GlassType As String) As Variant ' Because it can return Boolean or String!
On Error GoTo Error_GetShortDesc
Dim dbsdb1 As DAO.Database
Dim rstGlassFlat As DAO.Recordset
Dim boolGlassAssy As Boolean
Dim strCriteria As String
Set dbsdb1 = CurrentDb()
Set rstGlassFlat = dbsdb1.OpenRecordset("SELECT [PartMaster].[PartNumber], UCase([Colors].[ColorDescription]) AS [GlassColor] FROM PartMaster INNER JOIN Colors ON [PartMaster].[PartGlassColor] = [Colors].[ColorCode] WHERE [PartMaster].[PartNumber]='" & GlassType & "' AND NOT [PartMaster].[PartGlassType]='I' AND NOT [PartMaster].[PartGlassType]='L' AND NOT [PartMaster].[PartGlassType]='G' ORDER BY Len([PartMaster].[PartNumber]), [PartMaster].[PartNumber]", dbOpenForwardOnly)
GetShortDesc = False
If rstGlassFlat.RecordCount = 0 Then
Dim rstGlassAssy As DAO.Recordset
Set rstGlassAssy = dbsdb1.OpenRecordset("SELECT [BomTable].[BomParentPart], [BomTable].[BomComppart], [PartMaster_1].[PartGlassType] FROM (PartMaster INNER JOIN BomTable ON [PartMaster].[PartNumber] = [BomTable].[BomParentPart]) INNER JOIN PartMaster AS [PartMaster_1] ON [BomTable].[BomComppart] = [PartMaster_1].[PartNumber] WHERE [PartMaster].[PartNumber]='" & GlassType & "' AND [PartMaster_1].[PartGlassType] Is Not Null ORDER BY Len([BomTable].[BomParentPart]), [BomTable].[BomParentPart], [BomTable].[BomSeq]", dbOpenForwardOnly)
boolGlassAssy = True
If rstGlassAssy.RecordCount = 0 Then
GetShortDesc = "UNKNOWN"
Else
If rstGlassAssy("PartGlassType") = "L" Then
GetShortDesc = "LAM "
Else
GetShortDesc = "IG "
End If
Do While Not rstGlassAssy.EOF
GetShortDesc = GetShortDesc & rstGlassAssy("BomcompPart") & " / "
rstGlassAssy.MoveNext
Loop
GetShortDesc = Left(GetShortDesc, Len(GetShortDesc) - 3)
End If
Else
GetShortDesc = rstGlassFlat("GlassColor")
End If
FunctionClosing:
If boolGlassAssy = True Then
boolGlassAssy = False
rstGlassAssy.Close
Set rstGlassAssy = Nothing
End If
rstGlassFlat.Close
Set rstGlassFlat = Nothing
Set dbsdb1 = Nothing
Exit Function
Error_GetShortDesc:
GetShortDesc = False
Debug.Print Err.Number & ": " & Err.Description ' No MsgBox to prevent Query interruption!
Resume FunctionClosing
End Function