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

ADO记录集与二维数组互导

时 间:2020-02-13 13:14:52
作 者:AngelHis   ID:20576  城市:沈阳
摘 要:ADO记录集与二维数组互导
正 文:

Option Compare Database
Option Explicit

Function RST_TO_ARR(RST As ADODB.Recordset) As Variant
'记录集转二维数组
    Dim I As Long, J As Long, R As Long, F As Long
    Dim ARR As Variant
    R = RST.RecordCount
    F = RST.Fields.Count
    If R = 0 Then Exit Function
    ReDim ARR(1 To R, 1 To F)
    Do While RST.EOF = False
        I = I + 1
        For J = 1 To F
            ARR(I, J) = RST.Fields(J - 1)    '  MsgBox arr(I, J), , I
        Next
        RST.MoveNext

    Loop

    RST.Close 

    RST_TO_ARR = ARR
End Function


Function ARR_TO_RST(ARR As Variant, RST As ADODB.Recordset)
'二维数组转记录集
    Dim I As Long, J As Long, L As Long
    For I = 1 To UBound(ARR)
        If RST.State = 0 Then RST.Open
        RST.AddNew
        For J = 0 To RST.Fields.Count - 1
            RST.Fields(J).Value = ARR(I, J + 1) & I
        Next
        RST.Update
    Next

End Function

'======================================================
Function TEST()
    Dim RST As New ADODB.Recordset, X As Variant, Y As Variant
    '记录集转二维数组
    RST.Open "Select  A,B,C  FROM TA", CurrentProject.Connection, adOpenStatic, adLockReadOnly
    If RST.State = 1 Then X = RST_TO_ARR(RST)
    RST.Close
    '二维数组转记录集
    RST.Open "Select A,B,C FROM TA", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
    If RST.State = 1 Then Y = ARR_TO_RST(X, RST)
    RST.Close
End Function



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

常见问答:

技术分类:

相关资源:

专栏作家

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