Solved

sql in ado

Posted on 2000-05-12
11
227 Views
Last Modified: 2013-11-23
Hi guys,
  I am using  an ado .I require to execute an sql statment such as "Select count(empno) from emp".I want to return the value of count to a variable.How can I do this"
0
Comment
Question by:shyma
  • 5
  • 3
  • 3
11 Comments
 
LVL 15

Expert Comment

by:StingRaY
ID: 2806468
shyma

Try this:

Dim rs As New ADODB.Recordset
rs.open "SELECT COUNT(empno) AS EMPCOUNT FROM emp",cn
rs!EMPCOUNT will provide "COUNT(empno)" from your SQL
Don't for get to close rs and set it to nothing.
0
 
LVL 4

Expert Comment

by:mberumen
ID: 2806527
shyma,  the cn on stingray's code refers to the connection to the database.
remember to declare it at the top of your code

Sub OpenMyDB()
Dim cn As New Connection
Dim rs As Recordset
Dim intCount as integer
'Create the connection.
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\Program Files\Microsoft Office\Office\" & _
        "Samples\Northwind.mdb;"
 
'Create recordset reference and set its properties.
    Set rs = New ADODB.Recordset
 'Open recordset.
    rs.open "SELECT COUNT(empno) AS EMPCOUNT FROM emp",cn

intCount=rs(0)

    rs.Close
set rs=nothing
    cn.Close
set cn=nothing
 
End Sub


'regards
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 2809572
mberumen

Thank you for referring cn to its origin.
I forgot to tell shyma what cn is.

regards
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shyma
ID: 2810328
when I applied the sql statment I got the count correctly.But my problem is not solved.I used findfirst and findnext method in dao.But in ado there is only find method.
   Can you please tell me whether find method will search for a record from the beginning and to move to next record what I have to use ?
  Similarly in dao we check whether the record is there or not with Nomatch property.How to make a check in case of ado?.Please suggest
0
 

Author Comment

by:shyma
ID: 2810330
Adjusted points from 20 to 25
0
 
LVL 4

Accepted Solution

by:
mberumen earned 25 total points
ID: 2811649
shyma... When you say that you want to search for e record from the beginning?...  you could select the record based on a condition that you can include in your SQL statement

i.e.
 rs.open "SELECT * FROM emp WHERE lastname='Smith';",cn

if rs.EOF<>True then
'We found a record!
else
'Record not found
end if
rs.close

Now if you want to select various records and scroll through them then use the rs.movenext ,movefirst or movelast to navigate through them

i.e.

 rs.open "SELECT * FROM emp WHERE lastname='Smith';",cn
Do while Not rs.eof
if rs.firstname="Bob" then
'We found Bob!
end if
rs.movenext
loop
rs.close

regards
0
 

Author Comment

by:shyma
ID: 2813073
Thank you for the answer.
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 2813075
shyma:
This is Find Syntax I copied from MSDN.

Find (criteria, SkipRows, searchDirection, start)

Parameters

criteria   A String containing a statement that specifies the column name, comparison operator, and value to use in the search.

SkipRows   An optional Long value, whose default value is zero, that specifies the offset from the current row or start bookmark to begin the search.

searchDirection   An optional SearchDirectionEnum value that specifies whether the search should begin on the current row or the next available row in the direction of the search. Its value can be adSearchForward or adSearchBackward. The search stops at the start or end of the recordset, depending on the value of searchDirection.

start   An optional Variant bookmark to use as the starting position for the search.

------------------------
PS.
..NoMatch is equivalent to .EOF
if .Find return no record found , .EOF = True
otherwise, Cursor will pose to result record.

0
 
LVL 15

Expert Comment

by:StingRaY
ID: 2813085
mberumen:
Your answer is okay. But if you want to search thru 1,000,000 record, it means you have to take 1,000,000 itteration.
0
 
LVL 4

Expert Comment

by:mberumen
ID: 2813168
I agree stingRay,  I wasn't sure of what shyma was trying to accomplilsh, however.  I assume that you would limit the amount of records in your recordset by running a query or a filter before opening the recordset.

regards
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 2813316
Ok.
Let shyma take his own decision.
regards.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi...Split view - idea? 1 113
Child Form in front 4 56
Not able to call Delphi XE10 dll function from Delphi 6.0 4 48
MS Access from Delphi 31 77
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question