如何将数据导入到完全相同时间戳的表中?

时间:2015-08-27 20:24:31

标签: excel vba ms-access access-vba

我有这个代码,可以将数据从Excel加载到Access中的表格中。我的所有表中的最后一列都是[Load_date],在我的表的设计视图中,我使用函数=Now()作为默认值。

我的问题是我的每张桌子都有不同的时间戳差异几秒钟。 如何更新我的VBA代码以在同一时间导入所有表中的数据?

Sub ImportAllTables_New_Click()
Call LoadData("C:\Idea Attributes\tbl_IdeasITAssumptions.xlsm", "TempIdeasITAssumptions", "Qry_IdeasITAssumptions", "Qry_AppendIdeasITAssumptions")
Call LoadData("C:\Idea Attributes\tbl_IdeasDependencies.xlsm", "TempIdeasDependencies", "Qry_IdeasDependencies", "Qry_AppendIdeasDependencies")
Call LoadData("C:\Idea Attributes\tbl_IdeasImpactedPlan.xlsm", "TempIdeasImpactedPlan", "Qry_IdeasImpactedPlan", "Qry_AppendIdeasImpactedPlan")
Call LoadData("C:\Idea Attributes\tbl_IdeasImpactedSubsidiaries.xlsm", "TempIdeasImpactedSubsidiaries", "Qry_IdeasImpactedSubsidiaries", "Qry_AppendIdeasImpactedSubsidiaries")
Call LoadData("C:\Idea Attributes\tbl_IdeasLOB.xlsm", "TempIdeasLOB", "Qry_IdeasLOB", "Qry_AppendIdeasLOB")
Call LoadData("C:\Idea Attributes\tbl_IdeasPhaseGate.xlsm", "TempIdeasPhaseGate", "Qry_IdeasPhaseGate", "Qry_AppendIdeasPhaseGate")
Call LoadData("C:\Idea Attributes\tbl_IdeasDataExtractMain.xlsm", "TempIdeasDataExtractMain", "Qry_IdeasDataExtractMain", "Qry_AppendIdeasDataExtractMain")

End Sub

Sub LoadData(Filepath As String, TempTable As String, Qry_Ideas As Variant, Qry_Append As Variant)

If FileExist(Filepath) Then
    DoCmd.TransferSpreadsheet acImport, , TempTable, Filepath, True

     'The following will Check for new Load_Date , if it is not new you will get no new data msg. This function is currently not useful since we are using Now() function in our tables.
     'But if in future we need to use it, delete now() in tables it self under Load_Date Default Value.
    If IsNull(DLookup("(idea_code)", Qry_Ideas)) Then
        MsgBox "No New Data to add"
    Else
        DoCmd.OpenQuery Qry_Append, acViewNormal
    End If
Else
    MsgBox "File not found. Please check the filename or File Location."

End If

'Use Sql Command to delete everything in Temp Table
Dim SQLDelete As String

    SQLDelete = "Delete * from " & TempTable
    DoCmd.RunSQL SQLDelete
End Sub

2 个答案:

答案 0 :(得分:2)

  1. 为[Load_date]抛弃now()的默认值,因此导入后它将为null

  2. 决定是否要在导入开始之前或导入结束之后捕获时间戳。 (见2A和2B)

  3. 导入所有表后,在表上运行更新查询以使用[Load_date]为空的时间戳更新[Load_date] col

  4. 2A。在导入开始之前

      Sub ImportAllTables_New_Click()
        dim dt as date
        dt = now() 'save current single timestamp for later use
    
        'do all of your loaddata() calls
    
        'update all [Load_date] null values to the timestamp captured before you started import
        'the only nulls are going to be the newest records added
        'no problem to run this if there are none.
        docmd.runsql "UPDATE [yourtablename] set [Load_date] = #" & dt & "# where [Load_date] is null"
    
    End Sub
    

    2B。导入完成后

      Sub ImportAllTables_New_Click()
    
        'do all of your loaddata() calls
    
        'update all [Load_date] null values with current timestamp
        'the only nulls are going to be the newest records added
        'no problem to run this if there are none.
        docmd.runsql "UPDATE [yourtablename] set [Load_date] = now() where [Load_date] is null"
    
    End Sub
    

    我希望有所帮助。 您可能希望在运行SQL之前关闭警告,我强烈建议您向子服务器添加一些错误处理

答案 1 :(得分:1)

在脚本开头捕获“now”的时间戳并将其存储在变量中。然后使用该变量代替查询中的“now”函数