Need Help with " Run-time error '1004': SQL Syntax Error"

livetolearn4life

New Member
Joined
Jul 14, 2020
Messages
15
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
Hello hope you all are having a great day!

i'm looking for a solution to a " Run-time error '1004': SQL Syntax Error" that occurs at the " .Refresh BackgroundQuery:=False" line. Any help would be greatly appreciated. Thanks!


Sub Purged_MO_HRS()
''QUERY HOURS DATA FOR FAI AND TLG MANUFACTURING ORDERS THAT HAVE PURGED/CLOSED OUT
Worksheets("Purged MO HRS").Activate
Cells.Select
Selection.ListObject.QueryTable.Delete
Selection.ClearContents




With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"ODBC;DSN=AMFLIB1;UID=ddt;PWD=xs892kmfg", Destination _
:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT MOHMST.ORDNO, MOHMST.REFNO, MOHMST.JOBNO, MOHMST.FITEM, MOHMST.FDESC, MOHRTG.OPSEQ, MOHRTG.WKCTR, MOHRTG.SRLHU, MOHRTG.SSLHU, MOHRTG.SETCS, MOHRTG.OPSTC, MOHRTG.QUETM, MOHRTG.RLHTP, MOHRTG.RLHTD, " _
, "" & Chr(13) & "" & Chr(10) & "FROM S1099F6P.AMFLIB1.MOHMST MOHMST, S1099F6P.AMFLIB1.MOHRTG MOHRTG" & Chr(13) & "" & Chr(10) & "WHERE MOHRTG.CLDT = MOHMST.CLDT AND MOHRTG.ORDNO = MOHMST.ORDNO AND ((MOHMST.CRUS='EFS') " _
, "AND (MOHMST.OSTAT<'99') OR (MOHMST.CRUS='KKR') AND (MOHMST.OSTAT<'99') OR (MOHMST.CRUS='CBL') AND (MOHMST.OSTAT<'99') OR (MOHMST.CRUS='TBD') AND (MOHMST.OSTAT<'99') AND " _
, "(MOHMST.JOBNO Like '%240124') OR (MOHMST.CRUS='FTE') AND (MOHMST.OSTAT<'99') AND (MOHMST.JOBNO Like '%240124') OR (MOHMST.CRUS='DSN') AND (MOHMST.OSTAT<'99') AND (MOHMST.JOBNO Like '%240124')) " _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Purged_MO_HRS"
.Refresh BackgroundQuery:=False


End With


Range("A1").Select

ActiveSheet.ListObjects("Table_Purged_MO_HRS").TableStyle = _
"TableStyleLight13"

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Remove the colon after the word Query. Replace it with a space.
 
Upvote 0
This is how it should look.

VBA Code:
.Refresh BackgroundQuery = False

Nore the space before and after the equal symbol.
 
Upvote 0

Forum statistics

Threads
1,221,706
Messages
6,161,406
Members
451,702
Latest member
Kc3475

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top