为了更新现有的记录,需要比较这两个记录,并在目标数据库中不相同时加以更新。
视所涉领域的数量而定,这可能是复杂的。
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
您可以通过这一职能通过两个表格的名称或两个查询名称。 这使许多灵活性成为可能。 它假设所通过物体的实地名称相同,如果它们的名字相同,你可以提出对田地的询问,以便与另一张表格的相匹配。
这是使用刺.时间的编号一的变式。 基本原则是,它根据各行各有不同价值的最新表格,进行一系列的审调和查询。