VBA+ADO查询ACCESS数据库

Sub  调用ACCESS数据库查询()
  
    Dim rst As New ADODB.Recordset
    Dim myPath As String
    Dim SQL As String
    Dim i As Integer
    myPath = ThisWorkbook.Path & "\新建 Microsoft Access 数据库 (4G-2G).accdb"
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & myPath  ‘’调用ACCESS数据库

    SQL = "SELECT mecontext,optionalfeatUrelicense,featurestate,licensestate FROM [OPT] where [OptionalFeatureLicense] like '%mul%'"       ‘’‘’查询数据库
   
    rst.Open SQL, cnn, adOpenKeyset, adLockOptimistic
    
    Cells.ClearContents  ‘’‘清空表格
    For i = 0 To rst.Fields.Count - 1     ’‘’‘’‘’建立表头
    Cells(1, i + 1) = rst.Fields(i).Name
   Next

    Range("A2").CopyFromRecordset rst
    rst.Close
    cnn.Close
    Set rst = Nothing
    
End Sub

你可能感兴趣的:(VBA)