2016-08-05 51 views
0

我有7套,相關表的訪問後端數據庫:客戶,項目,項目目標,目標的障礙,障礙的原因,可能的解決辦法和需要採取的行動或資源。每個客戶可能有多個項目。每個項目在「目標到行動」中可能有多個記錄。出口嵌套表到新後端

我想一個客戶項目,其嵌套表格導出到具有完全相同的結構的空後端,所以客戶端,與前端和後端供應,可以查看信息並進行更改。如果客戶端進行了更改,我想將更改導回到主數據庫中,替換現有數據。

客戶有一般的個人信息。項目有八個字段,包括與客戶端的鏈接ID。從目標到動作的每個表格都有一個自動編號的主鍵,一個鏈接到上表主鍵的ID字段,一個100個字符的簡短描述,一個長的字段用於註釋,一個用於優先級或加權重要性的數字以及一個是/否字段已解決或已完成。每個可能有1到10個記錄。我正在使用的樣本記錄集中有94條記錄。

,這很容易提取項目,並從與查詢數據庫的相關數據;但我無法提出將其插入空白後端的最有效方法。我在Export按鈕下啓動了一些子例程,以便通過表傳輸數據表,這樣我就可以處理由重新編號爲主鍵引起的已更改的關係;但如果可能的話,我想一舉完成整個過程。我在一個While rsSource.EOF - Wend例程中創建了一個巨大的後端文件,其中一個有缺陷的嘗試。任何建議感激地收到。

********************************代碼************* *********************

Private Sub Command316_Click() 
' SelectedClient and SelectedProject are Public Integer variables 
' To be used in For...Next loops 
Dim i, iNumRecs, intGoal, intImped, intCause, intSolution, intAction As Integer 
Dim SQLstr As String 

'Open source database 
Dim dbSource As Database 
Set dbSource = CurrentDb 

'Open dest database 
Dim dbDestination As Database 
Set dbDestination = DAO.OpenDatabase("C:\Prosolve\Temp\Prosolve_BE.accdb") 

' Select Project to be transferred 
' Might be easier to work with if everything NOT selected at once 

SQLstr = "SELECT Clients.ClientID, Clients.ContactFirstName, Clients.ContactLastName, Clients.Address, Clients.City, Clients.StateOrProvince, Clients.PostalCode, " 
SQLstr = SQLstr + "Clients.Country, Clients.EmailAddress, Clients.CompanyName, Clients.PhoneNumber, Clients.CellNumber, Clients.BillingRate, Clients.TaxPayable, Clients.Discount, " 
SQLstr = SQLstr + "Projects.ProjectID, Projects.ClientID, Projects.ProjectName, Projects.ProjectOwner, Projects.ProjectDescription, Projects.EmployeeID, Projects.Priority, Projects.TotalBilled, " 
SQLstr = SQLstr + "Goals.GoalID, Goals.ProjectID, Goals.Goal, Goals.Notes, Goals.Owners, Goals.Gpriority, " 
SQLstr = SQLstr + "Impediments.ImpedID, Impediments.IgoalID, Impediments.Impediment, Impediments.Notes, Impediments.Iweight, Impediments.Resolved, " 
SQLstr = SQLstr + "Causes.CauseID, Causes.cimpedID, Causes.cause, Causes.Notes, Causes.cweight, Causes.resolved, " 
SQLstr = SQLstr + "Solutions.SolutionID, Solutions.ScauseID, Solutions.Solution, Solutions.Notes, Solutions.Sweight, Solutions.Implemented, " 
SQLstr = SQLstr + "Actions.ActionID, Actions.AsolutionID, Actions.Action, Actions.Notes, Actions.Priority, Actions.Completed " 
SQLstr = SQLstr + "FROM ((Clients INNER JOIN Projects ON Clients.ClientID = Projects.ClientID) " 
SQLstr = SQLstr + "INNER JOIN ((Goals INNER JOIN Impediments ON Goals.GoalID = Impediments.IgoalID) " 
SQLstr = SQLstr + "INNER JOIN (Causes INNER JOIN Solutions ON Causes.causeID = Solutions.ScauseID) " 
SQLstr = SQLstr + "ON Impediments.ImpedID = Causes.cimpedID) ON Projects.ProjectID = Goals.ProjectID) " 
SQLstr = SQLstr + "INNER JOIN Actions ON Solutions.SolutionID = Actions.AsolutionID " 
SQLstr = SQLstr + "WHERE Clients.ClientID = " & SelectedClient & " AND Projects.ProjectID = " & SelectedProject & " ;" 


'Open source recordset 
Dim rsSource As Recordset 
Set rsSource = dbSource.OpenRecordset(SQLstr, dbOpenDynaset) 

'Open dest recordset 
Dim rsDestination As Recordset 
Set rsDestination = dbDestination.OpenRecordset("Clients", dbOpenDynaset) 

