Results 1 to 10 of 10
  1. #1
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581

    List box criteria

    I have a list box, lstVehicleMileage.
    The SQL in the Row Source is: SELECT [MileageID], [UnitTypeID], [UnitName], [UseDate], [BeginMileage], [EndMileage], [TotalMileage] FROM qryVehicleMileageUser ORDER BY [UseDate] DESC;
    I want it to only get the records of the Employee on the form. I want it to look for [EmployeesID] in that query. The form is called, frmVehicleMileage.

    Is there a way to write the criteria in the list box instead of the query? That way, I can use that same query for something else also.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Don't reference query object. Build the SQL statement directly in the RowSource.

    SELECT [MileageID], [UnitTypeID], [UnitName], [UseDate], [BeginMileage], [EndMileage], [TotalMileage] FROM qryVehicleMileageUser WHERE EmployeesID = [EmployeesID] ORDER BY [UseDate] DESC;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Worked perfectly. Thanks.

  4. #4
    MadTom's Avatar
    MadTom is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Location
    CT and VT
    Posts
    206
    You could use
    Like "*" & [Enter Name:] & "*" in the criteria in your Query and use the QueryName in the RowSource and call the Query from your form. You can still use the Query for other things.
    MT

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    If EmployeesID is just digits, LIKE might not work nice. LIKE "*2*" would return 2, 200, 1200, 702, etc. So if the field is autonumber, a bunch of records would return.

    If EmployeesID is text field and value is fixed length such as 021049, 135207, then the LIKE and wildcard would work.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I'm having problems with this again. I have another form that I'm trying to do almost the same thing. I have a list box [lstCTWSubjectInformation]. The SubjectID is column 1. That information comes from a query [qryCTWSubjectInReport]. My second list box is trying to get information from a query [qryCTWTrespassedFrom]. I'm trying to use the information from the first list box [lstCTWSubjectInformation] as the criteria. This is what I came up with:
    SELECT [SubjectID], [BldgName] FROM qryCTWTrespassedFrom WHERE SubjectID = forms!frmRptCTWEntry!lstCTWSubjectInformation.colu mn(1) ORDER BY [BldgName];

    That gave me this error:
    Click image for larger version. 

Name:	error1.jpg 
Views:	9 
Size:	18.0 KB 
ID:	38511

    I'm not sure what I'm doing wrong.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Query objects cannot resolve the Column(x) reference.

    Columns are referenced by index. Index begins with 0. So if data is in first column, its index is 0.

    Don't use the Column(x) reference.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    This wasn't in the query. It was in the code to the list box.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    You mean the RowSource property? Same issue. It's still an SQL statement and Access is running a query in the background.

    A textbox ControlSource property can resolve the Column(x) reference. So if you need a value from column other than 0, have a textbox pull that value then SQL references the textbox as parameter.

    However, if desired value is in first column, there is no need to use Column(0).
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I simplified to for myself. I just redid my query and changed the sql so that the subjectID was 0. It was in column 1. It's working. Thanks.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. List Box Items as criteria
    By Alex Motilal in forum Programming
    Replies: 3
    Last Post: 07-11-2018, 05:54 AM
  2. Replies: 6
    Last Post: 07-01-2015, 09:56 AM
  3. Replies: 5
    Last Post: 08-02-2012, 09:44 AM
  4. List Criteria
    By drow in forum Forms
    Replies: 1
    Last Post: 03-12-2012, 11:19 PM
  5. Querie criteria with list
    By apsf68 in forum Access
    Replies: 1
    Last Post: 12-04-2010, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums