SQL "Last" instruction returns wrong value
I used the SQL instruction below:
sql1 = SELECT LAST(FLDS_Idx) FROM EXT_Field_Src
sql2 = SELECT LAST(FLDD_Idx) FROM EXT_Field_Dst
Where Idx represents autonumber fields starting at 1 for the table EXT_Field_Src and 0 for the table EXT_Field_Dst. By opening an ADO recordset using the function at the bottom of the post.
I get the correct answer for sql1 -- 88 and incorrect answer for sq12 -- 0 (it should be 87). The only difference between the tables
that i can see is that the one that returns 0 also starts at 0. This may be a red herring...
Another important point is that when i use MAX instead of LAST it works.
If anyone knows of similar strange occurences or possible reasons for this plz let me know; otherwise MAX does the job.
Thanks
'Executes a complete sequence of sql code which returns a single integer
Public Function SQL_execute_int(sql As String) As Integer
Const C_FUNCTION = "SQL_execute"
'MsgBox sql
Dim condatabase As New ADODB.connection
Dim rst As New ADODB.recordSet
Set condatabase = CurrentProject.connection
rst.ActiveConnection = condatabase
Dim fieldname As String, value As Integer
On Error GoTo ERR_SQL
rst.Open sql, condatabase, adOpenKeyset, adLockOptimistic
If rst.EOF = True Then
Call ERR_raise(ERR_sql_exec, C_FUNCTION, "No integer was returned") 'No records were returned
Else
rst.MoveFirst
fieldname = rst.fields(0).name
If (rst.EOF = False) Then
value = rst.fields(0).value
End If
End If
rst.Close
condatabase.Close
Set condatabase = Nothing
SQL_execute_int = value
Exit Function
ERR_SQL:
Call ERR_throw_auto(ERR_sql_exec, C_FUNCTION, "Error executing SQL ||| " & sql & " |||")
End Function
|