'Loop through source recordset 
'While Not rsSource.EOF 

    'Look for record in dest recordset 
    rsDestination.FindFirst "ContactFirstName = '" & rsSource.Fields("ContactFirstName") & "'" 
    '& " AND ContactLastName = " & rsSource.Fields("ContactLastName") & "" 

    'If not found, copy record 
    'Works okay 
    If rsDestination.NoMatch Then 
     rsDestination.AddNew 
     rsDestination.Fields("ContactFirstName") = rsSource.Fields("ContactFirstName") 
     rsDestination.Fields("ContactLastName") = rsSource.Fields("ContactLastName") 
     rsDestination.Fields("Address") = rsSource.Fields("Address") 
     rsDestination.Fields("City") = rsSource.Fields("City") 
     rsDestination.Fields("StateOrProvince") = rsSource.Fields("StateOrProvince") 
     rsDestination.Fields("PostalCode") = rsSource.Fields("PostalCode") 
     rsDestination.Fields("Country") = rsSource.Fields("Country") 
     rsDestination.Fields("EmailAddress") = rsSource.Fields("EmailAddress") 
     rsDestination.Fields("CompanyName") = rsSource.Fields("CompanyName") 
     rsDestination.Fields("PhoneNumber") = rsSource.Fields("PhoneNumber") 
     rsDestination.Fields("CellNumber") = rsSource.Fields("CellNumber") 
     rsDestination.Fields("BillingRate") = rsSource.Fields("BillingRate") 
     rsDestination.Fields("TaxPayable") = rsSource.Fields("TaxPayable") 
     rsDestination.Fields("Discount") = rsSource.Fields("Discount") 
     rsDestination.Update 
    Else 
    MsgBox "Record alreasy exists" 
    End If 
'Works okay 
Set rsDestination = dbDestination.OpenRecordset("Projects", dbOpenDynaset) 
    rsDestination.FindFirst "ClientID = 1" 
    If rsDestination.NoMatch Then 
     rsDestination.AddNew 
     rsDestination.Fields("ClientID") = 1 
     rsDestination.Fields("ProjectName") = rsSource.Fields("ProjectName") 
     rsDestination.Fields("ProjectOwner") = rsSource.Fields("ProjectOwner") 
     rsDestination.Fields("ProjectDescription") = rsSource.Fields("ProjectDescription") 
     rsDestination.Fields("EmployeeID") = rsSource.Fields("EmployeeID") 
     rsDestination.Fields("Priority") = rsSource.Fields("Projects.Priority") 
     rsDestination.Fields("TotalBilled") = rsSource.Fields("TotalBilled") 
     rsDestination.Update 
    Else 
    MsgBox "Record alreasy exists" 
    End If 

' Try to find number of Goals for a For ... Next procedure 
' Not counting Goals in Query. Will cause problems later when 2 or more client have same ProjectID 
iNumRecs = DCount("ProjectID", "Goals", "ProjectID = " & SelectedProject & "") 

'Here we need to copy all goals for projectID = 1 from 1 to number of goals' 
' Once this is done successfully the process can be repeated for tables below it 
'Loop through source recordset 
'Currently copies 6 x first goal. rsSource.Movenext not working. 
Set rsDestination = dbDestination.OpenRecordset("Goals", dbOpenDynaset) 
    rsDestination.FindFirst "ProjectID = " & SelectedProject & "" 
    If rsDestination.NoMatch Then 
    For i = 1 To iNumRecs 
     rsDestination.AddNew 
     rsDestination.Fields("ProjectID") = SelectedProject 
     rsDestination.Fields("Goal") = rsSource.Fields("Goal") 
     rsDestination.Fields("Notes") = rsSource.Fields("Goals.Notes") 
     rsDestination.Fields("Owners") = rsSource.Fields("Owners") 
     rsDestination.Fields("Gpriority") = rsSource.Fields("Gpriority") 
     rsDestination.Update 
     rsSource.MoveNext 
    Next 
    Else 
    MsgBox "Record alreasy exists" 
    End If 


MsgBox "Procedure successfully completed to this point" 

End Sub 

********************** **********結束碼**********************************

+0

你到目前爲止嘗試過什麼?請顯示你的一些代碼。很難提供有關實際編程的細節信息。 –

+0

對不起。錯誤的努力要簡潔。上面的代碼一直工作到複製目標。它複製了6個第一個目標,因爲Movenext沒有像我在rsSource記錄集上預期的那樣工作。 –

回答

0

您正在構建一個連接所有表的巨型源記錄集。所以這個記錄集會有大量的記錄(在你的例子中爲94),但是你輸出的各種表記錄較少。

這工作時,你知道你只需要插入一個記錄到目標記錄(客戶,項目),但如果有記錄的變量數將無法正常工作。

通過目標,您可以使用DCountFor循環限制其編號,但您只需獲取巨源的前6條記錄。由於加入,這通常會是同一個目標的6倍。

您應該爲所有表創建一個單獨的源記錄集,每個表的客戶機/項目都有一個WHERE子句。然後您可以將所有記錄從源複製到目標。


一個更容易方法是:

所有表直接到目標數據庫運行INSERT語句。

INSERT INTO [Goals] IN 'C:\Prosolve\Temp\Prosolve_BE.accdb' 
SELECT * 
FROM Goals 
WHERE Goals.ProjectID = 1; 

請注意,這也適用於自動編號列。


,並注意困難的部分仍然是在您的前方:導入更改後的數據將不會那麼容易。有多難取決於客戶端是隻能更改現有數據,還是可以添加/刪除記錄。

+0

謝謝安德烈。我最終得出了同樣的結論,即我咬得比我咀嚼的更多。我已經把記錄集分成了一些大小的部分,我將它們按表格插入。我已經放棄將更改的記錄導入原始數據庫的想法,除非我刪除整個項目並導入更改的項目。複製和粘貼。可能必須足夠好.... –