Results 1 to 4 of 4
  1. #1
    Lhoj is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2021
    Posts
    92

    Restore unbound controls' value

    Hello there,



    I'm having trouble trying to reset unbound controls to their previous value. Right now I have a series of text fields, comboboxes and checkboxes that I want to use as filter selectors for an individual form. I have some custom record navigation buttons that cycle the records that fit the selected filters. The filter (or combination of filters) applies well, but I want to add the following functionality. If a user selects a new value on a filter selector (be it a combobox, text field or whatever) and the resulting filter would yield no records, I want to leave the remaining controls as they were, revert the modified control to its previous value and leave the filter untouched. So the obvious choice seemed like a beforeupdate event for each control. When any filter field is changed, the beforeupdate event calls a custom function that sets up the whole filter query and checks if any record matches it. If so, it stores the filter query in a variable (since it wouldn't let me apply the filter during a beforeupdate event) and returns a 0 value to the beforeupdate event. If not, it returns a 1 value. Then the afterupdate event (if triggered) calls the filter sub that applies the filter stored in the variable.

    The beforeupdate event looks like this:
    Code:
    Private Sub filtro_modelo_BeforeUpdate(Cancel As Integer)
        If checkFiltro = 1 Then
            Me.filtro_modelo.Undo
            Cancel = True
        End If
    End Sub
    The cancel bit is working since the afterupdate doesn't get triggered, but the .undo isn't reverting the value to whatever it was before, thus making the combobox or textfield show a value different to the one corresponding to the current filter.

    Any idea why the .undo doesn't work? Is there any other way to get the desired result other than doing it manually passing it in a variable or something?

    Thank you very much!

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    IIRC, Undo applies to bound forms (their records or individual controls/fields). You could fix this with a lot of code, or you could base these search fields on a search field table. Then you could enter the values into that table and take advantage of not only Undo but the OldValue property of a control. If you do that and need to make comparisons, be aware that while the Text property (what you see) of a control is Dog, the value may be Cat because it hasn't been updated yet. IIRC this would only apply to bound controls.

    If you really must take a code route, I'm sure we can come up with something, but I'd try the table first. Don't forget to wipe the table when the form closes.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Lhoj is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2021
    Posts
    92
    So .undo restores the value from the bound field then, right? A shame that it doesn't internally store the control's value for unbound controls. Since I already have the before and afterupdate events set up I'll just store the value in a variable on the afteraupdate event and then restore the current filter value (if any) on the beforeupdate if there are no records with the new value. At this points is seems faster than bounding the controls to a new table.

    Thanks Micron! Much appreciated!

  4. #4
    Lhoj is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2021
    Posts
    92
    Ok, so it won't let me tamper control's value during the beforeupdate so I couldn't restore the value, but I came up with a better solution seeing how after cancelling the beforeupdate event it would still let me change the value back with the esc key. So now instead of .undo, I put a SendKeys "{Esc}" if my custom filter check returns a 1 (as for no results with resulting filter). No need to bound the fields or store the previous value.

    Code:
    Private Sub filtro_retirado_BeforeUpdate(Cancel As Integer)
        If checkFiltro = 1 Then
            Cancel = True
            SendKeys "{Esc}"
        End If
    End Sub
    Hope it helps anyone with the same issue.

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

Similar Threads

  1. Multiple unbound controls
    By slimjen in forum Forms
    Replies: 3
    Last Post: 12-12-2019, 10:53 AM
  2. Replies: 7
    Last Post: 09-28-2018, 05:30 PM
  3. Replies: 9
    Last Post: 07-14-2014, 07:56 PM
  4. Writing multiple unbound controls to a table
    By dccjr in forum Programming
    Replies: 4
    Last Post: 11-28-2012, 08:18 PM
  5. Replies: 1
    Last Post: 07-13-2012, 09:13 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