📄 mdprofilterx.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML dir=ltr>
<HEAD>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=gb2312"><title>Filter 和 RecordCount 属性范例</title>
<style>@import url(msdn_ie4.css);</style>
</HEAD>
<BODY>
<h3><a name="mdprofilterx"></a>Filter 和 RecordCount 属性范例</h3>
<p>
该范例使用 <b>Filter</b> 属性打开一个新的 <b>Recordset</b>,它基于适用于已有 <b>Recordset</b> 的指定条件。它使用 <b>RecordCount</b> 属性显示两个 <b>Recordsets</b> 中的记录数。该过程运行时需要 FilterField 函数。</p>
<pre>Public Sub FilterX() Dim rstPublishers As ADODB.Recordset
Dim rstPublishersCountry As ADODB.Recordset
Dim strCnn As String
Dim intPublisherCount As Integer
Dim strCountry As String
Dim strMessage As String ' 使用出版商表中的数据打开记录集。
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
Set rstPublishers = New ADODB.Recordset
rstPublishers.CursorType = adOpenStatic
rstPublishers.Open "publishers", strCnn, , , adCmdTable ' 充填记录集。
intPublisherCount = rstPublishers.<span class=cfe><b>RecordCount</b></span> ' 获得用户输入。
strCountry = Trim(InputBox( _
"Enter a country/region to filter on:")) If strCountry <> "" Then
' 打开已筛选的记录集对象。
Set rstPublishersCountry = _
FilterField(rstPublishers, "Country", strCountry) If rstPublishersCountry.<span class=cfe><b>RecordCount</b></span> = 0 Then
MsgBox "No publishers from that country/region."
Else
' 打印原始记录集和已筛选记录集对象的记录数。
strMessage = "Orders in original recordset: " & _
vbCr & intPublisherCount & vbCr & _
"Orders in filtered recordset (Country = '" & _
strCountry & "'): " & vbCr & _
rstPublishersCountry.<span class=cfe><b>RecordCount</b></span>
MsgBox strMessage
End If
rstPublishersCountry.Close End IfEnd SubPublic Function FilterField(rstTemp As ADODB.Recordset, _
strField As String, strFilter As String) As ADODB.Recordset ' 在指定的记录集对象上设置筛选操作并打开一个新的记录集对象。
rstTemp.<span class=cfe><b>Filter</b></span> = strField & " = '" & strFilter & "'"
Set FilterField = rstTempEnd Function
</pre>
<p class=indent>
<B><b>注意 </b></B>当已知要选择的数据时,使用 SQL 语句打开 <b>Recordset</b> 通常更为有效。该范例说明了如何创建唯一的 <b>Recordset</b> 并从特定的国家(地区)获得记录。</p>
<pre>Public Sub FilterX2() Dim rstPublishers As ADODB.Recordset
Dim strCnn As String ' 使用出版商表中的数据打开记录集。
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
Set rstPublishers = New ADODB.Recordset
rstPublishers.CursorType = adOpenStatic
rstPublishers.Open "SELECT * FROM publishers " & _
"WHERE Country = 'USA'", strCnn, , , adCmdText
' 打印记录集中的当前数据。
rstPublishers.MoveFirst
Do While Not rstPublishers.EOF
Debug.Print rstPublishers!pub_name & ", " & _
rstPublishers!country
rstPublishers.MoveNext
Loop rstPublishers.CloseEnd Sub</pre>
<center> <A HREF="http://www.51windows.Net">www.51windows.Net</A></center>
<SCRIPT LANGUAGE="JavaScript" src="/log/sitelog2.asp"></SCRIPT>
<script src="script.js"></script></BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -