Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    restore previous value (2007)

    I have several controls on a form that will contain numbers. I also have two command buttons: Recommendation and Clear Recommendation . When I click on the Recommendation button, I will populate the numeric controls with a value based on a formula applied to the value of another control. What I would like is that when I click on the Clear Recommendation button, it will remove the calculated values and restore the numeric control values to whatever they were before the Recommendation was made (using the command button); if they were empty, they should be returned to an empty state.

    What is the best way to do this?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: restore previous value (2007)

    If you want to be able to restore the original values during the current session, you could store them in public variables (defined at the top of a standard module) in the code behind the Recommendation button, before populating the controls with new values.
    For a more robust solution that will even work across sessions, you could store the original values in a separate table.

  3. #3
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: restore previous value (2007)

    I only envision this taking place during a current session... even if it is more than one session, the original value will be the value present when the record is shown on the form.

    As far as storing them, it that simply saying that variable1=control1, variable2=control2? Then when restoring the value... control1=variable1, control2=variable2? I think I would need to reset variable1 & variale2 to null during an oncurrent event for the form. Is that correct?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: restore previous value (2007)

    > As far as storing them, it that simply saying that variable1=control1, variable2=control2? Then when restoring the value... control1=variable1, control2=variable2?

    Yes. I'd declare all the variables as Variant, regardless of the data type of the corresponding fields, so that they can hold Null values.

    > I think I would need to reset variable1 & variale2 to null during an oncurrent event for the form.

    You could fill them with the values from the current record.

    Or you could use an additional Boolean variable blnSet. Set blnSet to False in the On Current event of the form, and to True in the On Click event of the Recommendation button. In the On Click event of the Clear Recommendation button, only restore the values of the controls if blnSet is True, and set it to False afterwards. You might also want to set blnSet to False in the AfterUpdate event of the form, so that Clear Recommendation doesn't work after the record has been saved; this is for you to decide.

  5. #5
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: restore previous value (2007)

    Given that the old values only need to be remembered within one record at a time, could Don achieve this by just using control.oldvalue?
    Regards
    John



  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: restore previous value (2007)

    That depends on what Don actually wants - if the user edits a control, then clicks Recommendation, OldValue is the stored value of the underlying field, not the value of the control before Recommandation was clicked.

  7. #7
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: restore previous value (2007)

    Thanks for the ideas - I need to think about this and how it will be used. I wonder, if I wanted the user to have the option of EITHER restoring the value to just before the recommendation button was clicked OR to restore the original value of the control before it was (possibly) edited could I use both a named variable (variable1) AND OldValue then apply as appropriate depending on the button clicked (I would, of course, add another button)?

    Also, what is blnSet and how would use it? Does it offer any advantage over the other techniques mentioned?

  8. #8
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: restore previous value (2007)

    John, I just tried using oldvalue and I get a #Name? in the text box. In the control source data property of the textbox I have me.fname.oldvalue thinking it would render the original value of fname as stored in the table. Obviously I am not using it correctly. I just made a test text box on the form so I could see if it worked.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: restore previous value (2007)

    You shouldn't set the control source but the value of the control.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: restore previous value (2007)

    If you want to restore the stored value of all controls, you could simply run the line

    Me.Undo

    from the On Click event of a command button (or the user could press Esc twice).

    blnSet is a variable defined by you, just like the variables to store the values of the controls. I suggested using this variable to keep track of whether the values can be restored.

  11. #11
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: restore previous value (2007)

    Well, I changed from control source to default value in the property box... same result. How do i set the value - must it be done in code? can this be done for a single control or must it be for all controls?

    BTW, it is an ubound textbox that I am using for this test.

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: restore previous value (2007)

    I don't understand what you're doing any more - I thought you were using code.

  13. #13
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: restore previous value (2007)

    I have not yet assigned code to the command buttons. I thought I would see how the oldvalue worked by creating a text box control in the design view of the form. I expected I could, in the properties, assign me.fname.oldvalue and see it on my form and have it remain constant even if I edited the fname field... I just want to see how it worked.

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: restore previous value (2007)

    You can only use the keyword Me in VBA, not in the control source of controls. You could use

    =[FName].[OldValue]

    in the control source of a text box. This text box should *not* be named FName, for that would cause confusion.

  15. #15
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: restore previous value (2007)

    thanks - exactly what I needed. There is yet a question pending.

    If I wanted the user to have the option of EITHER restoring the value to just before the recommendation button was clicked OR to restore the original value of the control before it was (possibly) edited could I use both a named variable (variable1) AND OldValue then apply as appropriate depending on the button clicked (I would, of course, add another button)?

    Would this make sense?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •