Excel 作为对象 将遥感器作为对象 将文件设置为字符串 作为字符串的Dim strTable 将SQL设置为字符串 作为整数的Dim i '禁用屏幕闪烁。 Application.ScreenUpdating=False '指定accdb文件的文件路径。您还可以使用文件的完整路径,如: 'AccessFile=“C:\Users\Christos\Desktop\Sample.accdb” AccessFile=ThisWorkbook.Path&“\”和“Sample.accdb” '设置要检索数据的表的名称。 strTable=“客户” 出错时继续下一步 '创建ADODB连接对象。 Set con=CreateObject(“ADODB.connection”) '检查对象是否已创建。 如果错误号为0,则 MsgBox“未创建连接!”,vbCritical,“连接错误” 出口接头 如果结束 错误转到0 '打开连接。 con.Open“Provider=Microsoft.ACE.OLEDB.12.0;数据源=”&AccessFile '创建SQL语句以从表中检索数据。 '获取所有加拿大客户的必要信息(名字等)。 SQL=“从“&strTable&”WHERE COUNTRY='Canada'中选择名字、姓氏、地址、城市、电话” 出错时继续下一步 '创建ADODB记录集对象。 Set rs=CreateObject(“ADODB.Recordset”) '检查对象是否已创建。 如果错误号为0,则 “错!释放对象并退出。 设置rs=无 设置con=Nothing '向用户显示错误消息。 MsgBox“未创建记录集!”,vbCritical,“记录集错误” 出口接头 如果结束 错误转到0 '设置光标位置。 rs.CursorLocation=早期绑定时的3'adUseClient rs.CursorType=1'早期绑定上的adOpenKeyset '打开记录集。 开放式SQL,con '检查记录集是否为空。 如果是rs.EOF和rs.BOF,则 '关闭记录集和连接。 rs.Close 结案 '释放对象。 设置rs=无 设置con=Nothing '启用屏幕。 Application.ScreenUpdating=True '如果记录集为空,则显示错误。 MsgBox“记录集中没有记录!”,vbCritical,“没有记录” 出口接头 如果结束 '复制记录集标题。 对于i=0到rs.Fields.Count-1 表格(“新查询”)。单元格(1,i+1)=rs.字段(i).名称 接下来我 '在工作表中写入查询值。 表格(“新查询”).Range(“A2”).CopyFromRecordset rs '关闭记录集和连接。 rs.Close 结案 '释放对象。 设置rs=无 设置con=Nothing “调整列”宽度。 表格(“新查询”)。列(“A:E”)。自动拟合 '启用屏幕。 Application.ScreenUpdating=True '通知用户宏已成功执行。 MsgBox“加拿大客户已成功从“&strTable&”表中检索到”,vbInformation,“完成” 端接头

