Sign in to follow this  
Followers 0
DrParko

ADODB Number of Records

7 posts in this topic

I created a query GUI, largely based on code posted by DaRam. I have it working flawlessly but I wanted it to display the number of records returned. I have tried several different ideas with no luck. I also looked at some ADO sites with no luck. This is two snippets of the code, the connection and query sections.

Global $adoCn = ObjCreate( "ADODB.Connection" )
$adoCn.Properties("Prompt") = 2; 1=PromptAlways, 2=PromptComplete
Global $adoRs = ObjCreate( "ADODB.RecordSet" )
.
.
.
Case $ButtonQuery
            $QueryTxt = GUICtrlRead($EditSQL)
            If $adoRs.State = $adStateOpen Then $adoRs.Close
            GUICtrlSetData($EditResults, "Executing " & $QueryTxt)
            $adoRs = $adoCn.Execute($QueryTxt)
            If $adoRs.State = $adStateOpen Then
               GUICtrlSetData($RowResults, $adoRs.RecordCount)
               $ResultsTxt = ""
               $J = $adoRs.Fields.Count - 1
               For $I = 0 To $J
                     $ResultsTxt = $ResultsTxt & $adoRs.Fields($I).Name & @TAB
               Next;$I
               $ResultsTxt = $ResultsTxt & @CRLF
               $ResultsTxt = $ResultsTxt & $adoRs.GetString(2, -1, @TAB, @CRLF, "Null")
               GUICtrlSetData($EditResults, $ResultsTxt)
               $adoRs.Close
           EndIf

I tried a while loop to count records but couldn't get that to work. With this code, I get a -1 displayed in the $RowResults label. Any ideas?

Thanks in advance..

Share this post


Link to post
Share on other sites



Depends on the exact query string you are using. For example, some cursor types (i.e. "ForwardOnly") don't support .RecordCount. Post an otherwise working example for $QueryTxt that gives you good data but no .RecordCount.

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

http://dundats.mvps.org/autoit/udf_code.aspx?udf=access

Look at _accessCountRecords()


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Depends on the exact query string you are using. For example, some cursor types (i.e. "ForwardOnly") don't support .RecordCount. Post an otherwise working example for $QueryTxt that gives you good data but no .RecordCount.

:mellow:

PsaltyDS,

The query is variable as it is taken from an input box, but here is one that returns a list of records:

SELECT u.userid, a.viewed_status, u.last_login

from users u, autoemail a

where u.userid = a.userid and u.last_login is not null and a.viewed_status is null

I understand that some queries do not return records and therefore may not .RecordCount. Is there a way to determine that by information within the Record Set?

Share this post


Link to post
Share on other sites

Getting -1 for .RecordCount is exactly how it tells you that property is not supported for this recordset. You can just walk through the records in a While/WEnd loop and count them as you go. Or add an SQL query specifically for the COUNT.

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks PsaltyDS. I was able to add a while loop and count. It looks like this now:

Case $ButtonQuery
            $QueryTxt = GUICtrlRead($EditSQL)
            If $adoRs.State = $adStateOpen Then $adoRs.Close
            GUICtrlSetData($EditResults, "Executing " & $QueryTxt)
            $adoRs = $adoCn.Execute($QueryTxt)
            If $adoRs.State = $adStateOpen Then
               $ResultsTxt = ""
               $J = $adoRs.Fields.Count - 1
                For $I = 0 To $J
                     $ResultsTxt &= $adoRs.Fields($I).Name & @TAB
                Next;$I
               $ResultsTxt &= @CRLF
               $RowCount = 0
               While Not $adoRs.EOF
                    For $I = 0 To $J
                        $ResultsTxt &= $adoRs.Fields($I).Value & @TAB
                    Next;$I
                    $ResultsTxt &= @CRLF
                    $RowCount = $RowCount + 1
                    $adoRs.MoveNext
                WEnd
               GUICtrlSetData($RowResults, $RowCount)
               GUICtrlSetData($EditResults, $ResultsTxt)
               $adoRs.Close
           EndIf

Thanks again for your help, not only on this issue, but all of the issues you have responded to. I run into your posts on a regular basis!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0