Problem with .EOF and .BOF (1 Viewer)

rede96

Registered User.
Local time
Today, 03:57
Joined
Apr 2, 2004
Messages
134
I'm using the DOA.Recordset method to open a recordset based on a SQL statement. (SELECT * FROM ....)

I then use 'If Not (rst.EOF and rst.BOF) then' ... to check if the recordset is populated.

This normally works fine except when I use some sort of Group by, Max etc. in the SQL statement. In this case, even when there are no records, the .EOF and .BOF read false. I even get a record count of 1.

Am I doing something wrong in the way that I am generating the recordset?

An example of my SQL would be as follows:

Code:
    strSQL = "SELECT Max(Tbl_ScanResults.EndTimeStamp) AS EndTimeStamp " & _
    "FROM Tbl_ScanResults WHERE (((Tbl_ScanResults.ScannerID)='" & MyScanID(i) & "') AND " & _
    "((DateValue([EndTimeStamp]))=DateValue(Now())));"

Any help would be much appreciated!
 
Last edited:

Minty

AWF VIP
Local time
Today, 03:57
Joined
Jul 26, 2013
Messages
10,371
If you debug.print your sql statement and run it in the query window what result do you get?

I'm also a little wary of calling the max() value alias the same as the field name, pretty sure access has a bit of a hissy fit when you do that sometimes...
 

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 22:57
Joined
Apr 27, 2015
Messages
6,359
Must be hard-coded into the female OS!
 

isladogs

MVP / VIP
Local time
Today, 03:57
Joined
Jan 14, 2017
Messages
18,246
I find Access throws a 'massive wobbly' when it doesn't like what I want it to do...
 

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 22:57
Joined
Apr 27, 2015
Messages
6,359
Only if the settings are set to "Queen's English"...
 

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 22:57
Joined
Apr 27, 2015
Messages
6,359
Sorry for hijacking your thread Rede96...I would offer a suggestion, but I can't improve upon Minty's.
 

rede96

Registered User.
Local time
Today, 03:57
Joined
Apr 2, 2004
Messages
134
If you debug.print your sql statement and run it in the query window what result do you get?

I'm also a little wary of calling the max() value alias the same as the field name, pretty sure access has a bit of a hissy fit when you do that sometimes...

The SQL looked fine and worked fine for records that were populated.

However...after putting the SQL back into a normal query and playing around with it, I noticed it changed the SQL to using HAVING and GROUP BY instead of WHERE.

Code:
    strSQL = "SELECT Max(Tbl_ScanResults.EndTimeStamp) AS EndTimeStamp FROM Tbl_ScanResults " & _
    "GROUP BY Tbl_ScanResults.ScannerID, DateValue([EndTimeStamp])HAVING " & _
    "(((Tbl_ScanResults.ScannerID)='" & MyScanID(i) & "') AND ((DateValue([EndTimeStamp]))=DateValue(Now())));"

So I changed my SQL for my recordset and .EOF/.BOF now work...:confused:

Typical female operating system. There's always a number of ways to ask, but if you don't ask in the right way... :)
 

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 22:57
Joined
Apr 27, 2015
Messages
6,359
Typical female operating system. There's always a number of ways to ask, but if you don't ask in the right way... :)

Quite right, thanks for marking the thread as "solved"
 

Minty

AWF VIP
Local time
Today, 03:57
Joined
Jul 26, 2013
Messages
10,371
Glad you sorted it - I think it would work quicker with

((DateValue([EndTimeStamp]))=Date())

As you wouldn't be converting Now() to the current date for each record. (I suspect we're not talking earth shattering performance differences here...)
 

Users who are viewing this thread

Top Bottom