Hello All,
I have an embedded SQL statement in VBA (Access 2002). In my statement, I would like to write a simple VBA function which returns a boolean. Then I want to use this function as my argument for my WHERE clause.
The idea is that when the function evaluates/returns TRUE, the record will show in the query and when FALSE, it will not.
My code is a little messy, but here it is: (NOTE: Code works perfectly without the introduction of the WHERE clause)
I am unsure if access/VBA requires the 'extra End If s' but I put them there when I was trying to bludgeon my way through the syntax.
Thanks - blakey2.
I have an embedded SQL statement in VBA (Access 2002). In my statement, I would like to write a simple VBA function which returns a boolean. Then I want to use this function as my argument for my WHERE clause.
The idea is that when the function evaluates/returns TRUE, the record will show in the query and when FALSE, it will not.
My code is a little messy, but here it is: (NOTE: Code works perfectly without the introduction of the WHERE clause)
Code:
__________________________________________
Public Function generateAllQuery(typeFlag As Integer)
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim table1 As String
Dim table2 As String
Dim prodGrade As String
Dim prodMod As String
Dim qryNAME As String
Dim result As Boolean
' Deal with the three type flags.
' I cut this code out as not important eg init. below...
table1 = "tblColesStoresMasterList"
table2 = "tblGarbageBags"
qryNAME = "qryGarbageBags"
prodGrade = "GBGrade"
prodMod = "GBMods"
Set db = CurrentDb
Set qdf = db.QueryDefs(qryNAME)
' Below is summarised version of my query (should be the
' important bits.
strSQL = " SELECT CS.*, P.* " & _
"FROM " & table1 & " AS CS " & _
" LEFT OUTER JOIN " & table2 & " AS P " & _
"ON CS.StoreCode = P.StoreCode " & _
"WHERE filterTable(CS." & prodGrade & ", P.Grade) " & _
"ORDER BY CS.State, CS.StoreCode;"
qdf.SQL = strSQL
Set qdf = Nothing
Set db = Nothing
End Function
_______________________________________________
Public Function filterTable(LCSGrade As String, LPGrade As String) As Boolean
If LPGrade = C And (LCSGrade = C Or LCSGrade = B Or LCSGrade = A) Then
filterTable = True
End If
ElseIf LPGrade = B And (LCSGrade = B Or LCSGrade = A) Then
filterTable = True
End If
ElseIf LPGrade = A And LCSGrade = A Then
filterTable = True
End If
Else
filterTable = False
End If
End If
End Function
_________________________________________________
I am unsure if access/VBA requires the 'extra End If s' but I put them there when I was trying to bludgeon my way through the syntax.
Thanks - blakey2.