Variable loses it's initial value RRS feed

  • Question

  • Using Access 2013.

    I have a variable defined in a form as
    Private order_list_by as boolean
    In the Form>Open event I set the variable to True:
    order_list_by = True

    But the first time I test the variable, when a user clicks a button to change the list order of a form, the value of the variable is False!

    So I thought I could snooker Access by changing to a string variable and put in a letter telling me the current list order, but now when the user clicks for the first time to request the list be reordered, the string is empty,  the variable = "".

    What happened to the value I stored in the variable when the form opened? Is there a way to keep from losing it?

    Thanks in advance for any help.  --Fred

    Tuesday, October 10, 2017 9:28 PM

All replies

  • I have a variable defined in a form as
    Private order_list_by as boolean
    In the Form>Open event I set the variable to True:
    order_list_by = True

    Hi Fredrated451,

    What happens if you declare the variable order_list_by as a global variable, that is without the Private, and if necessary in a general module (not the form's module)?


    Tuesday, October 10, 2017 9:53 PM
  • Hi Fred,

    Did you declare the variable outside of any procedure? When the user clicks on a button, is this button on the same form where you declared the variable?

    Tuesday, October 10, 2017 11:34 PM
  • Like DBguy and Imb said, you need to declare the variable first. So:

    Dim order_list_by As Boolean

    Me.order_list_by = True

    Tuesday, October 10, 2017 11:53 PM
  • Based on what you say, and assuming the variable is declared at the module level and not inside any procedure, then I  suspect that your form's Open event procedure is not running.  Can you verify that by setting a breakpoint in the procedure and seeing if it gets hit?  And if it is, you can step through the procedure and see whether the variable gets set as you expect and then maybe see what occurs to clear or reset it.

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Wednesday, October 11, 2017 4:55 PM
  • Thanks for all your replys. Set a breakpoint in the Open event is one of the first things I tried, and the variable is indeed set. And it is scoped at the form level.

    I eventually solved the problem without resorting to a variable.

    I was testing the variable to determine the order the records were being displayed in, then changing the recordsource to a new query to list them in a new order.

    To fix the problem I moved the record order to the 'OrderBy' form property with the following code:

    If Me.OrderBy = "[lastname], [firstname]" Then
       Me.cmdReorder.Caption = "Order by Name"
       Me.OrderBy = "[creation_date] DESC"
       Me.OrderByOn = True
       Me.cmdReorder.Caption = "Order by Date/Time"
       Me.OrderBy = "[lastname], [firstname]"
       Me.OrderByOn = True
    End If

    This is probably an all around better way to do what I want.

    Thanks for the reply's, this is why I love this forum, I learn so much and always get help when I post here.


    • Proposed as answer by Chenchen Li Friday, October 13, 2017 2:02 AM
    Thursday, October 12, 2017 3:40 PM
  • Hello,

    Thanks for sharing the solution. I suggest you mark it and helpful post as answer to close this thread. I think it would also be of great help for others.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 13, 2017 2:03 AM