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

我有这个代码从Excel加载数据到我的表中的Access。 我有我的所有表中的[Load_date]的最后一列,并在我的表的devise视图中我使用默认值的函数=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 

  1. 为[Load_date]指定now()的缺省值,以便在导入后为空

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

  3. 在所有表导入之后,在您的表上运行更新查询以使用timestamp更新[Load_date] col,其中[Load_date]为null

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之前,您可能需要考虑closures警告,我强烈build议为您的潜艇添加一些error handling

在脚本开始处捕获“now”的时间戳,并将其存储在一个variables中。 然后使用该variables代替查询中的“now”函数