Office中國(guó)論壇/Access中國(guó)論壇

 找回密碼
 注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

使用ADO獲取外部數(shù)據(jù)

1970-1-1 08:00| 發(fā)布者: Daily Dose| 查看: 3993| 評(píng)論: 0

翻譯:tmtony(王宇虹) m.mzhfr.cn

一般使用 數(shù)據(jù)-》獲取外部數(shù)據(jù) 或 vdookups來(lái)獲取外部數(shù)據(jù),但當(dāng)數(shù)據(jù)量比較大,可直接使用ADO來(lái)獲取外部數(shù)據(jù)。

代碼如下:

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
   
    '如果第一次調(diào)用創(chuàng)建記錄集
    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

運(yùn)行后的效果:

ADOUDF1

最新評(píng)論

相關(guān)分類

QQ|站長(zhǎng)郵箱|小黑屋|手機(jī)版|Office中國(guó)/Access中國(guó) ( 粵ICP備10043721號(hào)-1 )  

GMT+8, 2025-7-13 07:52 , Processed in 0.071612 second(s), 16 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回頂部