I have two tables as follows.
TABLE CANBE
TABLE SET
And I have a stored query called "GP" like this
PARAMETERS [Policy_Name] Text, [Sequence] Long;
SELECT C.Name, C.Canbe, C.Seq, S.Level, S.Value, S.Set
FROM Canbe As C INNER JOIN Set As S ON (C.Canbe = S.Level) AND (C.Name = S.Name)
WHERE C.P_Name = [Policy_Name] AND
C.Seq >= [Sequence] AND
S.Level <> 'COMPANY'
ORDER BY C.Seq;
When I run this from VB with the following code (DAO),
Dim qdf As DAO.QueryDef
Set qdf = dbControl.QueryDefs.Item("GP"
qdf("Policy_Name"
= "ACCEPT_RET"
qdf("Sequence"
= 3
Set rsCanbe = qdf.OpenRecordset(dbOpenDynaset)
I get
and all is as it should be.
However, if I run it from VB directly (i.e. not as an existing stored query) like this
Set rsCanbe = db.OpenRecordset( _
"SELECT C.Name, C.Canbe, C.Seq, S.Level, S.Value, S.Set
FROM Canbe As C INNER JOIN Set As S ON (C.Canbe = S.Level) AND (C.Name = S.Name)
WHERE C.P_Name = 'ACCEPT_RET' AND
C.Seq >= 3 AND
S.Level <> 'COMPANY'
ORDER BY C.Seq;", dbOpenDynaset)
It Returns
The first record shouldn't be there because the value of "Seq" is less than 3, contrary to the "C.Seq >= 3" in the WHERE clause. Is this just one of those "weird computer things that happens all the time" or am I making a logical error somewhere?
If I copy and paste this SQL into an Access query, it produces the correct results.
TABLE CANBE
Code:
NAME Seq Canbe
ACCEPT_RET 1 STOCK
ACCEPT_RET 2 SUBDETAIL
ACCEPT_RET 3 SUBDEPT
ACCEPT_RET 4 DEPT
ACCEPT_RET 5 COMPANY
... etc.
TABLE SET
Code:
NAME LEVEL VALUE SET
ACCEPT_RET DEPT 34 No
ACCEPT_RET DEPT 37 No
ACCEPT_RET DEPT 99 No
ACCEPT_RET SUBDEPT 77 No
ACCEPT_RET SUBDETAIL A No
... etc.
And I have a stored query called "GP" like this
PARAMETERS [Policy_Name] Text, [Sequence] Long;
SELECT C.Name, C.Canbe, C.Seq, S.Level, S.Value, S.Set
FROM Canbe As C INNER JOIN Set As S ON (C.Canbe = S.Level) AND (C.Name = S.Name)
WHERE C.P_Name = [Policy_Name] AND
C.Seq >= [Sequence] AND
S.Level <> 'COMPANY'
ORDER BY C.Seq;
When I run this from VB with the following code (DAO),
Dim qdf As DAO.QueryDef
Set qdf = dbControl.QueryDefs.Item("GP"
qdf("Policy_Name"
qdf("Sequence"
Set rsCanbe = qdf.OpenRecordset(dbOpenDynaset)
I get
Code:
Name Canbe Seq Level Value Set
ACCEPT_RET SUBDEPT 3 SUBDEPT 77 No
ACCEPT_RET DEPT 4 DEPT 99 No
ACCEPT_RET DEPT 4 DEPT 37 No
ACCEPT_RET DEPT 4 DEPT 34 No
However, if I run it from VB directly (i.e. not as an existing stored query) like this
Set rsCanbe = db.OpenRecordset( _
"SELECT C.Name, C.Canbe, C.Seq, S.Level, S.Value, S.Set
FROM Canbe As C INNER JOIN Set As S ON (C.Canbe = S.Level) AND (C.Name = S.Name)
WHERE C.P_Name = 'ACCEPT_RET' AND
C.Seq >= 3 AND
S.Level <> 'COMPANY'
ORDER BY C.Seq;", dbOpenDynaset)
It Returns
Code:
NAME Canbe Seq LEVEL VALUE SET
ACCEPT_RET SUBDETAIL 2 SUBDETAIL A No
ACCEPT_RET SUBDEPT 3 SUBDEPT 77 No
ACCEPT_RET DEPT 4 DEPT 99 No
ACCEPT_RET DEPT 4 DEPT 37 No
ACCEPT_RET DEPT 4 DEPT 34 No
If I copy and paste this SQL into an Access query, it produces the correct results.