So indeed, the connection does not see any record being returned by sTbl.
If sTbl is a saved query name, or a table name, can you try:
? DCount("*", sTbl)
(That expression cannot be used if sTbl is something like " table1 WHERE
field1=444", ie, something else that a table name or saved query name ).
If DCount does not return 0, and if the CurrentProject (ADO) is the same
database that CurrentDb (DAO), then you have a serious bug. Maybe a
corrupted database.
If DCount also returns 0, then there is no record under sTbl, ADO and
DAO both agree on that. Instead of opening the query, can you paste the
content of sTbl in the RowSource property of a ListBox control in a form
to see if the list box display something. If it also display nothing,
clearly, what sTbl hold is a table or a query returning nothing. Can you
then describe what you do to manually open the query? maybe there is a
typo of some sort?
Vanderghast, Access MVP
JA said:
That returned the following error:
Either BOF or EOF is True, or the current record has been deleted.
Requested operation requires a current record.
Michel Walsh said:
If this is not a matter of disconnected data or something similar, if it
is standard native data in Access, I highly suspect that GetCount2 is
never assigned and just returns its initial value of 0 it implicitly got
when the function stated; in other words, I highly suspect error
trapping. Can you disable it, and add something like:
Function GetCount2(sTbl As String) As Long
Dim strSql As String
ON ERROR GOTO 0 ' <----
strSql = "SELECT COUNT(*) FROM " & sTbl
GetCount2 = CurrentProject.Connection.Execute(strSql).Fields(0).Value
strSql="SELECT * FROM " & sTbl ' <----
Debug.Print
CurrentProject.Connection.Execute(strSql).Fields(0).Value '<---
End Function
and try it with a sTbl which creates a problem (while having records in
it). The second execute should pump the value of the first field, of the
first record, if there is at least one record.
Vanderghast, Access MVP
Yes, I tried, but no error is occurring, it just returns 0 for
recordcount, but when I open the query manually, records are returned.
Maybe it is because of the table/query name + error trapping 'on error
resume next'. Have you tried:
strSql = "SELECT COUNT(*) FROM [" & sTbl & "]"
GetCount2 =
CurrentProject.Connection.Execute(strSql).Fields(0).Value
Debug.Assert Err.Number = 0
Vanderghast, Access MVP
I am running the code below on a closed query to determine the count
of records. It works on some, but not all queries. If I manually
open the query records are returned, but the function returns 0 on
some queries.
Function GetCount2(sTbl As String) As Long
Dim strSql As String
strSql = "SELECT COUNT(*) FROM " & sTbl
GetCount2 = CurrentProject.Connection.Execute(strSql).Fields(0)
End Function