Microsoft Access run-time error '3075' Syntax Error - sql

I want to delete something in my database using a button with the following VBA behind it.
Unfortunately it gives me a run-time error and I don't know why. I've looked how Access give the SQL statement back and in SQL Server it gives me no errors.
VBA:
Private Sub btnDeleteTaak_Click()
If Not IsNull(Me.lstTaakMonteur) Then
If Not IsNull(Me.comboMonteur.Value) Then
If Not IsNull(Me.comboOpdracht.Value) Then
Dim DeleteSQL As String
DeleteSQL = "DELETE OpdrachtTaak WHERE opdrachtnr = " & Me.comboOpdracht.Value & " AND taaknr = " & Me.lstTaakMonteur.Value & " AND monteurcode = '" & Me.comboMonteur.Value & "'"
DoCmd.RunSQL (DeleteSQL)
Me.lstTaakMonteur.Requery
Else
MsgBox ("Er is geen opdracht geselecteerd")
End If
Else
MsgBox ("Er is geen monteur geselecteerd")
End If
Else
MsgBox ("Er is geen taak geselecteerd")
End If
End Sub
The error:
Syntax error (missing operator) in query expression 'OpdrachtTaak WHERE opdrachtnr = 1 AND taaknr = 6 AND monteurcode = 'LM1"
Table structure OpdrachtTaak:
opdrachtnr NUMERIC(5) <pk,fk2> NOT NULL
taaknr NUMERIC(3) <pk, fk3> NOT NULL
tijdsduur NUMERIC(4,1) NULL
Monteurcode VARCHAR(3) <fk1> NULL
Would be great if you can help me out.

Delete statements in MS Access databases is fairly different from those in SQL Server.
In MS Access you must include From statement in your delete query. So your code will be:
DeleteSQL = "DELETE * FROM OpdrachtTaak WHERE opdrachtnr = " & Me.comboOpdracht.Value & " AND taaknr = " & Me.lstTaakMonteur.Value & " AND monteurcode = '" & Me.comboMonteur.Value & "'"

Related

How to find a syntax error in my SQL code?

I am trying to compile my code but I get the same error every time:
Syntax error (missing operator ) in query expression
' True Status.Subsystem Not LIKE '''
This is my code :
Sub Import_Loop_Check_list()
Dim strSQL As String
Dim SS_sel As String
Dim rcrd As DAO.Recordset
If IsNull(Cobsubsystem) Then
SS_sel = "True"
Else
If IsNull(Logic1) Then
SS_sel = "Status.Subsystem LIKE '" & Cobsubsystem & "' "
Else
SS_sel = "Status.Subsystem NOT LIKE '" & Cobsubsystem & "' "
End If
End If
strSQL = " SELECT DISTINCT LOOP_JB.Loop_name, [Easyplant Dump query].Subsystem, LOOP_JB.PANEL_FROM, LOOP_JB.ITR_PANEL_FROM, LOOP_JB.ITR_PANEL_FROM_state, LOOP_JB.CABLE_NUM, LOOP_JB.ITR_cable, LOOP_JB.ITR_STATE_Cable, LOOP_JB.Cabinet_JB, LOOP_JB.ITR_Cabinet_JB, LOOP_JB.ITR_STATE_CABINET, Multicors.CABLE_NUM AS Multicore, Multicors.ITR_PANEL_FROM, Multicors.ITR_PANEL_FROM_state, [Cabinet query].PANEL_TO, [Cabinet query].ITR_PANEL_TO, [Cabinet query].ITR_PANEL_TO_state INTO [LOOP_Check] " & _
" FROM (LOOP_JB INNER JOIN ([Cabinet query] RIGHT JOIN Multicors ON [Cabinet query].CABLE_NUM = Multicors.CABLE_NUM) ON LOOP_JB.Loop_name = Multicors.Loop_name) INNER JOIN [Easyplant Dump query] ON LOOP_JB.Loop_name = [Easyplant Dump query].Clean_Tag_Number" & _
" WHERE True " & SS_sel & strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
DoCmd.OpenTable "LOOP_Check"
End Sub
I looked at this again -- something else does not make sense.
You reference Status.Subsystem in the WHERE but there is no table named Status -- Did you not include the full query?
original answer
I think the error message is clear -- you have a strange where statement
WHERE True Status.Subsystem Not LIKE
you probably mean
WHERE Status.Subsystem Not LIKE
so change this line
" WHERE True " & SS_sel & strSQL
to this
" WHERE " & SS_sel & strSQL
Also, it does not right to me -- are you sure you want to do a RIGHT join to Multicors and not a left join? You want a row in your result for every row in the multicors table?

How do I concatenate a variable of the type Date/Time in my Insert statement

This runs when the form loads:
Private Sub Form_Load()
TempVars("F_ID") = txtF_SNr.Value
TempVars("Status") = txtStatus.Value
TempVars("seit") = txtSeit.Value
TempVars("bis") = txtBis.Value
TempVars("von") = txtVon.Value
TempVars("an") = txtAn.Value
TempVars("Bemerkung") = txtBemerkung.Value
End Sub
txtSeit is the name of the text field in my form, bound to the column [seit] with the type of Date/Time in my table.
sql = "Insert into tblStatusFahrzeugeHistory (F_ID, Status, seit, bis, von, an, Bemerkung, Datenbank_Nutzer, Eintrag_erstellt_am, Eintrag_erstellt_um)" & _
"Select tblStatusFahrzeuge.F_ID, tblStatusFahrzeuge.Status, tblStatusFahrzeuge.seit, tblStatusFahrzeuge.bis, tblStatusFahrzeuge.von, tblStatusFahrzeuge.an, tblStatusFahrzeuge.Bemerkung, tblStatusFahrzeuge.Datenbank_Nutzer, tblStatusFahrzeuge.Eintrag_erstellt_am, tblStatusFahrzeuge.Eintrag_erstellt_um " & _
"From tblStatusFahrzeuge Where ( " & txtSeit.Value & " <> seit AND [F_ID] = '" & Me.txtF_SNr.Value & "') "
The sql statement above gives me the error:
runtime error: '3075' syntax error in number in query expression...
How do I concatenate txtSeit.Value correctly? (Sorry im a beginner!)

VBA (access) set data type when creating a table

I have a small problem. I have a VBA macro that creates a table. It works. But Call set data type "short text" and a need a number for column FB_average, Cil_FB and Plneni_FB.
Does anyone please have a solution tip?
Call generuj_t_vystup("t_prod_work_time_fb.Skill,t_prod_work_time_fb.Skill_KM,t_prod_work_time_fb.Locality,t_prod_work_time_fb.Team,t_prod_work_time_fb.Spv_name,t_prod_work_time_fb.Agent_name,t_prod_work_time_fb.Agent_login,t_prod_work_time_fb.Rok,t_prod_work_time_fb.Kvartal,t_prod_work_time_fb.Měsíc,t_prod_work_time_fb.Tyden,t_prod_work_time_fb.Event_date", "t_vystup_fb_denni_agent")
Public Function generuj_t_vystup(hlavicka As String, vystupni_tabulka As String)
SQL = "Select "
SQL = SQL & hlavicka & ", "
SQL = SQL & "Nz(SUM([Feedback_summary])) AS FB_summary,"
SQL = SQL & " Nz(SUM([Feedback_count])) AS FB_count,"
SQL = SQL & " Nz(SUM([Feedback_summary])/SUM([Feedback_count])) AS FB_average,"
SQL = SQL & " Nz(SUM([Feedback_summary])/SUM([Feedback_count])) AS Cil_FB,"
SQL = SQL & " Nz(SUM([Feedback_summary])/SUM([Feedback_count])) AS Plneni_FB"
SQL = SQL & " INTO "
SQL = SQL & vystupni_tabulka
SQL = SQL & " FROM t_prod_work_time_fb "
SQL = SQL & " GROUP BY "
SQL = SQL & hlavicka
spustsql SQL
End Function
Sub spustsql(SQL As Variant)
On Error GoTo chyba
DoCmd.RunSQL SQL
Exit Sub
Thanks a lot.
Set a numeric value (0) to replace Null:
SQL = SQL & " Nz(SUM([Feedback_summary]), 0) AS FB_summary,"
SQL = SQL & " Nz(SUM([Feedback_count]), 0) AS FB_count,"
SQL = SQL & " Nz(SUM([Feedback_summary])/SUM([Feedback_count]), 0) AS FB_average,"
SQL = SQL & " Nz(SUM([Feedback_summary])/SUM([Feedback_count]), 0) AS Cil_FB,"
SQL = SQL & " Nz(SUM([Feedback_summary])/SUM([Feedback_count]), 0) AS Plneni_FB"

New ODBC issue Access 2007

I have an MS Access 2007 front end to a MS SQL Server 2008R2 back end.
I've got a query that's part of a loop, and it's suddenly started generating Run-time error '3146': ODBC--call failed. errors on one query as the loop itterates. Oddly, it's not the first iteration. EDIT: When I say suddenly, it's been running like this just fine for several months (probably since March).
Set db = CurrentDb
db.QueryTimeout = 480 'thought there was a timeout issue, so set this very high
Set SupvRS = db.OpenRecordset("SELECT DISTINCT tblProcessors.Supervisor, tblProcessors.SupervisorEmail " & _
" FROM tblProcessors INNER JOIN (tblAuditPr INNER JOIN tblAuditPr_A ON tblAuditPr.PrAudit_ID = tblAuditPr_A.PrAudit_ID)" & _
" ON tblProcessors.Processor = tblAuditPr_A.Processor" & _
" WHERE tblProcessors.Supervisor IS NOT NULL " & _
" AND tblAuditPr.EndDate BETWEEN " & GetSQLDate(Me.txtFrom) & " AND " & GetSQLDate(Me.txtTo) & _
" AND tblProcessors.Processor<>'Default Processor' " & _
" AND tblAuditPr_A.Answer Not In ('NA','NF')" & _
" AND tblAuditPr.Status = 'Submitted'")
Do While Not SupvRS.EOF
'Send Supervisor Email
Attachment = PARG.GenerateAuditReport(SupvRS.Fields("Supervisor"), parSupervisor, Me.txtFrom, Me.txtTo)
Set EmailRS = db.OpenRecordset("SELECT * FROM tblProcessors WHERE Supervisor = " & GetSQLString(SupvRS.Fields("Supervisor")))
If EmailRS.EOF Then
Err.Raise -234923, Description:="Cannot find tblProcessors record for " & SupvRS.Fields("Supervisor") & "."
End If
If IsNull(EmailRS.Fields("SupervisorEmail")) Then
MailTo = <redacted>
Else
MailTo = EmailRS.Fields("SupervisorEmail")
End If
Set EmailRS = Nothing
Mailer.AddMailDocument MailTo:=MailTo, _
Subject:=Subject, _
Body:=Body, _
Attachments:=Attachment, _
From:=<redacted>
'Iterate processor emails if requested.
If Me.chkProcessor Then
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'----------------------------------------------------------------------------------------
'ERROR occurs here, but only after several supervisors have successfully passed through
Set ProcRS = db.OpenRecordset("SELECT DISTINCT tblProcessors.Processor, tblProcessors.ProcessorEmail FROM tblAuditPr" & _
" INNER JOIN (tblProcessors INNER JOIN tblAuditPr_A ON tblProcessors.Processor = tblAuditPr_A.Processor) " & _
" ON tblAuditPr.PrAudit_ID = tblAuditPr_A.PrAudit_ID" & _
" WHERE tblProcessors.Supervisor = " & GetSQLString(SupvRS.Fields("Supervisor")) & _
" AND tblAuditPr.EndDate BETWEEN " & GetSQLDate(Me.txtFrom) & " AND " & GetSQLDate(Me.txtTo))
'----------------------------------------------------------------------------------------
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Do While Not ProcRS.EOF
If Not IsNull(ProcRS.Fields("ProcessorEmail")) Then
On Error Resume Next
Attachment = PARG.GenerateAuditReport(ProcRS.Fields("Processor"), parProcessor, Me.txtFrom, Me.txtTo)
If Attachment <> "" Then
MailTo = ProcRS.Fields("ProcessorEmail")
Mailer.AddMailDocument MailTo:=MailTo, _
Subject:=Subject, _
Body:=Body, _
Attachments:=Attachment, _
From:=<redacted>
Else
Err.Clear
End If
On Error GoTo 0
End If
ProcRS.MoveNext
Loop
Set ProcRS = Nothing
End If
SupvRS.MoveNext
Loop
As noted in the code, the error occurs on an inner loop that successfully executes for other supervisors. It does consistantly break on one particular individual. Using the GetSQLString and GetSQLDate functions (returns ' delimited, double '' when necessary, pretty strings, and # delimited dates, respectivly, for passing Access queries through to SQL Server) in break mode, I duplicated the exact query that was giving me problems in an Access query. It returned a timeout error, but changing the database default timeout (and closing/opening the db) didn't resolve the issue. I pasted the exact same query into SSMS (replacing the # date delimiters with ') and it executed just fine there (taking about 1:30-2:00 to execute each try).
Taking a pause in typing up this post, I put some error trapping in to see if I could get the actual error message SQL Server was returning, when it ran without error. To me, that indicates that it is likely a time out issue. Anyone have a more educated guess as to what it actually is, or a suggestion on how to make the query more efficient so it doesn't time out?

SQL Error Access 2010 VBA Update Command

I need help figuring out an error in my SQL statement. I have been trying several things, but nothing seems to work?
This is the error message I receive
Run-time error '3075':
Syntax error (missing operator) in query expression '([description] = Manufacturing and Delivery Schedule AND [pr_num] = 83)'.
This is my code:
Private Sub Command6_Click()
' ===================================================
' Receives the selected item in the combo box
' ===================================================
' Open the Database connection
Dim data_base As Database
Set data_base = CurrentDb
' Grab description and pr number from the form
Dim desc As string
dim pr_number as long
desc = Combo4.Value
pr_number = Text8.Value
' Build the query
Dim query As String
query = "UPDATE VDR_Table " & _
"SET [received] = [p1] " & _
"WHERE ([description] = " & desc & _
" AND [pr_num] = " & pr_number & ");"
Dim rec_set As DAO.Recordset
Set rec_set = data_base.OpenRecordset(query)
' Build the QueryDef
Set qd = data_base.CreateQueryDef("")
qd.SQL = query
' Execute query
qd.Parameters("p1").Value = true
qd.Execute
' Close nad null record set
rec_set.close
set rec_set = nothing
' Close the connection to the database
data_base.Close
' Prompt the user success
MsgBox "Item has been received"
End Sub
Thanks in advance for any help!
You need to enclose the description field value you are setting in quotes since it is a string field. It should look like this:
' Build the query
Dim query As String
query = "UPDATE VDR_Table " & _
"SET [received] = [p1] " & _
"WHERE ([description] = '" & desc & _
"' AND [pr_num] = " & pr_number & ");"
Removed the links below since they don't matter in this case.
Also, I would recommend using parameters instead of string concatenations to avoid SQL injections. Here's an example of using parameters with VBA - http://support.microsoft.com/kb/181734 - and here is some reasoning on why to use parameterized sql - http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html.