VBA代码中的MS-Access联合查询语法错误

时间:2018-11-21 13:10:27

标签: sql vba ms-access

我制作了一条SQL语句,用于计算我收集的所选字段的总和。我的查询在MS Access中运行,但是当我将其转换为数据库中的VBA编码时,它会吐出编译错误3319:语法错误。在下面,我附加了我的工作查询以及使用语法的查询。

MS Access中的查询在下面可以正常使用:

SELECT 1,'Passed - Depot' AS QRY, Sum(IIf(([PreStressStackDate]>=[StartDate] And [PreStressStackDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=5 And [CurrentLevelOfCompletion]<1073741829) Or [CurrentLevelOfCompletion]>1073741829),1,0)) AS [PreStress Stackup], Sum(IIf(([StackCompressionDate]>=[StartDate] And [StackCompressionDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=21 And [CurrentLevelOfCompletion]<1073741845) Or [CurrentLevelOfCompletion]>1073741845),1,0)) AS [Stack Compression], Sum(IIf(([TestingDate]>=[StartDate] And [TestingDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>85 And [CurrentLevelOfCompletion]<1073741909) Or [CurrentLevelOfCompletion]>1073741909),1,0)) AS Testing, Sum(IIf(([ShroudAssemblyDate]>=[StartDate] And [ShroudAssemblyDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=341 And [CurrentLevelOfCompletion]<1073742165) Or [CurrentLevelOfCompletion]>1073742165),1,0)) AS [Shroud Assembly], Sum(IIf(([TransformerInstallDate]>=[StartDate] And [TransformerInstallDate]<=[EndDate]) And (([CurrentLevelOfCompletion]>=1365 And [CurrentLevelOfCompletion]<1073743189)),1,0)) AS [Transformer Installation]
FROM TR343DrySide
WHERE (([TransducerSN] Not Like "CR*"));

