如何解决VBA代码中的SQL查询?

时间:2018-11-20 14:20:07

标签: sql vba ms-access

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

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*"));

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

下面,我附加了带有语法编译错误的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



        '###########################################################3
        '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]>=#" & 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]=1073742165,1,0)) AS [Transformer Installation]" & _
                        " FROM TR343DrySide" & _
                        " WHERE (([TransducerSN] Like ""CR*""));"







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

        '###########################################################
        'DRYSIDE DEPOT
        strDrySQL_Depot = "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] Not Like ""CR*""));"

        Me.sfrmCraneDrySidePassFailDateRange_Depot.Form.RecordSource = strDrySQL_Depot
        Me.sfrmCraneDrySidePassFailDateRange_Depot.Visible = True
    End If
End Sub

我相信该语句中会发生错误:

  "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],"

1 个答案:

答案 0 :(得分:1)

您的字符串不好,并且在这些行中由于字符串中有流氓"而变得无效:

" 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 & "#)" & _
像这样的

MultiLine字符串分配应该用于使代码更具可读性。就您的情况而言,我认为这使内容的可读性和故障排除更加困难。我建议从此sql中构建查询对象,而不是在VBA中构建字符串。仅出于故障排除的目的,如果必须在vba中构建它-请使用结构化,自连接的方式构建字符串。以这种方式查看编译错误要容易得多。

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

以上代码实际上将debug.print格式化良好的SQL语句;轻松解决问题