0

I'm looking for a way to import a specific tab into my Access table. Normally I do a TransferSpreadsheet type of code, so this is rather new to me.

I need the user to be able to open the file from any directory - followed by have the program import a specific spreadsheet that can have a semi-variable name.

The spreadsheet that needs to be imported is always " Details". So obviously, right now the file would contain a tab called July Details. So my logic with this bit of code was to find the spreadsheet with LIKE (or *) Detail. I'm not sure if this works however, and I'm not sure how best to transfer this to my Access Table since I'm so used to TransferSpreadsheet. Any help is appreciated!

Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = ActiveWorkbook

ImportFileName = cmdFileDialog("Select File to Import: ")
Set wb2 = Workbooks.Open(FileName:=ImportFileName)

For Each Sheet In wb2.Sheets
        If Sheet.Name Like Detail Then
            Sheet.Copy After:=wb1.Sheets(wb1.Sheets.Count)
        End If
Next Sheet

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, DataTable, ImportFileName, True, DataRange

EDIT: Let me clarify: Once I find the sheet with Details in it, I am not sure how to import it into an Access table like I need. That is my last problem with this.

4

2 回答 2

0

这是对您问题最后一部分的回应!要将 Excel 中的表上传到 Access 表,您将需要以下内容:

  1. ADODB 库(Microsoft ActiveX 数据对象 2.x)
  2. 数组操作

您将在 ADODB 中使用以下对象和方法:

  1. 联系
  2. 记录集
  3. 添新

下面是一个使用 ADODB 将新记录附加到 Access 表中的通用示例:

(如果您需要添加多条记录,则需要编写一个循环来创建包含值的不同数组并重复调用 AddNew 函数。据我所知,ADODB 不支持批量上传记录*正确如果我在这里错了,我)

Dim objcon As ADODB.Connection
Dim objrec As ADODB.Recordset
Dim strSQL As String
Dim strCon As String

'important strings you'll need
strSQL = "SELECT * FROM " & TABLE_NAME & ";"
strCon = "DSN=MS Access Database;DBQ=" & FULLPATH_OF_DATABASE & ";"

'open your connection to the database
Set objcon = New ADODB.Connection
objcon.Open strCon

'open your recordset from database
Set objrec = New ADODB.Recordset
objrec.Open strSQL, objcon, adOpenKeyset, adLockOptimistic

'adding a new record
objrec.AddNew Array("FIELD_1", "FIELD_2", "FIELD_3"), Array("Value1", "Value2", "Value3")

Set objrec = Nothing
Set objcon = Nothing

让我知道这是否有帮助!

于 2013-07-24T18:23:56.790 回答
-1

看起来我找到了一种结合 DoCmd.TransferSpreadsheet 的方法。谢谢大家。如果这有点难看,请随时纠正或给我一个更合适的方法。

ImportFileName = cmdFileDialog("Select File to import : ")
DoCmd.SetWarnings False

Dim xlApp As Object
Dim xlWrk As Object
Dim xlSheet As Object
Dim db As Database
Dim SheetName As String

Set xlApp = CreateObject("Excel.Application")
Set xlWrk = xlApp.Workbooks.Open(ImportFileName)
Set db = CurrentDb

For Each Sheet In xlWrk.Sheets
    If Sheet.Name Like "*Detail*" Then
        Set xlSheet = xlWrk.Sheets(Sheet.Name)
        SheetName = Sheet.Name
    End If
Next Sheet

DoCmd.TransferSpreadsheet _
    transfertype:=acImport, _
    TableName:="tblImport", _
    FileName:=ImportFileName, _
    HasFieldNames:=True, _
    Range:=SheetName & "!A1:BM" & ls_last_row
于 2013-07-24T20:25:14.380 回答