RecordCount Function:
Returns the record count returned by an SQL statement.
Description:
The RecordCount function returns a long representing the record count returned by an SQL statement. Works either with MSAccess, MSSQL and MySQL. There are two required arguments: ConnString and SQL. ConnString must be set to a valid database connection string and SQL must be set to a valid SQL statement. RecordCount returns Null if error handling is enabled and a problem is encountered while obtaining a record count.
Syntax:
long = RecordCount(connstring, sql)
Example:
<%
'--- Use RecordCount to determine the record count returned by an SQL statement.
Dim a, cn, SQL cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.Mappath("/database/mydata.mdb") & ";" SQL = "SELECT ID FROM Table_Name" a = RecordCount(cn, SQL) response.write a & " Records Found!"
%>
ASP Source Code:
<%
Private Function RecordCount(byVal connstring, byVal sql) '--- MSAccess, MSSQL and MySQL Dim objCn, bErr1, bErr2, strErrDesc, objRs, intRows On Error Resume Next Set objCn = Server.CreateObject("ADODB.Connection") objCn.Open ConnString If Err Then bErr1 = True Else Set objRs = Server.CreateObject("ADODB.Recordset") objRs.Open sql, objCn, 3, 1, &H0001 '--- adOpenStatic, adLockReadOnly, adCmdText If objRs.BOF then RecordCount = 0 Else intRows = objRs.GetRows objRs.MoveFirst RecordCount = CLng(UBound(intRows, 2) + 1) End If objRs.Close Set objRs = Nothing If Err Then bErr2 = True strErrDesc = Err.Description End If End If objCn.Close Set objCn = Nothing On Error GoTo 0 If bErr1 then Err.Raise 5109, "RecordCount Function", "Bad connection string. Database cannot be accessed." RecordCount = Null ElseIf bErr2 then Err.Raise 5109, "RecordCount Function", strErrDesc RecordCount = Null End If End Function '--- RecordCount
%>
|