VBA: DoCmd Transferspreadsheet

心不动则不痛 提交于 2021-01-29 10:41:14

问题


Im trying to import an excel worksheet into Access table. Here is my code:

Sub test2()

 Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False

Dim fd As Object
Set fd = xlApp.Application.FileDialog(msoFileDialogFilePicker)

Dim selectedItem As Variant

If fd.Show = -1 Then
    For Each selectedItem In fd.SelectedItems
        Debug.Print selectedItem
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "POR", selectedItem, True, "POR Plan!A1:Z100"
    Next
End If

Set fd = Nothing
    xlApp.Quit
Set xlApp = Nothing
End Sub

I get an error at the DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "POR", selectedItem, True, "POR Plan!A1:Z100" line. When I dont specify the range and omit "POR Plan!A1:Z100", it takes the very first worksheet in my excel workbook and works fine. But I would like to get the POR Plan worksheet.


回答1:


You can import from Excel into Access (this runs in Access).

Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean

' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = False

' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents\"

' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"

strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
      strPathFile = strPath & strFile
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
            strTable, strPathFile, blnHasFieldNames

' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
'       Kill strPathFile

      strFile = Dir()
Loop

Or, push data from Excel to Access (this runs in 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

There are several other variations of these two sample scripts. Do some Googling and you will get all kinds of ideas for all kinds of things.



来源:https://stackoverflow.com/questions/50168626/vba-docmd-transferspreadsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!