MS-Access union query syntax error within VBA code












0















I have made a SQL statement that calculates the sum of the chosen fields I've gathered. I have the queries working within MS Access, but when I translate it to VBA coding within my database it spits out a compile error 3319: Syntax error. Below I have attached my working query as well as my query with the syntax.



Query within MS Access is below that works properly:



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


When this query is run, it tallies up the sum of the fields between the chosen dates.



Below I have attached my VBA code that comes up with a syntax compile error 3319:



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



The result when the query is activated is: Run-time error '3319':
Syntax error within union query.




The problem is raised within the line of:
Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New










share|improve this question

























  • Did you ask this yesterday?

    – Nathan_Sav
    Nov 21 '18 at 13:12











  • yes, this was asked yesterday but made some changes to my question and code for more clarification.

    – nick irvin
    Nov 21 '18 at 13:15











  • And what is the error message and which line raises it? And include the resulting strDrySQL_New, please.

    – Gustav
    Nov 21 '18 at 13:16













  • @Gustav I have added more information on the error message and where it questions the line, thanks.

    – nick irvin
    Nov 21 '18 at 13:24











  • Possibly the vbCrLf in your SQL? SQL doesn't need carriage returns - it's just one long string. We only split it into multiple lines to make it easier to read.

    – Darren Bartrup-Cook
    Nov 21 '18 at 13:34


















0















I have made a SQL statement that calculates the sum of the chosen fields I've gathered. I have the queries working within MS Access, but when I translate it to VBA coding within my database it spits out a compile error 3319: Syntax error. Below I have attached my working query as well as my query with the syntax.



Query within MS Access is below that works properly:



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


When this query is run, it tallies up the sum of the fields between the chosen dates.



Below I have attached my VBA code that comes up with a syntax compile error 3319:



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



The result when the query is activated is: Run-time error '3319':
Syntax error within union query.




The problem is raised within the line of:
Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New










share|improve this question

























  • Did you ask this yesterday?

    – Nathan_Sav
    Nov 21 '18 at 13:12











  • yes, this was asked yesterday but made some changes to my question and code for more clarification.

    – nick irvin
    Nov 21 '18 at 13:15











  • And what is the error message and which line raises it? And include the resulting strDrySQL_New, please.

    – Gustav
    Nov 21 '18 at 13:16













  • @Gustav I have added more information on the error message and where it questions the line, thanks.

    – nick irvin
    Nov 21 '18 at 13:24











  • Possibly the vbCrLf in your SQL? SQL doesn't need carriage returns - it's just one long string. We only split it into multiple lines to make it easier to read.

    – Darren Bartrup-Cook
    Nov 21 '18 at 13:34
















0












0








0








I have made a SQL statement that calculates the sum of the chosen fields I've gathered. I have the queries working within MS Access, but when I translate it to VBA coding within my database it spits out a compile error 3319: Syntax error. Below I have attached my working query as well as my query with the syntax.



Query within MS Access is below that works properly:



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


When this query is run, it tallies up the sum of the fields between the chosen dates.



Below I have attached my VBA code that comes up with a syntax compile error 3319:



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



The result when the query is activated is: Run-time error '3319':
Syntax error within union query.




The problem is raised within the line of:
Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New










share|improve this question
















I have made a SQL statement that calculates the sum of the chosen fields I've gathered. I have the queries working within MS Access, but when I translate it to VBA coding within my database it spits out a compile error 3319: Syntax error. Below I have attached my working query as well as my query with the syntax.



Query within MS Access is below that works properly:



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


When this query is run, it tallies up the sum of the fields between the chosen dates.



Below I have attached my VBA code that comes up with a syntax compile error 3319:



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



The result when the query is activated is: Run-time error '3319':
Syntax error within union query.




The problem is raised within the line of:
Me.sfrmCraneDrySidePassFailDateRange_New.Form.RecordSource = strDrySQL_New







sql vba ms-access






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 13:21







nick irvin

















asked Nov 21 '18 at 13:10









nick irvinnick irvin

12




12













  • Did you ask this yesterday?

    – Nathan_Sav
    Nov 21 '18 at 13:12











  • yes, this was asked yesterday but made some changes to my question and code for more clarification.

    – nick irvin
    Nov 21 '18 at 13:15











  • And what is the error message and which line raises it? And include the resulting strDrySQL_New, please.

    – Gustav
    Nov 21 '18 at 13:16













  • @Gustav I have added more information on the error message and where it questions the line, thanks.

    – nick irvin
    Nov 21 '18 at 13:24











  • Possibly the vbCrLf in your SQL? SQL doesn't need carriage returns - it's just one long string. We only split it into multiple lines to make it easier to read.

    – Darren Bartrup-Cook
    Nov 21 '18 at 13:34





















  • Did you ask this yesterday?

    – Nathan_Sav
    Nov 21 '18 at 13:12











  • yes, this was asked yesterday but made some changes to my question and code for more clarification.

    – nick irvin
    Nov 21 '18 at 13:15











  • And what is the error message and which line raises it? And include the resulting strDrySQL_New, please.

    – Gustav
    Nov 21 '18 at 13:16













  • @Gustav I have added more information on the error message and where it questions the line, thanks.

    – nick irvin
    Nov 21 '18 at 13:24











  • Possibly the vbCrLf in your SQL? SQL doesn't need carriage returns - it's just one long string. We only split it into multiple lines to make it easier to read.

    – Darren Bartrup-Cook
    Nov 21 '18 at 13:34



















