2010-11-02 68 views
1

我對Access數據庫(存儲更多60.000記錄),一張桌子,我必須在其他Access數據庫B.將數據從MS Access數據庫導入到另一個數據庫而不更改訪問數據庫的最佳方式是什麼?

對Access數據庫B存儲從表中獲取導入到表在開始的時候,表3000個記錄數據庫A.

訪問數據庫A由另一個程序處理,如果我不需要對其進行更改,我最好打開它並獲取數據。我需要在Access數據庫中的表導入更新的記錄和新的記錄訪問數據庫B.

  • 每天數據庫中的有更多的100個新紀錄,有些更新記錄,我需要將它們導入到數據庫中的B一天結束。我需要自動執行。

  • 新記錄很容易找到,但如何更新記錄?隨着更新記錄,我需要添加爲數據庫B新行,而不是在數據庫B.

更改任何行是否有更好的方式來做到這一點,可能使用ODBC連接或別的東西嗎?

請幫幫我! 感謝和問候。

回答

2

Have a look at this microsoft web page

的基本步驟是

  1. 確認兩個表中的字段和數據類型是兼容例如數據在田間是可轉移的;
  2. 從數據庫A附加數據庫B中的遠程表;
  3. 運行追加查詢來傳輸記錄;
  4. 確認它可以工作,例如沒有錯誤信息,正確數量的記錄等
+0

感謝您的回答。對不起,但我無法打開你的鏈接。 – Ekin 2010-11-02 10:18:16

+0

對不起,我修復了這個鏈接。你現在應該可以閱讀它了。 – heferav 2010-11-02 10:28:11

0
  1. 在數據庫B,右鍵點擊 - >鏈接表
  2. 選擇您的數據庫。
  3. 選擇您感興趣的表格。

您的數據庫B現在是在數據庫A.「鏈接」的數據現在你可以使用一個「聯合」查詢或任何你喜歡將它與存儲在數據庫B表中的數據結合起來。最重要的是,這不需要對數據庫A進行任何更改。

0

要更新現有記錄,需要比較兩個記錄,並在目標數據庫中進行更新(如果它們不相同)。

根據涉及的字段數量,這可能很複雜。

這裏的代碼中,我用過去那種目的:

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 

您可以通過這個函數的兩個表名,或兩個查詢的名稱。這允許很大的靈活性。它假定字段名稱在它傳遞的兩個對象中都是相同的,並且如果它們不是相同的名稱,則可以創建一個查詢來替換字段以匹配另一個表中的字段。

這是我使用bazillion次的代碼的一個變體。基本原理是它執行一系列UPDATE查詢,逐行遍歷表並根據哪些行具有不同的值進行更新。

相關問題