将一组自定义对象导出到Access表中

时间:2013-04-05 15:02:57

标签: excel ms-access excel-vba vba

我在excel中有一个时间表系统,每个(100+)员工有3行(标准时间,加班时间,双倍时间),网站上每个成本代码有一列。这最终成为一个巨大的矩阵,其中大部分都是空的。我的解决方案是基本上创建一个员工数据类型,用于存储单个成本代码的员工信息和小时数。

   Public Type Employee
     Name As String
     Trade(1 To 3) As String
     EmpNum As Long
     Comment As String
     AddOns(1 To 3) As Single
     Allowance(1 To 3) As Single
     Contract As Long
     CostCode As Long
     STHours As Single
     OTHours As Single
     DTHours As Single
     WorkDate As Date
   End Type

我可以处理电子表格并将excel中的信息组织为一组员工类型对象,但我不熟悉如何将其导出到Access中,并且大多数问题都与从Excel单元格导出到Access有关。我显然可以将这些对象放在另一个工作表的单元格中并以这种方式执行,但似乎应该有更好的方法。

目前我最好的猜测是这样的:

Insert data form Excel to Access 2010 using VBA

然后我会为每次导出对表格进行100多次更新。

是否有一种有效的方法在VBA中创建表对象,使用数组信息填充它,然后在单个更新中将其附加到Access表中的末尾?

感谢。

-Sean

2 个答案:

答案 0 :(得分:0)

最简单的方法是在Access中创建表链接。表链接看起来像Access的其余部分中的表,但数据存储在外部。数据可能位于另一个Access数据库内,也可能位于SQL Server数据库内,或者您拥有的内容。

特别是,数据可以在Excel电子表格中。在Excel中定义一个表,其中包含适合Access应用程序格式的数据。然后在Access中构建一个表链接,链接回您在Excel中定义的表。

更新Excel表格时,更新的结果将在您下次引用Access中的表格链接时自动显示。

答案 1 :(得分:0)

感谢大家的帮助......我只想分享我想出的解决方案。我最终构建了一个函数,将一个对象插入数据库......从interwebs复制和修改。下面的代码,干杯!

Public Function InsertTimeRecord(EmpData As Employee) As Boolean

Dim SaveTime As Date
Dim db As DAO.Database
Dim rs As DAO.Recordset

'//Database Location
Const DB_LOCATION = "C:\access\KMP Tracker.mdb"

'//If errors occur the function will exit with a return value of false (insertion failed)
On Error GoTo ErrHandler:

'//Table has a datecreated/datemodified timestamp for each record
SaveTime = Now

'//Open Database
If db Is Nothing Then
   Set db = DAO.Workspaces(0).OpenDatabase("C:\access\KMP Tracker.mdb") 'Removed DB_LOCATION
End If

'//Open Table
If rs Is Nothing Then
    Set rs = db.OpenRecordset("Timesheet Data", dbOpenDynaset)
End If

'//Create a new record
With rs
    .AddNew
    ![EmpName] = EmpData.Name
    ![Trade1] = EmpData.Trade(1)
    ![Trade2] = EmpData.Trade(2)
    ![Trade3] = EmpData.Trade(3)
    ![EmpNum] = EmpData.EmpNum
    ![Comment] = EmpData.Comment
    ![AddOns1] = EmpData.AddOns(1)
    ![AddOns2] = EmpData.AddOns(2)
    ![AddOns3] = EmpData.AddOns(3)
    ![Allowance1] = EmpData.Allowance(1)
    ![Allowance2] = EmpData.Allowance(2)
    ![Allowance3] = EmpData.Allowance(3)
    ![Contract] = EmpData.Contract
    ![CostCode] = EmpData.CostCode
    ![STHours] = EmpData.STHours
    ![OTHours] = EmpData.OTHours
    ![DTHours] = EmpData.DTHours
    ![WorkDate] = EmpData.WorkDate
    ![DateSubmitted] = SaveTime

    '//Insert Record into Database
    .Update
    InsertMachineHoursRecord = True '//SUCCESSFUL INSERTION
End With

'//Note that we use recordset in this example, but equally effective
'//  is to create an update query command text and simply run the update query:
'//  (INSERT INTO Table (Field1, Field2) VALUES (Value1, Value2);

'//Make sure we have closed the database
My_Exit:
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
Exit Function

ErrHandler:
MsgBox Err.Description
Resume My_Exit
End Function