Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 特定Excel电子表格的访问表_Vba_Excel_Ms Access 2010 - Fatal编程技术网

Vba 特定Excel电子表格的访问表

Vba 特定Excel电子表格的访问表,vba,excel,ms-access-2010,Vba,Excel,Ms Access 2010,我在使用excel vba将Access表中的记录复制到excel工作表时遇到问题 文档打开表格后,我迷路了 有人能帮忙吗 谢谢 这是我的密码: Sub OpenAccessDB() Dim DBFullPath As String Dim DBFullName As String Dim TableName As String Dim TargetRange As Range Dim appAccess As Object Dim RS As N

我在使用excel vba将Access表中的记录复制到excel工作表时遇到问题

文档打开表格后,我迷路了

有人能帮忙吗

谢谢

这是我的密码:

Sub OpenAccessDB()
    Dim DBFullPath As String
    Dim DBFullName As String
    Dim TableName As String
    Dim TargetRange As Range
    Dim appAccess As Object
    Dim RS As New ADODB.Recordset

    'File Paths and Names*********************************
    DBFullPath = "e:\ccampbellStuff\"
    DBFullName = "2015_02.accdb"
    TableName = "Record Opt Outs"

    'Initiating the Access DB Engine**********************
    Set appAccess = CreateObject("Access.Application")

    'Opening the database
    appAccess.OpenCurrentDatabase (DBFullPath & DBFullName)
    appAccess.Visible = True

    'Open Access Table Called Record Opt Outs****
    **appAccess.DoCmd.Opentable (TableName)**
    'Set RS = appAccess.DoCmd.Opentable (TableName) this didnt work either
    'Set appAccess = Nothing
    'Copy Access Records and Patse to Excel''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Close database
    appAccess.Quit
End Sub

我不想麻烦自动访问本身-只需使用ADO:

Sub loadAccessData()
'////////////////////////////////////////////////////////////////////
' requires a reference to a Microsoft ActiveX Data Objects Library.
'////////////////////////////////////////////////////////////////////
   Dim cn               As ADODB.Connection
   Dim sQuery           As String
   Dim rs               As ADODB.Recordset
   Dim sDB_Path         As String
   Dim ws               As Worksheet

'    output to activesheet
   Set ws = ActiveSheet

'    Path to database
   sDB_Path = "c:\somepath\database1.accdb"

   Set cn = New ADODB.Connection
'    open connection to database
   With cn
      .CursorLocation = adUseServer
      .Provider = "Microsoft.ACE.OLEDB.12.0"
      .ConnectionString = "Data Source=" & sDB_Path & ";"
      .Open
   End With

'    SQL query string - change to suit
   sQuery = "SELECT * FROM tblTest"

'    Create New Recordset
      Set rs = New ADODB.Recordset

      ' open recordset using query string and connection
      With rs
         .Open sQuery, cn, adOpenStatic, adLockPessimistic, adCmdText
         ' check for records returned
         If Not .EOF Then
            'Populate field names
            For i = 1 To .Fields.Count
               ws.Cells(1, i) = .Fields(i - 1).Name
            Next i
            ' Load data starting at A2
            ws.Cells(2, 1).CopyFromRecordset rs
         End If
         .Close
      End With

      ' clean up
      cn.Close
End Sub