简体   繁体   中英

MS Access 2010 VBA SQL error 3122

I am hitting a snag in my SQL and I can't seem to figure out the solution. I have isolated the part of the code causing the error, but I can't seem to fix it. The issue is in the (TimeValue([tblDeptTime]![DeptTime]) BETWEEN (TimeValue([tblAllSchedules]![MX Out Date/Time]) - [tblDeptWindowsLeadTimes]![NBLT]) AND TimeValue([tblAllSchedules]![MX Out Date/Time]) portion of my SQLHaving statement.

The error states that I am trying to execute a query that does not include the specified expression [Schedule Name])=[Forms]![frmStationReports]![cbxSchedName]) AND (([tblAllSchedules]![Arvl Sta])=[Forms]![frmStationReports]![cbxArvlSta]) AND DateValue([tblAllSchedules]![MX Out Date/Time]) >= #" & FMon & "# AND DateValue([tblAllSchedules]![MX Out Date/Time]) <= #" & LSun & "#) AND ((tblAllSchedules.Type)='NB' This is the first part of my SQLHaving statement.

Here is the entire SQL code. Thanks for your assistance.

SQLInsert = "INSERT INTO tblDeptWindowNB ( [Schedule Name], [Arvl Sta], [Day], [Date], Metal, Type, [Key ID], [LAA NB], [LUS NB], [Sub Fleet], CompSchedName, CompSchedDay, DeptTime ) "
    SQLSelect = "SELECT [Schedule Name], [Arvl Sta], Format(DateValue([tblAllSchedules]![MX Out Date/Time]),'ddd') AS [Day], DateValue([tblAllSchedules]![MX Out Date/Time]) AS [Date], Metal, Type, [Key ID], [LAA NB], [LUS NB], [Sub Fleet], '" & csName & "', Sum(DateDiff('d', '" & FMon & "' ,  [tblAllSchedules]![MX In Date/Time])+ 1) AS CompSchedDay, [tblDeptTime]![DeptTime] "
    SQLFrom = "FROM tblDeptTime, tblAllSchedules, tblDeptWindowsLeadTimes "
    SQLGroup = "GROUP BY [Schedule Name], [Arvl Sta], DateValue([tblAllSchedules]![MX Out Date/Time]), [tblDeptTime]![DeptTime], Metal, Type, [Key ID], [LAA NB], [LUS NB], [Sub Fleet] "
    SQLHaving = "HAVING ((([tblAllSchedules]![Schedule Name])=[Forms]![frmStationReports]![cbxSchedName]) AND (([tblAllSchedules]![Arvl Sta])=[Forms]![frmStationReports]![cbxArvlSta]) AND DateValue([tblAllSchedules]![MX Out Date/Time]) >= #" & FMon & "# AND DateValue([tblAllSchedules]![MX Out Date/Time]) <= #" & LSun & "#) AND ((tblAllSchedules.Type)='NB') AND (TimeValue([tblDeptTime]![DeptTime]) BETWEEN (TimeValue([tblAllSchedules]![MX Out Date/Time]) - [tblDeptWindowsLeadTimes]![NBLT]) AND TimeValue([tblAllSchedules]![MX Out Date/Time]))"
    SQLOrderBy = "ORDER BY [Schedule Name], [Arvl Sta], DateValue([tblAllSchedules]![MX Out Date/Time])"

    SQLStatement = SQLInsert & SQLSelect & SQLFrom & SQLGroup & SQLHaving & SQLOrderBy & ";"

    DoCmd.RunSQL SQLStatement

I figured it out. I needed to move the (TimeValue([tblDeptTime]![DeptTime]) BETWEEN (TimeValue([tblAllSchedules]![MX Out Date/Time]) - [tblDeptWindowsLeadTimes]![NBLT]) AND TimeValue([tblAllSchedules]![MX Out Date/Time]) statement into a Where statement and now it works perfectly. Thanks guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM