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
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.
Related
i had this error when i was trying to INSERT records to a local table with VBA.
I have checked the data type and putting in the quotes for the the short text data type but it doesn't work.
table_newid = "SELECT Cint(t1." & id_name(i) & "_new), " & Replace(select_column_str, local_table_name, "t2") & " FROM " & vbCrLf & _
"(SELECT CInt(DCount(""[" & id_name(i) & "]"", """ & qry_distinct_id_name & """, ""[" & id_name(i) & "]<="" & [" & id_name(i) & "])) as " & id_name(i) & "_new, * FROM " & qry_distinct_id_name & ") AS t1 " & vbCrLf & _
"LEFT JOIN " & local_table_name & "_ALL as t2 " & vbCrLf & _
"ON t1." & id_name(i) & " = t2." & id_name(i) & " " & vbCrLf & _
"WHERE t2.database = '" & database_name & "'"
strQuery = "INSERT INTO " & local_table_name & "_temp (" & temp_field(i) & ", " & Replace(select_column_str, local_table_name & ".", "") & ") " & vbCrLf & table_newid
Debug.Print strQuery
DoCmd.SetWarnings False
db.Execute strQuery
DoCmd.SetWarnings True
From the debug.print, i have got:
INSERT INTO TblLUMachineTypes_temp (MachTypeID_new, MachTypeID, MachTypeCode, MachTypeMod, MachTypeDesc, MachTypeDisc, NewCode, Approved, mttime, CreatedBy, CreatedTS, ModifiedBy, ModifiedTS)
SELECT t1.MachTypeID_new, t2.MachTypeID, t2.MachTypeCode, t2.MachTypeMod, t2.MachTypeDesc, t2.MachTypeDisc, t2.NewCode, t2.Approved, t2.mttime, t2.CreatedBy, t2.CreatedTS, t2.ModifiedBy, t2.ModifiedTS FROM
(SELECT CInt(DCount("[MachTypeID]", "qry_TblLUMachineTypes_id_distinct", "[MachTypeID]<=" & [MachTypeID])) as MachTypeID_new, * FROM qry_TblLUMachineTypes_id_distinct) AS t1
LEFT JOIN TblLUMachineTypes_ALL as t2
ON t1.MachTypeID = t2.MachTypeID
WHERE t2.database = 'CPM-252-2'
When i copied this query and execute it manually, it works fine but not with VBA. Any idea?
Thanks in advance.
Remove all the & vbCrLf from your code, they are not necessary and I assume they corrupt the SQL syntax.
I found the problem. I found that qry_distinct_id_name query table has a Dlookup function in there that returns a string value, which will work when executing the query manual but doesn't work when you run it with VBA. So I have re-written the code to put in the quote before and after dlookup() function.
i cannot update the data, it says that data type mismatch in criteria experession
CurrentDb.Execute "UPDATE PC_Details " & _
" SET Serial_No=" & Me.txtNo & _
", PC_Brand='" & Me.txtBrand & "'" & _
", PC_Model='" & Me.txtModel & "'" & _
", Status='" & Me.txtStatus & "'" & _
", Description='" & Me.txtDesc & "'" & _
", Staff_ID='" & Me.txtID & "'" & _
" WHERE Serial_No=" & Me.txtNo.Tag
Do this to debug:
Dim SQL As String
SQL = "UPDATE PC_Details " & _
" SET Serial_No=" & Me.txtNo & _
", PC_Brand='" & Me.txtBrand & "'" & _
", PC_Model='" & Me.txtModel & "'" & _
", Status='" & Me.txtStatus & "'" & _
", Description='" & Me.txtDesc & "'" & _
", Staff_ID='" & Me.txtID & "'" & _
" WHERE Serial_No=" & Me.txtNo.Tag
Debug.Print SQL
CurrentDb.Execute SQL
Then study the resulting SQL and post it here.
If all values are present, one or more is text but is handled as number or vice versa.
I have an excel userform with various textboxes, some are fields to enter dates. The user can then save their entries.
At this point, I connect to an access backend via an ADO connection. The values entered by a user are passes to an SQL string, e.g.
strSQL = "UPDATE tblDECONVERSION_DATA SET tblDECONVERSION_DATA.Status = '" & NewBusiness_WorkQueue.Decon_CaseStatus & "', " & _
"tblDECONVERSION_DATA.DMS = '" & NewBusiness_WorkQueue.Decon_DMS & "', " & _
"tblDECONVERSION_DATA.DateRecieved = #" & Format(NewBusiness_WorkQueue.Decon_DateRecieved, "mm/dd/yyyy") & "#, " & _
"tblDECONVERSION_DATA.WireDate = #" & Format(NewBusiness_WorkQueue.Decon_WireDate, "mm/dd/yyyy") & "#, " & _
"tblDECONVERSION_DATA.LastEditXID = '" & CurrUser & "', tblDECONVERSION_DATA.LastEditDate = #" & Now & "# " & _
"WHERE (((tblDECONVERSION_DATA.CaseID)=" & ID & "));"
adoRecSet.Open Source:=strSQL, ActiveConnection:=dbconnect, CursorType:=adOpenDynamic, LockType:=adLockOptimistic
However, some of the date fields can be left blank, meaning for example the NewBusiness_WorkQueue.Decon_DateRecieved variable being empty. This causes a syntax error. How can I pass a Null or Empty date variable in the SQL statement that both VBA and the access database will accept?
strSQL = "UPDATE tblDECONVERSION_DATA SET tblDECONVERSION_DATA.Status = '" & _
NewBusiness_WorkQueue.Decon_CaseStatus & "', " & _
"tblDECONVERSION_DATA.DMS = '" & NewBusiness_WorkQueue.Decon_DMS & "', " & _
"tblDECONVERSION_DATA.DateRecieved = " & _
DateOrNull(NewBusiness_WorkQueue.Decon_DateRecieved) & ", " & _
"tblDECONVERSION_DATA.WireDate = " & _
DateOrNull(NewBusiness_WorkQueue.Decon_WireDate) & ", " & _
"tblDECONVERSION_DATA.LastEditXID = '" & CurrUser & _
"', tblDECONVERSION_DATA.LastEditDate = #" & Now & "# " & _
"WHERE tblDECONVERSION_DATA.CaseID=" & ID & ";"
An example function:
Function DateOrNull(v) As String
Dim rv as String
If IsDate(v) Then
rv = " #" & Format(v, "mm/dd/yyyy") & "# "
Else
rv = " null "
End If
DateOrNull = rv
End Function
I need some help with this, sorry, I am new in VBA and I am trying to run a update query which should obtain a value from a variable and update an already existing table. This is done using VBA. On executing no error message is shown but the table isn't updated with the new value. The code is as follows:
Query = "UPDATE Results " & _
"SET fk_Report='" & Report & "'" & _
",fk_Name='" & Namevar & "'" & _
",fk_Age='" & Agevar & "'" & _
",fk_Sex='" & Sexvar & "'" & _
"WHERE [Count]='" & Countvar & "'" & _
",[Positives]='" & Posvar & "'" & _
",[Negatives]='" & Negvar & "'" & _
",[Unknow]='" & Unkvar & "';"
CurrentDb.Execute (Query)
If somebody can help...
You don't need the commas in the where clause
Query = "UPDATE Results " & _
"SET fk_Report='" & Report & "'" & _
",fk_Name='" & Namevar & "'" & _
",fk_Age='" & Agevar & "'" & _
",fk_Sex='" & Sexvar & "'" & _
"WHERE [Count]='" & Countvar & "' " & _
"AND [Positives]='" & Posvar & "' " & _
"AND [Negatives]='" & Negvar & "' " & _
"AND [Unknow]='" & Unkvar & "';"
CurrentDb.Execute (Query)
use AND instead of , (comma) after WHERE clause
I am new to using Access 2010. I wish to execute the following sql update statement, but I have problems with the syntax. The table is called "Forecasts", and users will edit & update the qty forecasted.
Problem - The table fieldnames are 2014_1, 2014_2, 2014_3 ... to represent the different months, stored in an array. I have done abit of research and I believe the way to dynamically do this is:
Dim sqlString As String
sqlString = "UPDATE Forecasts " & _
" SET Branch_Plant=" & Me.txtBranch_Plant & _
", Item_Number_Short='" & Me.txtItem_Number_Short & "'" & _
", Description='" & Me.txtDescription & "'" & _
", UOM='" & Me.txtUOM & "'" & _
", Estimated_Cost=" & Me.txtEstimated_Cost & _
", Requesting_Business_Unit='" & Me.txtRequesting_Business_Unit & "'" & _
", End_Customer='" & Me.txtEnd_Customer & "'" & _
", Project='" & Me.txtProject & "'" & _
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " & _
" WHERE ID =" & Me.txtID.Tag
MsgBox ("This is the output: " & sqlString)
CurrentDb.Execute sqlString
It was working fine until this line was added
Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume
The msgbx output now shows: "False". Whats wrong with sqlString?
Please help! Thank you very much.
", Forecasts.[" & arrMonthToDisplay(0) & "] = " & Me.txtProjectedJanVolume & _
" WHERE ID =" & Me.txtID.Tag
You compare string to string.
Change
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " &
to
", Forecasts." & "[" & arrMonthToDisplay(0) & "] = " & " Me.txtProjectedJanVolume " &