Excel 作为对象 将遥感器作为对象 将文件设置为字符串 作为字符串的Dim strTable 将SQL设置为字符串 作为整数的Dim i '禁用屏幕闪烁。 Application.ScreenUpdating=False '指定accdb文件的文件路径。您还可以使用文件的完整路径,如: 'AccessFile=“C:\Users\Christos\Desktop\Sample.accdb” AccessFile=ThisWorkbook.Path&“\”和“Sample.accdb” '设置要检索数据的表的名称。 strTable=“客户” 出错时继续下一步 '创建ADODB连接对象。 Set con=CreateObject(“ADODB.connection”) '检查对象是否已创建。 如果错误号为0,则 MsgBox“未创建连接!”,vbCritical,“连接错误” 出口接头 如果结束 错误转到0 '打开连接。 con.Open“Provider=Microsoft.ACE.OLEDB.12.0;数据源=”&AccessFile '创建SQL语句以从表中检索数据。 '获取所有加拿大客户的必要信息(名字等)。 SQL=“从“&strTable&”WHERE COUNTRY='Canada'中选择名字、姓氏、地址、城市、电话” 出错时继续下一步 '创建ADODB记录集对象。 Set rs=CreateObject(“ADODB.Recordset”) '检查对象是否已创建。 如果错误号为0,则 “错!释放对象并退出。 设置rs=无 设置con=Nothing '向用户显示错误消息。 MsgBox“未创建记录集!”,vbCritical,“记录集错误” 出口接头 如果结束 错误转到0 '设置光标位置。 rs.CursorLocation=早期绑定时的3'adUseClient rs.CursorType=1'早期绑定上的adOpenKeyset '打开记录集。 开放式SQL,con '检查记录集是否为空。 如果是rs.EOF和rs.BOF,则 '关闭记录集和连接。 rs.Close 结案 '释放对象。 设置rs=无 设置con=Nothing '启用屏幕。 Application.ScreenUpdating=True '如果记录集为空,则显示错误。 MsgBox“记录集中没有记录!”,vbCritical,“没有记录” 出口接头 如果结束 '复制记录集标题。 对于i=0到rs.Fields.Count-1 表格(“新查询”)。单元格(1,i+1)=rs.字段(i).名称 接下来我 '在工作表中写入查询值。 表格(“新查询”).Range(“A2”).CopyFromRecordset rs '关闭记录集和连接。 rs.Close 结案 '释放对象。 设置rs=无 设置con=Nothing “调整列”宽度。 表格(“新查询”)。列(“A:E”)。自动拟合 '启用屏幕。 Application.ScreenUpdating=True '通知用户宏已成功执行。 MsgBox“加拿大客户已成功从“&strTable&”表中检索到”,vbInformation,“完成” 端接头,excel,ms-access,vba,Excel,Ms Access,Vba,你会在这里找到更多信息 我的建议是开始这样做。VBA在Access中的表现与Excel一样出色。开始编写代码,然后带着问题回到这里。这是一个非常开放的过程,看起来你是在要求别人为你写代码。如果你问一些具体的问题,并表明你已经付出了努力,你会得到更多的反馈。你尝试过谷歌吗?我的建议是开始这样做。VBA在Access中的表现与Excel一样出色。开始编写代码,然后带着问题回到这里。这是一个非常开放的过程,看起来你是在要求别人为你写代码。如果你问一些具体的问题,并表明你已经付出了努力,你会得到更多的反

你会在这里找到更多信息


我的建议是开始这样做。VBA在Access中的表现与Excel一样出色。开始编写代码,然后带着问题回到这里。这是一个非常开放的过程,看起来你是在要求别人为你写代码。如果你问一些具体的问题,并表明你已经付出了努力,你会得到更多的反馈。你尝试过谷歌吗?我的建议是开始这样做。VBA在Access中的表现与Excel一样出色。开始编写代码,然后带着问题回到这里。这是一个非常开放的过程,看起来你是在要求别人为你写代码。如果你问一些具体的问题,并表明你已经付出了努力,你会得到更多的反馈。你试过谷歌吗?
Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
    ' connect to the Access database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
        "Data Source=C:\FolderName\DataBaseName.mdb;"
    ' open a recordset
    Set rs = New ADODB.Recordset
    rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable  
    ' all records in a table
    r = 3 ' the start row in the worksheet
    Do While Len(Range("A" & r).Formula) > 0 
    ' repeat until first empty cell in column A
        With rs
            .AddNew ' create a new record
            ' add values to each field in the record
            .Fields("FieldName1") = Range("A" & r).Value
            .Fields("FieldName2") = Range("B" & r).Value
            .Fields("FieldNameN") = Range("C" & r).Value
            ' add more fields if necessary...
            .Update ' stores the new record
        End With
        r = r + 1 ' next row
    Loop
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub
Sub ADOImportFromAccessTable(DBFullName As String, _
    TableName As String, TargetRange As Range)
' Example: ADOImportFromAccessTable "C:\FolderName\DataBaseName.mdb", _
    "TableName", Range("C1")
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
    Set TargetRange = TargetRange.Cells(1, 1)
    ' open the database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
        DBFullName & ";"
    Set rs = New ADODB.Recordset
    With rs
        ' open the recordset
        .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable 
        ' all records
        '.Open "SELECT * FROM " & TableName & _
            " WHERE [FieldName] = 'MyCriteria'", cn, , , adCmdText 
        ' filter records

        RS2WS rs, TargetRange ' write data from the recordset to the worksheet

