用 ado find 方法
dao 包含了四个“ find ”方法: findfirst,findlast,findnext 和 findprevious .
dao 方法 ado find 方法
下面的一个例子示范了如何用 ado find 方法查询记录:
sub findrecord(strdbpath as string, _
strtable as string, _
strcriteria as string, _
strdisplayfield as string)
' this procedure finds a record in the specified table by
' using the specified criteria.
' for example, to use this procedure to find records
' in the customers table in the northwind database
' that have " usa " in the country field, you can
' use a line of code like this:
' findrecord _
' "c:/program files/microsoft office/office/samples/northwind.mdb", _
' "customers", "country=' usa '", "customerid"
dim cnn as adodb.connection
dim rst as adodb.recordset
' open the connection object.
set cnn = new adodb.connection
with cnn
.provider = "microsoft.jet.oledb.4.0"
.open strdbpath
end with
set rst = new adodb.recordset
with rst
' open the table by using a scrolling
' recordset object.
.open source:=strtable, _
activeconnection:=cnn, _
cursortype:=adopenkeyset, _
locktype:=adlockoptimistic
' find the first record that meets the criteria.
.find criteria:=strcriteria, searchdirection:=adsearchforward
' make sure record was found (not at end of file).
if not .eof then
' print the first record and all remaining
' records that meet the criteria.
do while not .eof
debug.print .fields(strdisplayfield).value
' skip the current record and find next match.
.find criteria:=strcriteria, skiprecords:=1
loop
else
msgbox "record not found"
end if
' close the recordset object.
.close
end with
' close connection and destroy object variables.
cnn.close
set rst = nothing
set cnn = nothing
end sub
例如,用用这个过程查询“罗期文商贸”示例数据库中“客户”表的“国家”等于 usa 的记录,可以使用下面的代码:
findrecord “c:/program files/microsoft office/office/samples/northwind.mdb”, _
“customers”, “country=' usa '”, ”customerid”
( 译者注:如果你安装的是简体中文版要将相应的字段名改成中文 )
用 ado seek 方法
因为 ado seek 方法使用 index ,最好是在用这个方法之前指定一个索引,可是,如果你没有指定索引, jet database engine 将用主键。
如果你需要从多个字段中指定值做为搜索条件,可以用 vba 中的 array 函数传递这些值到参数 keyvalues 中去。如果你只需要从一个字段中指定值做为搜索条件,则不需要用 array 函数传递。
象 find 方法一样,你可以用 bof 或者 eof 属性测试是否查询到记录。
下面的一个例子显示了如何用 ado seek 方法查询记录:
sub seekrecord(strdbpath as string, _
strindex as string, _
strtable as string, _
varkeyvalues as variant, _
strdisplayfield as string)
' this procedure finds a record by using
' the specified index and key values.
' for example, to use the primarykey index to
' find records in the order details table in the
' northwind database where the orderid field is
' 10255 and productid is 16, and then display the
' value in the quantity field, you can use a line
' of code like this:
' seekrecord _
' "c:/program files/microsoft office/office/samples/northwind.mdb", _
' "primarykey", "order details", array(10255, 16), "quantity"
dim cnn as adodb.connection
dim rst as adodb.recordset
' open the connection object.
set cnn = new adodb.connection
with cnn
.provider = "microsoft.jet.oledb.4.0"
.open strdbpath
end with
set rst = new adodb.recordset
with rst
' select the index used to order the
' data in the recordset.
.index = strindex
' open the table by using a scrolling
' recordset object.
.open source:=strtable, _
activeconnection:=cnn, _
cursortype:=adopenkeyset, _
locktype:=adlockoptimistic, _
options:=adcmdtabledirect
' find the order where orderid = 10255 and
' productid = 16.
.seek keyvalues:=varkeyvalues, seekoption:=adseekfirsteq
' if a match is found, print the value of
' the specified field.
if not .eof then
debug.print .fields(strdisplayfield).value
end if
' close the recordset object.
.close
end with
' close connection and destroy object variables.
cnn.close
set rst = nothing
set cnn = nothing
end sub
例如,用主键索引查询“罗期文商贸”示例数据库中“订单明细”表的“订单编号”等于 10255 并且产品编号等于 16 的 ” 数量 ” 的值,可以使用下面的代码:
seekrecord “c:/program files/microsoft office/office/samples/northwind.mdb”, _
“primarykey”, “order details”, array(10255,16), ”quantity”
( 译者注:如果你安装的是简体中文版要将示例中引用的相应的字段名改成中文 )
新闻热点
疑难解答