Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

Do...Loop 循环

时 间:2010-04-06 16:52:05
作 者:杨鑫   ID:7434  城市:广州
摘 要:本文通过一些简单的ADO,来练习 Do...Loop 循环
正 文:

点击下载此附件

目的:练习 Do...Loop 循环

相关知识:ADO

语法:

Do [While | Until Condition]

         [VBA statements]

         [Exit Do]

         [VBA statements]

Loop

或者,While(或 Until)也可以位于结构底部:

Do

         [VBA statements]

         [Exit Do]

         [VBA statements]

Loop  [While | Until Condition]

补充:当条件为 True 时, While 子句让 Do...Loop 中的VBA语句执行,否则,退出Do...Loop循环;

            Until 子句的作用刚好相反,只有当条件为 False 时,Do...Loop中的代码才会执行。

代码:

Option Compare Database
Option Explicit

Public Sub PrintCompany()
    Dim rs As ADODB.Recordset

    Set rs = New ADODB.Recordset

    rs.Open "tblContacts", CurrentProject.Connection, adOpenDynamic, adLockOptimistic

    Do Until rs.EOF     '设定循环条件:直到记录集的指针移动到空记录时,停止循环
        Debug.Print rs![company]     '打印公司名称
        rs.MoveNext     '移动到下一条记录
    Loop    '循环

    rs.Close    '关闭记录集
    Set rs = Nothing    '将记录集从内存中清除

End Sub

结果:

Nopa Auto Parts
Jiffy Auto Sales
All Start Autos
Columbia Chevy
James Auto Parts
Fillion Sales And Service
Tip Top Chevy
Newbury Auto
Peekskill Sales
KJ Auto Repair
R & G Monda Inc
Tires National
Hayes Auctions
Middletown Auto Parts
Bill Thomas Sales, Inc
Yantic Auto Parts
ACC Car Sales
A-1 Auto Sales
Main Street Used Auto Parts
LAN Trucking
ABC Trucking
Fordman Colonial Times
Circle Auto Sales
Sammy Fordman
Pine Plains BMY
Pleasantville Monda Inc
Newton Monda Inc
American Auto Sales
Allstate International Sales
Bush Sales
Fordman Sales
First Chance Auto
Nopa East Auto Parts
Last Stop Auto
McDonald Sales
On the Hill
El Pasco Auto Sales
SaundersTown Auto Sales
Hatter Sales
Corona Fordman Sales
Montclair Auto
Sherman Oaks Auto Sales
Bell Auto Sales
Jackpot Fordman
Jackhorn Monda, Inc
McHugh Cash Auto Sales
Sakes American Autos
Ghost Automobile Sales
Iron Springs Auto Sales
Salado Formal Auto Sales
Elsa Honest Sales
Chicota American Auto Sales
M&M Sales
Greenleaf Car Sales
Knox Sales
Jackson & Sons
Paul's Best Autos
Truxton Auto Sales



Access软件网QQ交流群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助