设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

使用ADO获取外部数据

1970-1-1 08:00| 发布者: Daily Dose| 查看: 3590| 评论: 0

翻译:tmtony(王宇虹) www.office-cn.net

一般使用 数据-》获取外部数据 或 vdookups来获取外部数据,但当数据量比较大,可直接使用ADO来获取外部数据。

代码如下:

Dim adoCn As ADODB.Connection
Dim adoRs As ADODB.Recordset
   
Function GetFields(sKey As String, lField As Long) As Variant
   
    Dim sCon As String, sSql As String
   
    '如果第一次调用创建记录集
    If adoCn Is Nothing Or adoRs Is Nothing Then
        sCon = "DSN=MS Access Database;" & _
            "DBQ=C:Program FilesMicrosoft Office 2000OfficeSamplesNorthwind.mdb;" & _
            "DefaultDir=C:Program FilesMicrosoft Office 2000OfficeSamples;" & _
            "DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"
        sSql = "SELECT ProductID, ProductName, QuantityPerUnit, Products.UnitPrice " & _
            "FROM Products"
       
        Set adoCn = New ADODB.Connection
        adoCn.Open sCon
       
        Set adoRs = New ADODB.Recordset
        adoRs.CursorType = adOpenDynamic
        adoRs.CursorLocation = adUseClient
        adoRs.Open sSql, adoCn
    End If
   
    adoRs.MoveFirst
    adoRs.Find "ProductID=" & sKey
   
    If adoRs.EOF Or adoRs.BOF Then
        GetFields = "Not found"
    Else
        GetFields = adoRs.Fields(lField).Value
    End If
   
End Function

运行后的效果:

ADOUDF1

最新评论

相关分类

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-29 06:37 , Processed in 0.146095 second(s), 16 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回顶部