Getting New Textbox value without using Afterupdate

Hi. I'm fairly new with programming and I hope someone can help me with this.

I trying to filter a list based on the value entered on a textbox but the only way I know of getting the new textbox value is wait for the AfterUpdate event. This works fine but I need to activate the filtering routine as the user types in the new value. Im thinking of using keypres, can anyone show me a way doing this.

Any help would be appreciated. Thanks



Answer this question

Getting New Textbox value without using Afterupdate

  • poletnik

    There are a few events you can use, the keypress, keydown or change event. Keypress is generally used to determine what type of key was pressed. It's used for validation, ensuring the key pressed was a character or a number. Keydown event is used to catch various key combinations, for example alt-f was pressed. Change event indicates that the text in the control has changed. Put the code behind the best event for whats required.

    The overall responsiveness of the application might suffer though with this idea. For example if your filtering a large amount of data then the user input might be affected. It might help if you filtered used wildcards. For example rather than searching for 'p', 'pr', 'pro', 'prod', 'produ', 'produc', 'product' perform the filter as 'p*', 'pr*', 'pro*', if you do this then the user needs to enter less text to find the item that they want which will reduce the filter hit.

    Private Sub txtProduct_Change()
    'bound form filtering on product text field based on the text being entered
    Me.Filter = "[Product] = '" & Me.txtProduct.Value & "*'"
    End Sub

    if your using a list box to display the results of the filter then...

    Private Sub txtProduct_Change()
    'list displaying the results of a filtered query
    Me.lstResults.RowSource = "SELECT * FROM [tblProduct] WHERE [Product] = '" & Me.txtProduct.Value & "*'"
    End Sub

    Hope thats useful



  • annemarie

    Thanks a lot.
  • Kundan Singh

    Thanks for replying. I tried as you suggested, using the keypress and change events, but I still get the old value from the textbox. I thinking of having the new value and invoking the filter routine each time the user presses a key.

    Here's my code.

    *******************************
    Private Sub DoFilter()
    Dim sStockFilter as String

    sStockFilter = Me.txtItem.Value & "*'" '<- here's where I get the problem

    Me.ItemList.Rowsource = "SELECT ItemCode, UPrice FROM StockItems" & _
    " WHERE ItemCode Like '" & sStockFilter & "'"
    End Sub

    Private Sub txtItem_KeyPress(KeyAscii As Integer)
    DoFilter
    End Sub

    *******************************

    My problem here is that if the old value of txtItem is 'pro' and then the user adds 'c' the value property is still set to 'pro' instead of 'proc'. How can I work around this so that the user don't have to press enter everytime to invoke AfterUpdate just to reset the list I'm using Access2000 by the way.

    Thanks.


  • DMcNeil

    Put the call in the KeyUp event that will solve it. I'm very surprised your getting the old value of the text box in the Change event, I'd imagine that would happen in the KeyPress event, but not the Change event. The KeyUp event is the last key based event to be raised, it shouldn't happen there.


  • Getting New Textbox value without using Afterupdate