在不更改 Access 数据库的情况下将数据从 MS Access 数据库导入到另一个数据库的最佳方法是什么?

发布于 2024-09-30 02:27:52 字数 450 浏览 0 评论 0原文

我在 Access 数据库 A 上有一个表(存储了 60.000 多条记录),我必须将其导入到另一个 Access 数据库 B 上的表中。

一开始,Access 数据库 B 上的表存储 Access 数据库 A 上表中的 3000 条记录

。 A 由另一个程序处理,如果我不必对其进行更改,那就最好了,我只需打开它并获取数据即可。我需要从access数据库A的表中导入更新记录和新记录来访问数据库B。

  • 数据库A每天都会有100多条新记录和一些更新记录,我需要在最后将它们导入到数据库B中天。我需要自动执行。

  • 新记录很容易找到,但是更新记录怎么样?通过更新记录,我需要在数据库 B 中添加新行,而不是更改数据库 B 中的任何行。

是否有更好的方法来执行此操作,也许使用 ODBC 连接或其他方式?

请帮我 ! 谢谢并致以诚挚的问候。

I have a table on access database A (store more 60.000 records), which I must import into a table on another Access database B.

In the beginning, table on Access database B stores 3000 records from table on Access database A.

The access database A is handled by another program, and it would be best if I did not have to makes changes to it, I just can open it and get data. I need import updated records and new records from table on the access database A to access database B.

  • Every day database A have more 100 new records and some update records, I need to import them into database B in the end of the day. I need do automatically.

  • The new records is easy to find but how about update records ? With update record I need to add as a new row in database B, not change any rows in database B.

Is there a better way to do this, perhaps using an ODBC connection or something else?

Please help me !
Thanks and best regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪 2024-10-07 02:27:52

有看一下这个微软网页

基本步骤是

  1. 确认两个表中的字段和数据类型兼容,例如字段中的数据是可转移的;
  2. 从数据库 A 附加数据库 B 中的远程表;
  3. 运行追加查询来传输记录;
  4. 确认它有效,例如没有错误消息、正确的记录数等。

Have a look at this microsoft web page

The basic steps are

  1. Confirm that the fields and data types in the two tables are compatible e.g. data in fields are transferable;
  2. Attach remote table in Database B from Database A;
  3. Run an append query to transfer the records;
  4. Confirm that it worked e.g. no error messages, correct number of records etc.
花之痕靓丽 2024-10-07 02:27:52
  1. 在数据库B中,右键->链接表
  2. 选择您的数据库。
  3. 选择您感兴趣的桌子。

您的数据库 B 现在“链接”到数据库 A 中的数据。现在您可以使用“联合”查询或任何您喜欢的查询将其与数据库 B 表中存储的数据组合起来。最重要的是,这不需要对数据库 A 进行任何更改。

  1. In database B, Right click->Link tables.
  2. Choose your database.
  3. Choose your table(s) of interest.

Your database B is now "linked" to the data in database A. Now you can use a "union" query or whatever you like to combine it with data stored in your database B table. Best of all, this doesn't require any changes to database A.

带上头具痛哭 2024-10-07 02:27:52

要更新现有记录,您需要比较两条记录,并在目标数据库中的两条记录不相同时进行更新。

根据涉及的字段数量,这可能会很复杂。

以下是我过去用于此目的的代码:

  Public Function UpdateTableData(ByVal strSourceTable As String, _
      ByVal strTargetTable As String, ByVal strJoinField As String, _
      ByRef db As DAO.Database, Optional ByVal strExcludeFieldsList As String, _
      Optional strAdditionalCriteria As String) As Boolean
    Dim strUpdate As String
    Dim rsFields As DAO.Recordset
    Dim fld As DAO.Field
    Dim strFieldName As String
    Dim strNZValue As String
    Dim strSet As String
    Dim strWhere As String

    strUpdate = "UPDATE " & strTargetTable & " INNER JOIN " & strSourceTable & " ON " & strTargetTable & "." & strJoinField & " = " & strSourceTable & "." & strJoinField
    ' if the fields don't have the same names in both tables,
    '   create a query that aliases the fields to have the names of the
    '   target table
    ' if the source table is in a different database and you don't
    '   want to create a linked table, create a query and specify
    '   the external database as the source of the table
    ' alternatively, for strTargetTable, supply a SQL string with
    '   the external connect string
    Set rsFields = db.OpenRecordset(strSourceTable)
    For Each fld In rsFields.Fields
      strFieldName = fld.Name
      If strFieldName <> strJoinField Or (InStr(", " & strExcludeFieldsList & ",", strFieldName & ",") <> 0) Then
         Select Case fld.Type
           Case dbText, dbMemo
             strNZValue = "''"
           Case Else
             strNZValue = "0"
         End Select
         strSet = " SET " & strTargetTable & "." & strFieldName & " = varZLSToNull(" & strSourceTable & "." & strFieldName & ")"
         strSet = strSet & ", " & strTargetTable & ".Updated = #" & Date & "#"
         strWhere = " WHERE Nz(" & strTargetTable & "." & strFieldName & ", " & strNZValue & ") <> Nz(" & strSourceTable & "." & strFieldName & ", " & strNZValue & ")"
         If db.TableDefs(strTargetTable).Fields(fld.Name).Required Then
            strWhere = strWhere & " AND " & strSourceTable & "." & strFieldName & " Is Not Null"
         End If
         If Len(strAdditionalCriteria) > 0 Then
            strWhere = strWhere & " AND " & strAdditionalCriteria
         End If
         Debug.Print strUpdate & strSet & strWhere
         Debug.Print SQLRun(strUpdate & strSet & strWhere, dbLocal) & " " & strFieldName & " updated."
      End If
    Next fld
    rsFields.Close
    Set rsFields = Nothing
    UpdateTableData = True
  End Function

