为什么是我。参考和INSERT INTO导致错误?

问题描述 投票:0回答:2

尝试创建查询以对记录进行更新后更新我的审核日志时收到两个错误。

首先,我被提示输入参数值,我相信我的ME.代码将指向具有字段[Corrected Med Ed ID]当前值的任何记录。

第二我收到消息:

INSERT INTO语句中的语法错误。

我的表单的代码是-

Private Sub Form_AfterUpdate()
    Dim UserName As String

    DoCmd.SetWarnings False
    'updates date
    DoCmd.RunSQL "update tbl_InterfaceLog SET tbl_InterfaceLog.[LastUpdated] = Date() Where [Corrected Med Ed ID] = " & Me.[Corrected Med Ed ID]
    'stamps username
    DoCmd.RunSQL "update tbl_InterfaceLog SET tbl_InterfaceLog.[LastUpdatedBy] = Username() Where [Corrected Med Ed ID] = " & Me.[Corrected Med Ed ID]
    'Adds the record to log
    DoCmd.RunSQL "INSERT INTO tbl_AuditLog (Status, Comments, Owner, [corrected med ed ID], [Upload File Name], [Submission Method], AirfareStatus, GroundStatus, MealsStatus, AccomodationStatus, AirfareComment, GroundComment, MealsComment, AccommodationComment, Coordinator, SupportRequested, LastUpdated, Reviewed, ReviewerComment, RequiredChange, EventDate, ProductsMatch, SubmissionMethod, TravelDestination, LastUpdatedBy" _
    & " SELECT tbl_InterfaceLog.Status, tbl_InterfaceLog.Comments, tbl_InterfaceLog.Owner, tbl_InterfaceLog.[corrected med ed ID], tbl_InterfaceLog.[Upload File Name], tbl_InterfaceLog.[Submission Method], tbl_InterfaceLog.AirfareStatus, tbl_InterfaceLog.GroundStatus, tbl_InterfaceLog.MealsStatus, tbl_InterfaceLog.AccomodationStatus, tbl_InterfaceLog.AirfareComment, tbl_InterfaceLog.GroundComment, tbl_InterfaceLog.MealsComment, tbl_InterfaceLog.AccommodationComment, tbl_InterfaceLog.Coordinator, tbl_InterfaceLog.SupportRequested, tbl_InterfaceLog.LastUpdated, tbl_InterfaceLog.Reviewed, tbl_InterfaceLog.ReviewerComment, tbl_InterfaceLog.RequiredChange, tbl_InterfaceLog.EventDate, tbl_InterfaceLog.ProductsMatch, tbl_InterfaceLog.SubmissionMethod, tbl_InterfaceLog.TravelDestination, tbl_InterfaceLog.LastUpdatedBy" _
    & " FROM tbl_InterfaceLog" _
    & " WHERE [Corrected Med Ed ID] = " & Me.[Corrected Med Ed ID]
    DoCmd.SetWarnings True
End Sub
sql vba ms-access access-vba
2个回答
1
投票

仅使用参数化查询,而无需在VBA中连接SQL字符串。具体来说,将以下两个SQL语句另存为存储的Access查询。然后,使用VBA中的QueryDefs绑定参数以获得更清晰,可读和可维护的代码。

注意:两个更新查询可以合并为一个。另外,使用表别名t。使用查询设计时,Access将不允许您保存语法错误的查询。下面假设参数为字符串/文本值。

SQL

更新查询

PARAMETERS idparam TEXT(255);
UPDATE tbl_InterfaceLog t
   SET t.[LastUpdated] = Date(),
       t.[LastUpdatedBy] = Username()
WHERE [CorrectedMedEdID] = idparam;

追加查询

PARAMETERS idparam TEXT(255);
INSERT INTO tbl_AuditLog ([Status], [Comments], [Owner], [corrected med ed ID], 
                          [Upload File Name], [Submission Method], AirfareStatus,
                          GroundStatus, MealsStatus, AccomodationStatus, 
                          AirfareComment, GroundComment, MealsComment, 
                          AccommodationComment, Coordinator, SupportRequested, 
                          LastUpdated, Reviewed, ReviewerComment, RequiredChange, 
                          EventDate, ProductsMatch, SubmissionMethod, 
                          TravelDestination, LastUpdatedBy)

SELECT t.Status, t.Comments, t.Owner, t.[corrected med ed ID], t.[Upload File Name],
       t.[Submission Method], t.AirfareStatus, t.GroundStatus, t.MealsStatus, 
       t.AccomodationStatus, t.AirfareComment, t.GroundComment, t.MealsComment, 
       t.AccommodationComment, t.Coordinator, t.SupportRequested, t.LastUpdated, 
       t.Reviewed, t.ReviewerComment, t.RequiredChange, t.EventDate, t.ProductsMatch, 
       t.SubmissionMethod, t.TravelDestination, t.LastUpdatedBy
FROM tbl_InterfaceLog t
WHERE t.[CorrectedMedEdID] = idparam;

VBA (无连接)

Private Sub Form_AfterUpdate()
    Dim UserName As String
    Dim qdef as QueryDef

    ' UPDATE QUERY 
    Set qdef = CurrentDb.QueryDefs("myUpdateQuery")        
    qdef!idparam = Me.[Corrected Med Ed ID]             ' BIND PARAM
    qdef.Execute dbFailOnError                          ' EXECUTE QUERY
    Set qdef = Nothing    

    ' APPEND QUERY
    Set qdef = CurrentDb.QueryDefs("myAppendQuery")        
    qdef!idparam = Me.[Corrected Med Ed ID]             ' BIND PARAM
    qdef.Execute dbFailOnError                          ' EXECUTE QUERY
    Set qdef = Nothing
End Sub

或DRY-er方法:

Private Sub Form_AfterUpdate()
    Dim UserName As String, qry As Variant
    Dim qdef as QueryDef

    ' RUN ACTION QUERIES
    For Each qry in Array("myUpdateQuery", "myAppendQuery")
        Set qdef = CurrentDb.QueryDefs(qry)  
        qdef!idparam = Me.[Corrected Med Ed ID]         ' BIND PARAM
        qdef.Execute dbFailOnError                      ' EXECUTE QUERY
        Set qdef = Nothing    
    Next qry

    Set qdef = Nothing
End Sub

0
投票

您是否已检查并研究了定界时数据库中的SQL列名称是否区分大小写?即在此表上创建。如果是这样,[corrected med ed ID][Corrected Med Ed ID]

您可以尝试修复Case并查看其是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.