'        ' optional approach for Excel 2000 or later (RS2WS is not necessary)
'        For intColIndex = 0 To rs.Fields.Count - 1 ' the field names
'            TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
'        Next
'        TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data

    End With
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub
Sub CreateAndRunQuery()

    '------------------------------------------------------------------------------------------
    'This macro opens the Sample.accdb database, creates and runs an SQL query (filtering
    'all the customers from Canada). Then, it copies selected fields back in the Excel sheet.
    'The code uses late binding, so no reference to external library is required.

    'Written By:    Christos Samaras
    'Date:          05/10/2013
    'Last Updated:  29/11/2014
    'E-mail:        xristos.samaras@gmail.com
    'Site:          http://www.myengineeringworld.net
    '------------------------------------------------------------------------------------------

    'Declaring the necessary variables.
    Dim con         As Object
    Dim rs          As Object
    Dim AccessFile  As String
    Dim strTable    As String
    Dim SQL         As String
    Dim i           As Integer

    'Disable screen flickering.
    Application.ScreenUpdating = False

    'Specify the file path of the accdb file. You can also use the full path of the file like:
    'AccessFile = "C:\Users\Christos\Desktop\Sample.accdb"
    AccessFile = ThisWorkbook.Path & "\" & "Sample.accdb"

    'Set the name of the table you want to retrieve the data.
    strTable = "Customers"

    On Error Resume Next
    'Create the ADODB connection object.
    Set con = CreateObject("ADODB.connection")
    'Check if the object was created.
    If Err.Number <> 0 Then
        MsgBox "Connection was not created!", vbCritical, "Connection Error"
        Exit Sub
    End If
    On Error GoTo 0

    'Open the connection.
    con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccessFile

    'Create the SQL statement to retrieve the data from table.
    'Get the necessary information (first name etc.) for all the Canadian customers.
    SQL = "SELECT FirstName, LastName, Address, City, Phone FROM " & strTable & " WHERE COUNTRY='Canada'"

    On Error Resume Next
    'Create the ADODB recordset object.
    Set rs = CreateObject("ADODB.Recordset")
    'Check if the object was created.
    If Err.Number <> 0 Then
        'Error! Release the objects and exit.
        Set rs = Nothing
        Set con = Nothing
        'Display an error message to the user.
        MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
        Exit Sub
    End If
    On Error GoTo 0

    'Set thee cursor location.
    rs.CursorLocation = 3 'adUseClient on early  binding
    rs.CursorType = 1 'adOpenKeyset on early  binding

    'Open the recordset.
    rs.Open SQL, con

    'Check if the recordet is empty.
    If rs.EOF And rs.BOF Then
        'Close the recordet and the connection.
        rs.Close
        con.Close
        'Release the objects.
        Set rs = Nothing
        Set con = Nothing
        'Enable the screen.
        Application.ScreenUpdating = True
        'In case of an empty recordset display an error.
        MsgBox "There are no records in the recordset!", vbCritical, "No Records"
        Exit Sub
    End If

    'Copy the recordset headers.
    For i = 0 To rs.Fields.Count - 1
        Sheets("New Query").Cells(1, i + 1) = rs.Fields(i).Name
    Next i

    'Write the query values in the sheet.
    Sheets("New Query").Range("A2").CopyFromRecordset rs

    'Close the recordet and the connection.
    rs.Close
    con.Close

    'Release the objects.
    Set rs = Nothing
    Set con = Nothing

    'Adjust the columns' width.
    Sheets("New Query").Columns("A:E").AutoFit

    'Enable the screen.
    Application.ScreenUpdating = True

    'Inform the user that the macro was executed successfully.
    MsgBox "The Canadian customers were successfully retrieved from the '" & strTable & "' table!", vbInformation, "Done"

End Sub