Sorry, subsequent to posting I realized the problem is in ADO, not in the Query and I couldn't edit the post because it requires approval.
I ran the two queries using the VB-Demo and got the same result for each, so the query syntax is correct.
The problem is that the following ADO code fails to return a correct result to the recordset using the following recordset open statement:
rs.Open vSQL, cn, adOpenStatic, adLockOptimistic
The result is that the second field Item.ListID contains a constant value in every record (which happens to be the value of the last record). This only occurs if I name fields in the SELECT. If I use SELECT * from <rest of query> then Item.ListID appears correctly.
but the second query does work with a dynamic recordset opened as follows
rs.Open vSQL, cn
The problem with this is that when you open a recordset this way, it will not give you a recordcount. Always returns -1.
I ended up working around by using DO WHILE NOT RS.EOF and counting the records manually, but I'm concerned that there are conditions that will return incorrect results which could cause me some future confusion/errors.
Is this just an attribute of using ADO with QODBC or am I coding incorrectly?
Sub TestSpecial1()
'Opens connection if not already open in Global var
If CNisOpen <> True Then Call InvokeQODBC Debug.Print Now(), "Called Invoke QODBC" End If
Dim rs As ADODB.Recordset Set rs = CreateObject("ADODB.Recordset")
'Returns incorrect results in Item.ListID with adOpenStatic, works only if open adOpenDynamic vSQL = "Select BillItemLine.ItemLineItemRefListID, Item.ListID from " BillItemLine Inner Join Item on BillItemLine.ItemLineItemRefListID = Item.ListID " & "Where BillItemLine.IsPaid = False" rs.Open vSQL, cn, adOpenStatic, adLockOptimistic
ActiveSheet.Cells.ClearContents For iCols = 0 To rs.Fields.Count - 1 ActiveSheet.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name Next
rs.MoveFirst ActiveSheet.Range("a2").CopyFromRecordset rs
Debug.Print Now(), "Query Complete"
rs.Close Set rs = Nothing
End Sub
|