您可以向此函数传递两个表名称或两个查询名称。这提供了很大的灵活性。它假定传递的两个对象中的字段名称相同,如果它们不是相同的名称,您可以创建一个查询来为字段添加别名,以与其他表中的字段相匹配。

这是我使用过无数次的代码的变体。基本原理是,它执行一系列 UPDATE 查询,逐列遍历表,并根据哪些行具有不同的值进行更新。

To update existing records, you need to compare the two records and update when they are not the same in the target database.

Depending on the number of fields involved, this can be complicated.

Here's code I've used for that purpose in the past:

  Public Function UpdateTableData(ByVal strSourceTable As String, _
      ByVal strTargetTable As String, ByVal strJoinField As String, _
      ByRef db As DAO.Database, Optional ByVal strExcludeFieldsList As String, _
      Optional strAdditionalCriteria As String) As Boolean
    Dim strUpdate As String
    Dim rsFields As DAO.Recordset
    Dim fld As DAO.Field
    Dim strFieldName As String
    Dim strNZValue As String
    Dim strSet As String
    Dim strWhere As String

    strUpdate = "UPDATE " & strTargetTable & " INNER JOIN " & strSourceTable & " ON " & strTargetTable & "." & strJoinField & " = " & strSourceTable & "." & strJoinField
    ' if the fields don't have the same names in both tables,
    '   create a query that aliases the fields to have the names of the
    '   target table
    ' if the source table is in a different database and you don't
    '   want to create a linked table, create a query and specify
    '   the external database as the source of the table
    ' alternatively, for strTargetTable, supply a SQL string with
    '   the external connect string
    Set rsFields = db.OpenRecordset(strSourceTable)
    For Each fld In rsFields.Fields
      strFieldName = fld.Name
      If strFieldName <> strJoinField Or (InStr(", " & strExcludeFieldsList & ",", strFieldName & ",") <> 0) Then
         Select Case fld.Type
           Case dbText, dbMemo
             strNZValue = "''"
           Case Else
             strNZValue = "0"
         End Select
         strSet = " SET " & strTargetTable & "." & strFieldName & " = varZLSToNull(" & strSourceTable & "." & strFieldName & ")"
         strSet = strSet & ", " & strTargetTable & ".Updated = #" & Date & "#"
         strWhere = " WHERE Nz(" & strTargetTable & "." & strFieldName & ", " & strNZValue & ") <> Nz(" & strSourceTable & "." & strFieldName & ", " & strNZValue & ")"
         If db.TableDefs(strTargetTable).Fields(fld.Name).Required Then
            strWhere = strWhere & " AND " & strSourceTable & "." & strFieldName & " Is Not Null"
         End If
         If Len(strAdditionalCriteria) > 0 Then
            strWhere = strWhere & " AND " & strAdditionalCriteria
         End If
         Debug.Print strUpdate & strSet & strWhere
         Debug.Print SQLRun(strUpdate & strSet & strWhere, dbLocal) & " " & strFieldName & " updated."
      End If
    Next fld
    rsFields.Close
    Set rsFields = Nothing
    UpdateTableData = True
  End Function

You can pass this function two table names, or two query names. This allows lots of flexibility. It assumes the field names are the same in both the objects it is passed, and if they aren't the same name, you can create a query to alias the fields to match those in the other table.

This is a variant of code I've used a bazillion times. The basic principle is that it executes a series of UPDATE queries that go column-by-column through your table and update based on which rows have differing values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文