简体   繁体   中英

Filtering dynamic SQL statement in MS Access 2010

I'm using MS Access 2010

I have a listbox to select the columns of my query, I would like to know is it possible to filter the first or the entire selection of the list box.

Below I have my code for the entire selection and process of getting the info to an empty query and filtering by the ship name and by the selection of the listbox (it works if I only select one field) but if I do multiple selection then i get this error.

The error message is

Run-Time error '3075'
Syntax error (comma) in query expression '(((q1_shcedule.ship) =[Forms]![dlg Manifest Report by date Selection]![dbShip] or IsNull([Forms]![dlg manifest report by date selection]![dcShip]))) AND (Jan_10, Jan_11)= "SH" Or (Jan_10, Jan_11) = "v"`

Any suggestions would be really appreciated.

Thanks.

Dim cdb As DAO.Database, qdf As DAO.QueryDef
Const queryName = "Manifest Report By Date Selection"

Dim strWhere As String
Dim strDate As String
Dim strEnd As String
Dim ctl As Control
Dim varItem As Variant
Dim Criteria As String
Dim itm As Variant

strWhere = "(" & CodeDate.Value & ")"

Set ctl = Me.CodeDate
For Each varItem In ctl.ItemsSelected
  strWhere = strWhere & ctl.ItemData(varItem) & ", "
  'Use this line if your value is text
  'strWhere = (strWhere) & "'" & ctl.ItemData(varItem) & "',"
Next varItem

'trim trailing comma
strWhere = Left(strWhere, Len(strWhere) - 2)

strWhere = Right(strWhere, Len(strWhere) - 2)

Set cdb = CurrentDb
DoCmd.Close acQuery, queryName, acSaveNo
On Error Resume Next
DoCmd.DeleteObject acQuery, queryName
On Error GoTo 0

Set qdf = cdb.CreateQueryDef(queryName, _
       "SELECT Q1_Schedule.[Resource Name], Gender, Company, Type, Position, Contract_End_Date, Q1_Schedule.IndividID, Q1_Schedule.Ship, Q1_Schedule.App, Q1_Schedule.SubApp, " + strWhere + " " & _
          "FROM Resource INNER JOIN (Q4_Schedule INNER JOIN (Q3_Schedule INNER JOIN (Q2_Schedule INNER JOIN Q1_Schedule ON (Q2_Schedule.App = Q1_Schedule.App) AND (Q2_Schedule.Ship = Q1_Schedule.Ship) AND (Q2_Schedule.IndividID = Q1_Schedule.IndividID)) ON (Q3_Schedule.App = Q2_Schedule.App) AND (Q3_Schedule.Ship = Q2_Schedule.Ship) AND (Q3_Schedule.IndividID = Q2_Schedule.IndividID)) ON (Q4_Schedule.App = Q3_Schedule.App) AND (Q4_Schedule.Ship = Q3_Schedule.Ship) AND (Q4_Schedule.IndividID = Q3_Schedule.IndividID)) ON (Resource.IndividID = Q4_Schedule.IndividID) AND (Resource.IndividID = Q3_Schedule.IndividID) AND (Resource.IndividID = Q2_Schedule.IndividID) AND (Resource.IndividID = Q1_Schedule.IndividID) Where (((Q1_Schedule.Ship) = [Forms]![dlg Manifest Report by Date Selection]![dbShip] OR IsNull([Forms]![dlg Manifest Report by Date Selection]![dbShip]))) AND  (" + strWhere + ") = ""SH"" Or (" + strWhere + ") = ""V"" ORDER BY Type DESC ")
       '   "ORDER BY Type DESC"



'Set qdf = cdb.OpenRecordset("Entire_Manifest", _
dbOpenDynaset, dbDenyRead)

Set qdf = Nothing
Set cdb = Nothing
DoCmd.OpenQuery queryName, acViewNormal, acEdit
Exit Sub

ErrorHandler:

MsgBox "Please Select A Range Of Date Or A Ship Name"  

在创建查询并进行研究之前,请打印出您的SQL字符串。

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