简体   繁体   English

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

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

I'm trying to use the method that was described in one of the other threads that I saw posted on stackoverflow here . 我正在尝试使用在此处我在stackoverflow上看到的其他线程之一中描述的方法。

When using the method that was described in that thread (that got the green check) I'm getting an error when running the code. 当使用该线程中描述的方法(带有绿色标记)时,运行代码时出现错误。 The error pop-ups up a blank message box with no contents. 该错误会弹出一个没有内容的空白消息框。

A couple of things to mention: 有两件事要提到:

(1) I've made sure to select and activate the Microsoft Access 14.0 Object Library in Excel. (1)我已经确保选择并激活Excel中的Microsoft Access 14.0对象库。

(2) I am running the sub procedure from my database worksheet in Excel. (2)我正在从Excel中的数据库工作表中运行子过程。

(3) I am then running the AccImport procedure within my code procedure from my wizard worksheet in Excel (separate worksheet). (3)然后,我从Excel的向导工作表(单独的工作表)中的代码过程中运行AccImport过程。


EXCEL SPREADSHEET SETUP 出色的电子表格设置

I can't use screenshots as of yet as I am new to the community but the database worksheet field range is setup as follows. 由于我是社区的新手,到目前为止我还无法使用屏幕截图,但是数据库工作表字段范围的设置如下。

B1 (Occurrence Date), C1 (Machine), D2 (Cell), E2 (Status), F2 (Issue), G2(Preventative/Corrective), H2 (Assigned To) B1(发生日期),C1(机器),D2(单元),E2(状态),F2(问题),G2(预防/纠正),H2(指派给)

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


ACCESS DATABASE TABLE IS SETUP AS FOLLOWS: 访问数据库表的设置如下:

Table Name: MaintenanceDatabase 表名称:MaintenanceDatabase

ID, Occurrence Date, Machine, Cell, Status, Issue, Preventative/Corrective Assigned To ID,发生日期,机器,单元格,状态,问题,分配给的预防/纠正措施

Here is the code that I am running from the Database worksheet in Excel: 这是我从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

Code Snippet from other Worksheet Object that Runs AccImport: 来自运行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

Hopefully someone could shed some light into why I'm getting an error. 希望有人可以阐明为什么我出错了。

Thanks in advance for any help. 在此先感谢您的帮助。

Cheers, 干杯,

Brad 布拉德

I've never tried writing from excel to access in the way you've mentioned. 我从未尝试按照您提到的方式从excel编写访问权限。 Below is my preferred method. 下面是我的首选方法。 You'll need to use the Microsoft DAO object library but using a DAO object you can do updates, inserts, pulls, pretty much whatever you need to accomplish. 您将需要使用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