shyma
asked on
sql in ado
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"
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"
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.OL EDB.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
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.OL
"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
mberumen
Thank you for referring cn to its origin.
I forgot to tell shyma what cn is.
regards
Thank you for referring cn to its origin.
I forgot to tell shyma what cn is.
regards
ASKER
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
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
ASKER
Adjusted points from 20 to 25
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the answer.
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.
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.
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.
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.
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
regards
Ok.
Let shyma take his own decision.
regards.
Let shyma take his own decision.
regards.
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.