UNION SELECT 2, 'Failed - Depot' AS QRY, Sum(IIf(([PreStressStackDate]>=[StartDate] And [PreStressStackDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073741829,1,0)) AS [PreStress Stackup], Sum(IIf(([StackCompressionDate]>=[StartDate] And [StackCompressionDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073741845,1,0)) AS [Stack Compression], Sum(IIf(([TestingDate]>=[StartDate] And [TestingDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073741909,1,0)) AS [Testing], Sum(IIf(([ShroudAssemblyDate]>=[StartDate] And [ShroudAssemblyDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073742165,1,0)) AS [Shroud Assembly], Sum(IIf(([TransformerInstallDate]>=[StartDate] And [TransformerInstallDate]<=[EndDate]) And [CurrentLevelOfCompletion]=1073743189,1,0)) AS [Transformer Installation]
FROM TR343DrySide
WHERE (([TransducerSN] Not Like "CR*"));

运行此查询时,它会汇总所选日期之间的字段总和。

下面,我附加了带有语法编译错误3319的VBA代码:

Private Sub cmdDrySideRunReport_Click()

    Dim strDrySQL_New, strDrySQL_Depot       As String
    Dim DryStartDate                         As Date
    Dim DryEndDate                           As Date


  '------------------------------------------------------------------------------------------------------
  If IsNull(Me.txtDryStartDate) Or Me.txtDryStartDate = "" Or IsNull(Me.txtDryEndDate) Or Me.txtDryEndDate = "" Then
        If IsNull(Me.txtDryStartDate) Or Me.txtDryStartDate = "" Then
            MsgBox "Please enter the Start Date"
            Me.txtDryStartDate.SetFocus
        End If
        If IsNull(Me.txtDryEndDate) Or Me.txtDryEndDate = "" Then
            MsgBox "Please enter the End Date"
            Me.txtDryEndDate.SetFocus
        End If
    Else
        DryStartDate = Me.txtDryStartDate
        DryEndDate = Me.txtDryEndDate + 1



        '###########################################################
        'DRYSIDE NEW

strDrySQL_New = "Select 1, 'Passed - New' AS QRY, Sum(IIf(([PreStressStackDate]>=#" & DryStartDate & "# And [PreStressStackDate]<=#" & DryEndDate & "#)" & _
                    " And (([CurrentLevelOfCompletion]>=5 And [CurrentLevelOfCompletion]<1073741829) Or [CurrentLevelOfCompletion]>1073741829),1,0)) AS [PreStress Stackup]," & _
                    " Sum(IIf(([StackCompressionDate]>=#" & DryStartDate & "# And [StackCompressionDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=21" & _
                    " And [CurrentLevelOfCompletion]<1073741845) Or [CurrentLevelOfCompletion]>1073741845),1,0)) AS [Stack Compression]," & _
                    " Sum(IIf(([TestingDate]>=#" & DryStartDate & "# And [TestingDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=85" & _
                    vbCrLf & " And [CurrentLevelOfCompletion]<1073741909) Or [CurrentLevelOfCompletion]>1073741909),1,0)) AS [Testing]," & _
                    " Sum(IIf(([ShroudAssemblyDate]>=#" & DryStartDate & "# And [ShroudAssemblyDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=341" & _
                    " And [CurrentLevelOfCompletion]<1073742165) Or [CurrentLevelOfCompletion]>1073742165),1,0)) AS [Shroud Assembly]," & _
                    " Sum(IIf(([TransformerInstallDate]>=#" & DryStartDate & "# And [TransformerInstallDate]<=#" & DryEndDate & "#) And (([CurrentLevelOfCompletion]>=1365 And [CurrentLevelOfCompletion]<1073743189)),1,0)) AS [Transformer Installation]" & _
                    " FROM TR343DrySide" & _
                    " WHERE (([TransducerSN] Like ""CR*""))" & _
                    vbCrLf & " UNION SELECT 2, 'Failed - New' AS QRY, Sum(IIf(([PreStressStackDate]>=#" & DryStartDate & "# And [PreStressStackDate]<=#" & DryEndDate & "#)" & _
                    " And [CurrentLevelOfCompletion]=1073741829),1,0)) AS [PreStress Stackup], Sum(IIf(([StackCompressionDate]>=#" & DryStartDate & "#" & _
                    " And [StackCompressionDate]<=#" & DryEndDate & "#) And [CurrentLevelOfCompletion]=1073741845,1,0)) AS [Stack Compression]," & _
                    " Sum(IIf(([TestingDate]>=#" & DryStartDate & "# And [TestingDate]<=#" & DryEndDate & "#) And [CurrentLevelOfCompletion]=1073741909,1,0)) AS [Testing]," & _
                    " Sum(IIf(([ShroudAssemblyDate]>=#" & DryStartDate & "# And [ShroudAssemblyDate]<=#" & DryEndDate & "#) And ([CurrentLevelOfCompletion]=1073742165 Or" & _
                    " [CurrentLevelOfCompletion]=1073742165),1,0)) AS [Shroud Assembly], Sum(IIf(([TransformerInstallDate]>=#" & DryStartDate & "# And [TransformerInstallDate]<=#" & DryEndDate & "#)" & _
                    " And [CurrentLevelOfCompletion]=1073743189,1,0)) AS [Transformer Installation]" & _
                    " FROM TR343Dryside" & _
                    " WHERE (([TransducerSN] Like ""CR*""));"





    Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New
    Me.sfrmCraneDrySidePassFailDateRange_New.Visible = True

 End If
End Sub
  

激活查询时的结果是:运行时错误'3319':   联合查询中的语法错误。

在以下行中出现此问题:     Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New

1 个答案:

答案 0 :(得分:0)

至少在这里,您还有一个右括号:

.. And [CurrentLevelOfCompletion]=1073741829),1,0)) 

应该是:

.. And [CurrentLevelOfCompletion]=1073741829,1,0)) 

插入一行:

Debug.Print strDrySQL_New

并研究印刷品。