Did you ask this yesterday?

– Nathan_Sav
Nov 21 '18 at 13:12





Did you ask this yesterday?

– Nathan_Sav
Nov 21 '18 at 13:12













yes, this was asked yesterday but made some changes to my question and code for more clarification.

– nick irvin
Nov 21 '18 at 13:15





yes, this was asked yesterday but made some changes to my question and code for more clarification.

– nick irvin
Nov 21 '18 at 13:15













And what is the error message and which line raises it? And include the resulting strDrySQL_New, please.

– Gustav
Nov 21 '18 at 13:16







And what is the error message and which line raises it? And include the resulting strDrySQL_New, please.

– Gustav
Nov 21 '18 at 13:16















@Gustav I have added more information on the error message and where it questions the line, thanks.

– nick irvin
Nov 21 '18 at 13:24





@Gustav I have added more information on the error message and where it questions the line, thanks.

– nick irvin
Nov 21 '18 at 13:24













Possibly the vbCrLf in your SQL? SQL doesn't need carriage returns - it's just one long string. We only split it into multiple lines to make it easier to read.

– Darren Bartrup-Cook
Nov 21 '18 at 13:34







Possibly the vbCrLf in your SQL? SQL doesn't need carriage returns - it's just one long string. We only split it into multiple lines to make it easier to read.

– Darren Bartrup-Cook
Nov 21 '18 at 13:34














1 Answer
1






active

oldest

votes


















0














At least here, you have an extra closing parenthesis:



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


Should be:



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


Insert a line:



Debug.Print strDrySQL_New


and study the print.






share|improve this answer
























  • many thanks, mate. Will try this out.

    – nick irvin
    Nov 21 '18 at 16:08











  • great solution, Gustav. works like a charm now. Thank you

    – nick irvin
    Nov 21 '18 at 16:12











  • Fine! Then please mark as answered.

    – Gustav
    Nov 21 '18 at 16:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412802%2fms-access-union-query-syntax-error-within-vba-code%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














At least here, you have an extra closing parenthesis:



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


Should be:



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


Insert a line:



Debug.Print strDrySQL_New


and study the print.






share|improve this answer
























  • many thanks, mate. Will try this out.

    – nick irvin
    Nov 21 '18 at 16:08











  • great solution, Gustav. works like a charm now. Thank you

    – nick irvin
    Nov 21 '18 at 16:12











  • Fine! Then please mark as answered.

    – Gustav
    Nov 21 '18 at 16:33
















0














At least here, you have an extra closing parenthesis:



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


Should be:



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


Insert a line:



Debug.Print strDrySQL_New


and study the print.






share|improve this answer
























  • many thanks, mate. Will try this out.

    – nick irvin
    Nov 21 '18 at 16:08











  • great solution, Gustav. works like a charm now. Thank you

    – nick irvin
    Nov 21 '18 at 16:12











  • Fine! Then please mark as answered.

    – Gustav
    Nov 21 '18 at 16:33














0












0








0







At least here, you have an extra closing parenthesis:



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


Should be:



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


Insert a line:



Debug.Print strDrySQL_New


and study the print.






share|improve this answer













At least here, you have an extra closing parenthesis:



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


Should be:



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


Insert a line:



Debug.Print strDrySQL_New


and study the print.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 13:48









GustavGustav

29.5k51734




29.5k51734













  • many thanks, mate. Will try this out.

    – nick irvin
    Nov 21 '18 at 16:08











  • great solution, Gustav. works like a charm now. Thank you

    – nick irvin
    Nov 21 '18 at 16:12











  • Fine! Then please mark as answered.

    – Gustav
    Nov 21 '18 at 16:33



















  • many thanks, mate. Will try this out.

    – nick irvin
    Nov 21 '18 at 16:08











  • great solution, Gustav. works like a charm now. Thank you

    – nick irvin
    Nov 21 '18 at 16:12











  • Fine! Then please mark as answered.

    – Gustav
    Nov 21 '18 at 16:33

















many thanks, mate. Will try this out.

– nick irvin
Nov 21 '18 at 16:08





many thanks, mate. Will try this out.

– nick irvin
Nov 21 '18 at 16:08













great solution, Gustav. works like a charm now. Thank you

– nick irvin
Nov 21 '18 at 16:12





great solution, Gustav. works like a charm now. Thank you

– nick irvin
Nov 21 '18 at 16:12













Fine! Then please mark as answered.

– Gustav
Nov 21 '18 at 16:33





Fine! Then please mark as answered.

– Gustav
Nov 21 '18 at 16:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412802%2fms-access-union-query-syntax-error-within-vba-code%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga