使用VBA访问ACCESS,Excel文件的方法

    技术2022-05-11  60

    Access中用VBA得到和修改数据库中表数据:

    ADO :

    Dim rstType As ADODB.RecordsetDim strSQL as String

    Set rstType = New ADODB.RecordsetstrSQL =”Select F1,F2 From Table 1”

    rstType.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdText

    Do While Not rstType.EOF         rstType!F1=xxx         rstType.MoveNext Loop

      DAO

    Dim dbTmp As DatabaseDim rstType As Recordset

    Set dbTmp = CurrentDbSet rstType = dbTmp.OpenRecordset("Select F1,F2 >From Table1", dbOpenDynaset)

    Do While Not rstType.EOF

    rstType.EditrstType.Fields("F1").Value = xxxrstType.UpdaterstType.MoveNext

    Loop

        读取Excel数据

    ADO

    Dim cnt As ADODB.ConnectionDim rsT As ADODB.RecordsetDim sConnStrXls As String  

    Set cnt = New ADODB.ConnectionsConnStrXls = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & strFileName & ";Extended Properties=Excel 8.0;"cnt.Open sConnStrXls  

    Set rsT = New ADODB.Recordset

    With rsT

        Set .ActiveConnection = cnt    .Source = "Select * from [SheetName$A1:W6000]"    .CursorLocation = adUseClient    .Open

    End With

      Do While Not rsD.EOF          访问某一字段     Xxx=rsT.Fields(strfldMD).Value Loop

      DAO

    Dim dbTmp As DAO.Database, rsD As DAO.RecordsetSet dbTmp = OpenDatabase(strFileName, False, True, "Excel 8.0;HDR=Yes;IMEX=1;")Set rsD = dbTmp.OpenRecordset(“Select * from [Sheet1$];”)

    Do While Not rsD.EOF         ‘访问某一个字段的值         Xxx= rstD.fields(“Project Name”)  or xxx=rstD.fields(2) Loop  


    最新回复(0)