Database query statement help

Status
Not open for further replies.

edgevb

Smack-Fu Master, in training
80
First off thanks in advance.

Now, onward.
I'm not a real programmer, just a piddler of sorts. I'm developing an application for my job that will ease a process up. It's a simple database program to store equipment checked in/ checked out states. I am using Access2000 for the database structure, and Visual Basic 6 for the dev enviornment. There is a table in the database called ProjList, with only a handfull of fields. One of those is a 'Yes/No' field called 'IsCheckedOut'. I want to query the database and return a recordset containing all fields where 'IsCheckedOut' is Yes/True; however, my believed statement of:
----------
SELECT * FROM ProjList WHERE IsCheckedOut = True
----------
seems not to work. 0 records are returned (the recordset's .RecordCount is -1)

My instinct tells me it has something to do with how I need to specify boolean values in the query.

Any help would be appreciated.
 

edgevb

Smack-Fu Master, in training
80
Well, I did some further experiments and find that the recordset is indeed being properly filled, but that ADO is unable to determine the number of records through .RecordCount. Straight from MSDN
Use the RecordCount property to find out how many records are in a Recordset object. The property returns -1 when ADO cannot determine the number of records. Reading the RecordCount property on a closed Recordset causes an error.
For referance the single quotes still allows it to work just as well as without them, Access must not be picky enough (on a side note; what values deserve single quotes?)

I'm opening the recordset as adOpenDynamic, so I'm unsure why ADO is having such difficulty determining the recordcount.
 

Norrick

Ars Legatus Legionis
16,491
I've had the .RecordCount = -1 error before.

The solution is to do a .move last, THEN do a .RecordCount.

If you have a large recordset, this can take a while. But it does work.

One otehr way to do it is to open an additional recordset along the lines of this:

SELECT Count(FieldName) as RecordCount FROM tblBlah;

Then you just use the value of rsBlah("RecordCount") as your record count number. Again, this will add overhead but it works.

Good luck!
 

enkafan

Wise, Aged Ars Veteran
111
try this:
Code:
Dim rs As Recordset
Set rs = New Recordset
rs.Open " Select count(*) as RecordCount from customers; SELECT * FROM Customers", conn
MsgBox rs(0) & " records."
Set rs = rs.NextRecordset
Do Until rs.EOF
    Debug.Print rs(0)
    rs.MoveNext
Loop
rs.Close

This code assumes you have a valid conn object and will perform two queries. The first one will do the Count(*) and the second one will have the records. Show the first field of the first recordset (the count) in a message box, and then set rs equal to the next recordset in the object. then it uses the standard recordset stuff to print out a list of the customerid fields (field #0) into the intermediate window.

I think the only other good option would be to write a stored procedure that returns a recordset and an output parameter that contains @@ROWCOUNT.

Also, dynamic recordset are the devil so avoid them at all costs.
 
Status
Not open for further replies.