繁体   English   中英

Excel VBA将数据导出到MS Access表-扩展

[英]Excel VBA to Export Data to MS Access Table - Extended

我正在尝试使用在此处我在stackoverflow上看到的其他线程之一中描述的方法。

当使用该线程中描述的方法(带有绿色标记)时,运行代码时出现错误。 该错误会弹出一个没有内容的空白消息框。

有两件事要提到:

(1)我已经确保选择并激活Excel中的Microsoft Access 14.0对象库。

(2)我正在从Excel中的数据库工作表中运行子过程。

(3)然后,我从Excel的向导工作表(单独的工作表)中的代码过程中运行AccImport过程。


出色的电子表格设置

由于我是社区的新手,到目前为止我还无法使用屏幕截图,但是数据库工作表字段范围的设置如下。

B1(发生日期),C1(机器),D2(单元),E2(状态),F2(问题),G2(预防/纠正),H2(指派给)

B2(2015年4月15日),C2(机器1),D2(单元格1),E2(0),F2(测试),G2(纠正),H2(名称示例1)


访问数据库表的设置如下:

表名称:MaintenanceDatabase

ID,发生日期,机器,单元格,状态,问题,分配给的预防/纠正措施

这是我从Excel中的数据库工作表运行的代码:

Sub AccImport()

    Dim acc As New Access.Application
    acc.OpenCurrentDatabase "C:\Users\brad.edgar\Desktop\DASHBOARDS\MAINTENANCE\MaintenanceDatbase.accdb"
    acc.DoCmd.TransferSpreadsheet _
        TransferType:=acImport, _
        SpreadsheetType:=acSpreadsheetTypeExcel12Xml, _
        TableName:="MaintenanceDatabase", _
        Filename:=Application.ActiveWorkbook.FullName, _
        HasFieldNames:=True, _
        Range:="Database$B1:H2"
    acc.CloseCurrentDatabase
    acc.Quit
    Set acc = Nothing

End Sub

来自运行AccImport的其他工作表对象的代码片段:

Public Sub DeleteSelectedRecord()
    Dim CurrentSelectedIndex    As Integer

    ' Assign the currently selected index to CurrentSelectedIndex
    CurrentSelectedIndex = [Database.CurrentIndex]

    ' Move the ListBox Selector
    If [Database.CurrentIndex].Value = [Database.RecordCount] Then    
'Last item on the list
        [Database.CurrentIndex].Value = [Database.CurrentIndex].Value - 1
    End If

    'Copy to Access Database

    Database.AccImport

    ' Delete the entry
    Database.ListObjects("Database").ListRows(CurrentSelectedIndex).Delete

End Sub

希望有人可以阐明为什么我出错了。

在此先感谢您的帮助。

干杯,

布拉德

我从未尝试按照您提到的方式从excel编写访问权限。 下面是我的首选方法。 您将需要使用Microsoft DAO对象库,但是使用DAO对象,您可以执行更新,插入,提取等几乎所有您需要完成的工作。

 Sub SaveCustomer_Defaults()

 Dim strSQL As Variant
 Dim accApp As Object
 Dim srcs As Variant
 Dim msg1 As Variant

 Sheets("Lists").Visible = True
 Sheets("Lists").Select
     Range("T6").Select
     x = Range("T500000").End(xlUp).Row

 For i = 6 To x
     Cells(i, 20).Select
     If Environ("USERNAME") = Cells(i, 23).Value Then
      'location of the access db   
      srcs = "C:\\user\desktop\Detail_1.accdb"  ''' Live location '''

    Set accApp = GetObject(srcs, "access.Application")

    'write your sql to pull the table along with the cell values
    strSQL = "Select * from US_CustomID "
    strSQL = strSQL & " where( [AssignedTo] = '" & Sheets("Lists").Cells(i, 21)
    strSQL = strSQL & "' and [Tab] = '" & Sheets("Lists").Cells(i, 24)
    strSQL = strSQL & "' and [RepID] = '" & Sheets("Lists").Cells(i, 23)
    strSQL = strSQL & "');"

    Set db = DAO.OpenDatabase(srcs)
    Set rs = db.OpenRecordset(strSQL)

    On Error Resume Next
    rs.Edit


    rs![Occurrence Date] = Sheets("Lists").Cells(i, 25)
    rs![Machine] = Sheets("Lists").Cells(i, 26)
    rs![Cell] = Sheets("Lists").Cells(i, 27)
    rs![Status] = Sheets("Lists").Cells(i, 28)
    rs![Issue] = Sheets("Lists").Cells(i, 29)
    rs![Preventative/Corrective] = Sheets("Lists").Cells(i, 30)
    rs![Assigned To] = Sheets("Lists").Cells(i, 31)

    rs.Update

    If Not rs Is Nothing Then rs.Close

    Set rs = Nothing
    Set db = Nothing

    accApp.DoCmd.RunSQL strSQL
    accApp.Application.Quit
        End If
    Next i

    Sheets("Lists").Visible = False

    End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM