Results 1 to 8 of 8
  1. #1
    Ramun_Flame is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    19

    Filter and Sort a form based on TextBox Values.

    I have a few text boxes that have values assigned to them. I want to be able to filter and sort based on those values in the them, but I can't seem to figure out a way.

    Let's say I have a Textbox called EndTime. This is not a field in the table, it is a text box taking the value of StartTime, a field in the table, plus an hour. Don't tell me to just filter based on the start time, since this is just an example, and I want to know how to do it with the textbox.

    So this is what I would use to filter:

    DoCmd.ApplyFilter , "EndTime < Now()"

    This would prompt me for the value of EndTime, since it doesn't realize it is a textbox. It must be looking for a table field called EndTime, but there isn't one.

    This is what I would use to sort it ascending:



    DoCmd.SetOrderBy "EndTime ASC"

    This would do nothing.

    I've been looking all over for a way to reference a textbox in these commands, but I can't seem to find anything. Is it only possible to reference table fields here? If so, please let me know, so I can rethink how I am doing this.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Don't put variable within quote marks. Reference to control on form is a variable. Concatenate variables. Either of these should work:

    DoCmd.ApplyFilter , Me.EndTime & "<Now()"
    DoCmd.ApplyFilter , Me.EndTime & "<" & Now()

    Then
    DoCmd.SetOrderBy Me.EndTime & " ASC"
    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
    Ramun_Flame is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    19
    StartT is a field in the table, EndT is a textbox on the form with the control source of =StartT + 1/24(adds an hour)

    Okay, I tried a few things.

    DoCmd.ApplyFilter , Me.StartT & "= #11:00AM#" This does nothing.

    DoCmd.ApplyFilter , Me.EndT & "= #11:00AM#" This does nothing.

    DoCmd.ApplyFilter , "StartT = #11:00AM#" This works.

    DoCmd.ApplyFilter , "Me.StartT = #11:00AM#" This asks me for what is Me.StartT.

    DoCmd.ApplyFilter , "EndT = #11:00AM#" This asks me for what is EndT.

    DoCmd.ApplyFilter , "Me.EndT = #11:00AM#" This asks me for what is Me.EndT

    Not sure what to try now.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I knew that expression looked odd to me, should have thought about it some more. It doesn't make sense to set the value in textbox = to another value as filter criteria.

    I've never used the ApplyFilter or SetOrderBy methods so this is a learning experience for me.

    This expression must be like the WHERE clause of an sql statement. That means the criteria must include a field that is in the form's RecordSource, just as you asked earlier.

    The textbox can provide the parameter for the filter expression, as in:

    DoCmd.ApplyFilter , "StartT = #" & Me.StartT & "#"
    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.

  5. #5
    Ramun_Flame is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    19
    So then, is there another way to filter or sort based on a textbox then? Since the expression I was trying only takes tables fields.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    That IS filtering/sorting based on textbox. Any filter/sort process must identify the field that the parameter is applied to. Textbox provides the parameter for the expression.
    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.

  7. #7
    Ramun_Flame is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    19
    Yeah, but I don't want to check if a field is equal to a text box, I want to check if a text box is equal to a value.

    Edit:

    I don't need to do this anymore. Instead of using Dlookup to show information, I just made a query with a one to many relationship between the tables. This means I can update the table, and have all the fields, while keeping them separate for organizations sake. Thanks for the help anyway,

  8. #8
    Rainlover's Avatar
    Rainlover is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2009
    Location
    Queensland Australia
    Posts
    691
    If you designed this in the Query Grid it would be so much easier.

    One you got it working you could copy the SQL and use that in Code as your recordsource.

    << Don't tell me to just filter based on the start time, since this is just an example >>

    I don't understand why not. Placing a Formula on the start time gives the result you want.

    I must be missing something.

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

Similar Threads

  1. Replies: 2
    Last Post: 03-30-2012, 08:26 AM
  2. Replies: 1
    Last Post: 12-04-2011, 06:33 PM
  3. Report Based on Form - Sort
    By jeffyyy in forum Reports
    Replies: 1
    Last Post: 10-16-2010, 06:15 PM
  4. Replies: 1
    Last Post: 04-27-2010, 09:30 PM
  5. Form Based Query/Filter
    By Micon in forum Access
    Replies: 0
    Last Post: 11-07-2008, 09:25 AM

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